我在尝试使用pyobdc访问ms-sql数据库时遇到问题。
这是系统配置: Python 2.7.11 Pyodbc 3.0.7 RedHat 5.4(Tikanga)32位系统 Microsoft SQL Server 2012(数据库服务器) unixODBC 2.3.0
$ tsql -C
输出
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
输出
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
输出
[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
输出
[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
输出
[sql-server]
host = >IP Addresss To Database server<
port = >Port Number<
tds version = 8.0
给我错误的命令:
connection = pyodbc.connect(r'DRIVER={FreeTDS};SERVER=>IP Addresss To Database server<; PORT=>Port Number<;DATABASE=Database name;UID=Database UID;PWD=DatabasePasswd;')
错误:
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)')
我在过去3天试图解决这个问题。但还没有运气。所以任何帮助/建议都会非常有帮助。 我已经通过谷歌搜索了。 在此先感谢:)
答案 0 :(得分:1)
尝试这样的事情:
<强> 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
在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版本8.0不存在。 7.2是FreeTDS 0.91中支持的最高值。请参阅此处以解释混淆:http://www.freetds.org/userguide/choosingtdsprotocol.htm
如果您仍然遇到问题,请尝试使用tsql
和isql
进行测试,分别测试连接堆栈的FreeTDS和unixODBC层。祝你好运!