问题描述
我正在尝试通过Linux(SLES)上的python连接到MSSQL数据库。
I'm trying to connect to an MSSQL database from python on Linux (SLES).
我已经安装了pyodbc和Free TDS。从命令行:
I have installed pyodbc and Free TDS. From the command line:
tsql -H server -p 1433 -U username -P password
不过,从Python连接到服务器没有问题:
Connects to the server without a problem, however, from Python:
import pyodbc
pyodbc.connect(driver='{FreeTDS}', server='server', database='database', uid='username', pwd='password')
产生错误:
pyodbc.Error: ('08001', '[08001] [unixODBC][FreeTDS][SQL Server]Unable to connect to data source (0) (SQLDriverConnect)')
我发现这个错误毫无意义。即使是缩小范围的建议现在也将有所帮助。
I'm finding this error unhelpfully vague. Even a suggestion to narrow down the issue would be helpful right now.
编辑:
查看TDS日志转储,看起来这就像是整件事崩溃:
Looking at the TDS log dump it looks like this is where the whole thing falls apart:
token.c:328:tds_process_login_tokens()
util.c:331:tdserror(0x87bbeb8, 0x8861820, 20017, 115)
odbc.c:2270:msgno 20017 20003
util.c:361:tdserror: client library returned TDS_INT_CANCEL(2)
util.c:384:tdserror: returning TDS_INT_CANCEL(2)
util.c:156:Changed query state from IDLE to DEAD
token.c:337:looking for login token, got 0()
token.c:122:tds_process_default_tokens() marker is 0()
token.c:125:leaving tds_process_default_tokens() connection dead
login.c:466:login packet accepted
util.c:331:tdserror(0x87bbeb8, 0x8861820, 20002, 0)
odbc.c:2270:msgno 20002 20003
util.c:361:tdserror: client library returned TDS_INT_CANCEL(2)
util.c:384:tdserror: returning TDS_INT_CANCEL(2)
mem.c:615:tds_free_all_results()
error.c:412:odbc_errs_add: "Unable to connect to data source"
推荐答案
经过数小时的盘算,原来我所缺少的只是
After hours of going in circles it turns out all I was missing was
TDS_Version = 8.0
在我的odbc.ini中的DSN中文件。
TDS_Version = 8.0in the DSN in my odbc.ini file.
我已经在其他地方指定了它,但显然它也必须在这里。
I had specified it elsewhere, but it needed to be here, too, apparently.
希望这可以帮助其他一些可怜的灵魂。
Hope this helps some other poor soul.
这篇关于是什么导致pyodbc“无法连接到数据源”?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!