Search code examples
sql-serverlinuxpython-3.xazureazure-sql-database

Linux python3 - Can't open lib 'SQL Server'


I am trying to connect to an Microsoft Azure SQL server database.

This is how i am trying to connect:

 conn = pyodbc.connect('DRIVER={SQL Server};SERVER=%s' % (self.config.get("Sql", "DataSource")),
                        user= self.config.get("Sql", "UserId"),
                        password=self.config.get("Sql", "Password"),
                        database=self.config.get("Sql", "Catalog"))

I am getting an error while excuting this line. The error:

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

Can't figure why this is happening, Any idea?


Solution

  • Update - December 2022

    The current installation instructions for the ODBC driver are here


    I also recommend you install the ODBC Driver and then try to use pyodbc. I am assuming you are on an Ubuntu 15.04+ machine.

    To install the ODBC Driver follow the following instructions:

    sudo su
    wget https://gallery.technet.microsoft.com/ODBC-Driver-13-for-Ubuntu-b87369f0/file/154097/2/installodbc.sh
    sh installodbc.sh
    

    Once you do that, install pyodbc using pip and try the following script:

    import pyodbc
    server = 'tcp:myserver.database.windows.net'
    database = 'mydb'
    username = 'myusername'
    password = 'mypassword'
    cnxn = pyodbc.connect('DRIVER={ODBC Driver 13 for SQL Server};SERVER='+server+';DATABASE='+database+';UID='+username+';PWD='+ password)
    cursor = cnxn.cursor()
    cursor.execute("SELECT @@version;")
    row = cursor.fetchone()
    while row:
        print row
        row = cursor.fetchone()
    

    Let me know how that goes.

    Cheers,
    Meet