What's causing 'unable to connect to data source' for pyodbc?

前端 未结 13 2117
北荒
北荒 2020-12-28 14:34

I\'m trying to connect to an MSSQL database from python on Linux (SLES).

I have installed pyodbc and Free TDS. From the command line:

tsql -H server          


        
相关标签:
13条回答
  • 2020-12-28 15:20

    I had the same problem and I found out that it was missing the TDS_Version parameter in the call to connect(). The following code works for me to connect to an instance of MS SQL Server 2008:

    import pyodbc
    
    driver = '/opt/local/lib/libtdsodbc.so' # Change this to where FreeTDS installed the driver libaray!
    
    conn = pyodbc.connect(
        driver = driver,
        TDS_Version = '7.2', # Use for
        server = '<hostname or ip address>',
        port = 1433,
        database = '<database>',
        uid = '<uid>',
        pwd = '<pwd>')
    
    0 讨论(0)
  • 2020-12-28 15:20

    One setting is enoug, /etc/odbcinst.ini:

    [FreeTDS]
    Description = FreeTDS Driver to MsSQL
    Driver = /usr/lib/x86_64-linux-gnu/odbc/libtdsodbc.so
    UsageCount = 1
    

    And next:

    connection = pyodbc.connect(
        'DRIVER=FreeTDS;'
        'SERVER=<host_name_or_ip>;'
        'PORT=1433;'
        'DATABASE=<database>;'
        'UID=<username>;'
        'PWD=<password>;'
        'TDS_VERSION=8.0;'
    )
    
    0 讨论(0)
  • 2020-12-28 15:23

    Just for an extra datapoint, odbc.ini is empty on my host, and odbcinst.ini has the following lines:

    # Driver from FreeTDS
    #
    [FreeTDS]
    Driver = /usr/lib64/libtdsodbc.so.0
    

    last, the freetds.conf file has these lines:

    [global]
        host= <hostname>
        port= <mssql port>
        tds version = 8.0
    

    While one can certainly specify option settings in odbc.ini, doing it this way allows the configuration options to all be managed where you'd expect them -- the freetds.conf file.

    0 讨论(0)
  • 2020-12-28 15:23

    This worked for me, not sure but thought that it might help someone

    run below command to find which version of odbcinst and isql you are using

     which odbcinst
    
     which isql
    

    Then run $ odbcinst -j to find which odbc.ini and odbcinst.ini is getting used.

    In odbcinst.ini add

    [FreeTDS]
    Description=FreeTDS Driver for Linux & MSSQL
    Driver=/usr/local/lib/libtdsodbc.so
    Setup=/usr/local/lib/libtdsodbc.so
    UsageCount=1
    

    And in odbc.ini configure your server as

    [YOUR_SERVER]
    Driver = FreeTDS
    Servername = <YOUR_MACHINE_NAME>
    Database = <Database_You_Want_To_Connect>
    

    I found some good description at https://docs.snowflake.net/manuals/user-guide/odbc-linux.html#unixodbc

    Also take a look at https://github.com/lionheart/django-pyodbc/wiki/Mac-setup-to-connect-to-a-MS-SQL-Server

    0 讨论(0)
  • 2020-12-28 15:26

    Adding TDS_Version to the connection string worked for me:

    connection_string = 'DRIVER={{FreeTDS}};SERVER={server};PORT=1433;DATABASE={database};UID={uid};PWD={pwd};TDS_VERSION=8.0'

    0 讨论(0)
  • 2020-12-28 15:36

    After hours of going in circles it turns out all I was missing was

    TDS_Version = 8.0 in the DSN in my odbc.ini file.

    I had specified it elsewhere, but it needed to be here, too, apparently.

    Hope this helps some other poor soul.

    0 讨论(0)
提交回复
热议问题