MSSQL django_pyodbc连接问题

时间:2018-09-25 14:52:11

标签: python django pyodbc django-pyodbc

我正在尝试借助django-pyodbc连接到MSSQL。我已经安装了所有必需的软件包,例如FreeTDS,unixODBC和django-pyodbc。当我使用tsql和isql连接时,我可以连接:-

>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> 

但是,当我尝试从python连接时,它不起作用。我遇到以下错误:-

>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)')
>>>

我已经检查了所有其他相关答案,但是没有任何反应。不确定要查找哪个数据源名称。

以下是所有相关配置:-

/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

我已经浪费了几天尝试解决此问题,但是没有运气。

请帮帮我。

编辑

/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 个答案:

答案 0 :(得分:1)

尝试:

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

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

TDS_Version=7.2是关键。

我已经开始使用MSODBC驱动程序代替FreeTDS和Django。如果您有兴趣浏览,请see here

答案 1 :(得分:1)

当unixODBC在连接字符串中看到DSN=GB0015APP09.dir.slb.com时,它将在odbc.ini文件(由odbcinst -j列出)中查找以[GB0015APP09.dir.slb.com]开头的条目。它找不到一个,因此会引发您引用的“未找到数据源名称...”错误。

如果要在连接字符串(“无DSN的连接”)中提供服务器名称,则需要将DSN=GB0015APP09.dir.slb.com更改为SERVER=GB0015APP09.dir.slb.com。您还需要在连接字符串中提供其他必要的连接属性(DRIVER=...;DATABASE=...)。注意@FlipperPA的答案中提到的TDS_Version=7.2属性;它可以避免因FreeTDS默认使用旧版4.2版本的TDS协议而引起的头痛。