我正在尝试在 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