2

with following code I try to connect to an azure database :

server = 'tcp:myserver.database.windows.net'
database = 'DBFREE'
username = 'user'
password = 'password'
conn = pyodbc.connect('DRIVER={SQL Server};SERVER='+server+';DATABASE='+database+';UID='+username+';PWD='+ password)

but getting following error :

pyodbc.Error: ('01000', "[01000] [unixODBC][Driver Manager]Can't open lib 'SQL Server' : file not found (0) (SQLDriverConnect)")

I used following commands to install pyodbc :

sudo apt-get install python-pyodbc
sudo apt-get install unixodbc-dev
pip3 install pyodbc

thanx in advance

2
  • Please ref the code here:learn.microsoft.com/en-us/azure/azure-sql/database/… Commented Oct 15, 2020 at 6:56
  • I spent a good week trying all the same links and articles mentioned here trying to get my Rasberry PIs to connect to an Azure database. They don't work. I think it has something to do with the ARM architecture. So even though Rasberry PI OS is based on Debian and Microsoft supplies the drivers, they don't work. I ended up using freetds.org. Commented Oct 15, 2020 at 13:30

2 Answers 2

1

As mentioned in my comment I recently walked this path and could never get the drivers from Microsoft to work on my Rasberry PIs. I speculate it has something to do with the ARM architecture. So even though Rasberry PI OS is based on Debian and Microsoft supplies the drivers, I could never get them to work.

I ended up using FreeTDS

First, install using the following commands:

sudo apt-get install freetds-dev freetds-bin unixodbc-dev tdsodbc
sudo pip install pyodbc sqlalchemy

When that is done, open the following file in a text editor:

/etc/odbcinst.ini

Add the following:

[FreeTDS]
Description=FreeTDS Driver
Driver=/usr/lib/arm-linux-gnueabihf/odbc/libtdsodbc.so
Setup=/usr/lib/arm-linux-gnueabihf/odbc/libtdsS.so

Then in the python connection string use FreeTDS as the driver and add ;TDS_Version=8.0 to the end of the connection string:

server = 'servername.database.windows.net'
database = 'database'
username = 'user'
password = 'password'
driver = 'FreeTDS'

pyodbc.connect('DRIVER='+driver+';SERVER='+server+';PORT=1433;DATABASE='+database+';UID='+username+';PWD='+ password + ';TDS_Version=8.0')
Sign up to request clarification or add additional context in comments.

3 Comments

hello, I seemed to get further, thanx. But now I get following error : pyodbc.OperationalError: ('08001', '[08001] [unixODBC][FreeTDS][SQL Server]Unable to connect to data source (0) (SQLDriverConnect)') What would cause this error?
@StevenNoppe I remember now that you have to add ";TDS_Version=8.0" to the end of the connection string. Try that, I'll go double check mine and update the answer here.
I think I found the problem : I forget to add to which port I needed to connect!
0

Please ref the pyodbc here: Quickstart: Use Python to query a database in Azure SQL Database or Azure SQL Managed Instance

import pyodbc
server = '<server>.database.windows.net'
database = '<database>'
username = '<username>'
password = '<password>'   
driver= '{ODBC Driver 17 for SQL Server}'

with pyodbc.connect('DRIVER='+driver+';SERVER='+server+';PORT=1433;DATABASE='+database+';UID='+username+';PWD='+ password) as conn:
    with conn.cursor() as cursor:
        cursor.execute("SELECT TOP 20 pc.Name as CategoryName, p.name as ProductName FROM [SalesLT].[ProductCategory] pc JOIN [SalesLT].[Product] p ON pc.productcategoryid = p.productcategoryid")
        row = cursor.fetchone()
        while row:
            print (str(row[0]) + " " + str(row[1]))
            row = cursor.fetchone()

You didn't set the ODBC driver version and the server don't have prefix tcp: .

6 Comments

still doesn't work :( : Can't open lib 'ODBC Driver 17 for SQL Server' : file not found (0) (SQLDriverConnect)")
@StevenNoppe Did you install it? please see: learn.microsoft.com/en-us/sql/connect/python/pyodbc/…
yes, but I get an error : "Unable to locate package msodbcsql17" on this line : sudo ACCEPT_EULA=Y apt-get install msodbcsql17
@StevenNoppe, can you please test also test ODBC 17 and 13? Download here: learn.microsoft.com/en-us/sql/connect/odbc/linux-mac/…
If you installed the v17 msodbcsql package that was briefly available, you should remove it before installing the msodbcsql17 package. This will avoid conflicts. The msodbcsql17 package can be installed side by side with the msodbcsql v13 package.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.