MSSQL django_pyodbc connectivity issues

391 views Asked by At

I am trying to connect to MSSQL with help of django-pyodbc. I have installed all the required packages like FreeTDS, unixODBC and django-pyodbc. When I connect using tsql and isql I am able to connect:-

>tsql -S mssql -U ********* -P ***********
locale is "en_US.UTF-8"
locale charset is "UTF-8"
using default charset "UTF-8"
1> 

isql -v mssql ********* ***********
+---------------------------------------+
| Connected!                            |
|                                       |
| sql-statement                         |
| help [tablename]                      |
| quit                                  |
|                                       |
+---------------------------------------+
SQL> 

But, when I am trying to connect from python it does not work. I am getting below error:-

>python
Python 2.7.14 (default, May 16 2018, 06:48:40)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-28)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import pyodbc;
>>> print(pyodbc.connect("DSN=GB0015APP09.dir.dbs.com;UID=*********;PWD=*************").cursor().execute("select 1"));
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 have checked all other related answers but nothing is working. Not sure which data source name it is looking for.

Below are all the related configurations:-

/root>cat .freetds.conf
[mssql]
host = my_server_name
instance = MSSQL
Port = 1433
tds version =

/root>cat /etc/odbc.ini
[ServerDSN]
Driver = FreeTDS
Description = FreeTDS
Trace = No
Servername = mssql
Server = my_server_name
Port = 1433
Database = my_db_name


/root>cat /etc/odbcinst.ini
[FreeTDS]
Description = FreeTDS
Driver = /usr/lib64/libtdsodbc.so
Setup = /usr/lib64/libtdsS.so
fileusage=1
dontdlclose=1
UsageCount=1

I have wasted couple of days trying to resolve this issue, but no luck.

Please help me out.

EDIT

/root>tsql -C
Compile-time settings (established with the "configure" script)
                            Version: freetds v0.95.81
             freetds.conf directory: /etc
     MS db-lib source compatibility: yes
        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

/root>odbcinst -j
unixODBC 2.3.1
DRIVERS............: /etc/odbcinst.ini
SYSTEM DATA SOURCES: /etc/odbc.ini
FILE DATA SOURCES..: /etc/ODBCDataSources
USER DATA SOURCES..: /root/.odbc.ini
SQLULEN Size.......: 8
SQLLEN Size........: 8
SQLSETPOSIROW Size.: 8
2

There are 2 answers

2
FlipperPA On

Try:

con = pyodbc.connect("DSN=GB0015APP09.dir.slb.com;UID=**;PWD=**;TDS_Version=7.2")
cursor = con.cursor()

results = cursor.execute("SELECT 1")
print(results)

The TDS_Version=7.2 is key.

I've started using the MSODBC driver instead of FreeTDS with Django. If you're interested in a walk through, see here.

1
Gord Thompson On

When unixODBC sees DSN=GB0015APP09.dir.slb.com in the connection string it goes looking in the odbc.ini files (listed by odbcinst -j) for an entry that begins with [GB0015APP09.dir.slb.com]. It doesn't find one, so it throws the "Data source name not found ..." error you cited.

If you want to supply the server name in your connection string (a "DSN-less connection") you need to change DSN=GB0015APP09.dir.slb.com to SERVER=GB0015APP09.dir.slb.com. You'll also need to supply the other essential connection attributes (DRIVER=...;DATABASE=...) in your connection string as well. Note the TDS_Version=7.2 attribute mentioned in the answer from @FlipperPA; it can avoid headaches associated with FreeTDS defaulting to the old 4.2 version of the TDS protocol.