2

We are having an error when launching the tsql/isql in my python script. And I follow the solutions from the other answers but I couldn't debug it.

Here's the error:

Traceback (most recent call last):
  File "test.py", line 10, in <module>
    cnxn = pyodbc.connect('DRIVER=FreeTDS;SERVER='+server+';DATABASE='+database+';UID='+username+';PWD='+ password+';TDS_Version=4.2;')
pyodbc.OperationalError: ('08001', u'[08001] [FreeTDS][SQL Server]Unable to connect to data source (0) (SQLDriverConnect)')

Here's the script:

import pyodbc 

server = 'shared-sql-ubu.xxx.com' 
database = 'db' 
username = 'root' 
password = 'root' 
print('DRIVER=FreeTDS;SERVER='+server+';DATABASE='+database+';UID='+username+';PWD='+ password+';TDS_Version=4.2;')
cnxn = pyodbc.connect('DRIVER=FreeTDS;SERVER='+server+';DATABASE='+database+';UID='+username+';PWD='+ password+';TDS_Version=4.2;')
cursor = cnxn.cursor()

With this command:

$ tsql -C v

In terminal prints:

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

In /etc/odbc.ini

[sqlserver]
Driver = /usr/lib/x86_64-linux-gnu/odbc/libtdsodbc.so
Description = ODBC to SQLserver via FreeTDS
Trace = No
Servername = sqlserver
Database = db
TDS_Version = 4.2

In /etc/odbcinst.ini

[FreeTDS]
Description = FreeTDS Driver
Driver = /usr/lib/x86_64-linux-gnu/odbc/libtdsodbc.so
Setup = /usr/lib/x86_64-linux-gnu/odbc/libtdsodbc.so
client charset = utf-8

In /etc/freetds/freetds.conf

[Global]
TDS_Version = 4.2
client charset = UTF-8

[sqlserver]
host=shared-sql-ubu
port=1433
tds version = 4.2

Any help will be appreciated, Thanks!

=======================================

Problem solved:

New version of /etc/odbc.ini

[sqlserver]
Driver=/usr/lib/x86_64-linux-gnu/odbc/libtdsodbc.so
Description=ODBC to SQLserver via FreeTDS
Trace=No
Server=tcp:172.0.0.0 # the id of your sqlserver
#Database=master
#TDS_Version=4.2

New version of /etc/odbcinst.ini

[FreeTDS]
Description=FreeTDS Driver
Driver=/usr/lib/x86_64-linux-gnu/odbc/libtdsodbc.so
#Setup=/usr/lib/x86_64-linux-gnu/odbc/libtdsodbc.so
#client charset=utf-8
UsageCount=1

Note that some lines are commented because they are not really doing anything!

swanf
  • 79
  • 7
  • 1
    Problem solved, I followed this answer https://stackoverflow.com/a/52696069/8819898 and replaced the Servername=sqlserver in odbc.ini to Server=tcp: and this answer https://stackoverflow.com/a/13920258/8819898 to add 2 env var ODBCINI and ODBCSYSINI.Then it works! New version of the odbc.ini and odbcinst.ini is appended in the question description, hope it helps! – swanf Jul 22 '20 at 14:55

0 Answers0