Search code examples
sql-serverredhatpyodbcfreetdsunixodbc

pyodbc not working on RedHat 5.4. Trying to connect to ms-sql database server using unixODBC and FreeTDS?


I am facing issue while trying to access ms-sql database using pyobdc.

Here is the System config: Python 2.7.11 Pyodbc 3.0.7 RedHat 5.4 (Tikanga) 32 Bit system Microsoft SQL Server 2012 (Database server) unixODBC 2.3.0

$ tsql -C output :

Compile-time settings (established with the "configure" script)
                            Version: freetds v0.91
             freetds.conf directory: /etc
     MS db-lib source compatibility: yes
        Sybase binary compatibility: no
                      Thread safety: yes
                      iconv library: yes
                        TDS version: 5.0
                              iODBC: no
                           unixodbc: yes
              SSPI "trusted" logins: no
                           Kerberos: no

$ odbcinst -j output :

unixODBC 2.3.0
DRIVERS............: /usr/local/etc/odbcinst.ini 
SYSTEM DATA SOURCES: /usr/local/etc/odbc.ini
FILE DATA SOURCES..: /usr/local/etc/ODBCDataSources
USER DATA SOURCES..: /root/.odbc.ini
SQLULEN Size.......: 4
SQLLEN Size........: 4
SQLSETPOSIROW Size.: 2

$ cat /usr/local/etc/odbcinst.ini output :

[ms-sql]
Description=TDS connection
Driver=/usr/local/lib/libtdsodbc.so
Setup=/usr/local/lib/libtdsodbc.so
FileUsage=1
UsageCount=1

$ cat /usr/local/etc/odbc.ini output :

[sqlserverdatasource]
Driver = ms-sql
Description = ODBC connection via ms-sql
Trace = No
Server = >IP Addresss To Database server<
Port = >Port Number<
Database = >Database name<

$ cat /etc/freetds.conf output :

[sql-server]
host        = >IP Addresss To Database server<
port        = >Port Number<
tds version = 8.0

Command which is giving me error:

connection = pyodbc.connect(r'DRIVER={FreeTDS};SERVER=>IP Addresss To Database server<; PORT=>Port Number<;DATABASE=Database name;UID=Database UID;PWD=DatabasePasswd;')

Error:

Traceback (most recent call last):   File "<stdin>", line 1, in
<module> pyodbc.Error: ('IM002', '[IM002] [unixODBC][Driver
Manager]Data source name not found, and no default driver specified
(0) (SQLDriverConnect)')

I am trying to solve this problem for last 3 days. But no luck yet. So any help/suggestion would be very helpful. I have already gone through googling. Thanks in advance :)


Solution

  • Try something like this:

    freetds.conf:

    [sql-server]
        host = sql-server.host.com
        port = 1433
        tds version = 7.2
    

    odbc.ini:

    [sql-server]
        Driver = FreeTDS
        Server = sql-server.host.com
        Port = 1433
        TDS_Version = 7.2
    

    odbcinst.ini:

    [FreeTDS]
        Description = v0.91 with protocol v7.2
        Driver = /usr/local/lib/libtdsodbc.so
    

    In Python:

    connection = pyodbc.connect(r'DRIVER={FreeTDS};SERVER=sql-server.host.com;PORT=1433;DATABASE=Database name;UID=Database Username;PWD=DatabasePasswd;TDS_Version=7.2')
    

    TDS Version 8.0 does not exist. 7.2 is the highest supported in FreeTDS 0.91. See here to explain the confusion: http://www.freetds.org/userguide/choosingtdsprotocol.htm

    If you're still having issues, try testing with tsql and isql to test the FreeTDS and unixODBC layers of the connection stack respectively. Good luck!