簡體   English   中英

無法連接到數據庫:Pyodbc

[英]Unable to connect to database : Pyodbc

我正在嘗試從Linux上的python連接到MSSQL數據庫。 每當我嘗試通過python連接到數據庫時。 我收到以下錯誤:

>>> import pyodbc 
>>> connection=pyodbc.connect('DRIVER={FreeTDS};SERVER=xx.xx.xxx.xxx;DATABASE=xxxxxxxxx;UID=xxxxx;PWD=xxxxxxx;')

Traceback (most recent call last):
   File "<stdin>", line 1, in <module>
pyodbc.Error: ('08001', '[08001] [unixODBC][FreeTDS][SQL Server]Unable to connect to data source (0) (SQLDriverConnect)')

這是freetds.log文件的輸出:

log.c:196:Starting log file for FreeTDS 0.91
on 2013-05-27 15:11:50 with debug flags 0x4fff.
iconv.c:330:tds_iconv_open(0x89fda68, ISO-8859-1)
iconv.c:187:local name for ISO-8859-1 is ISO-8859-1
iconv.c:187:local name for UTF-8 is UTF-8
iconv.c:187:local name for UCS-2LE is UCS-2LE
iconv.c:187:local name for UCS-2BE is UCS-2BE
iconv.c:349:setting up conversions for client charset "ISO-8859-1"
iconv.c:351:preparing iconv for "ISO-8859-1" <-> "UCS-2LE" conversion
iconv.c:391:preparing iconv for "ISO-8859-1" <-> "ISO-8859-1" conversion
iconv.c:394:tds_iconv_open: done
login.c:436:invalid port number
mem.c:615:tds_free_all_results()
error.c:412:odbc_errs_add: "Unable to connect to data source"
error.c:585:SQLError((nil), 0x8a744b0, (nil), 0xbfd23d93, 0xbfd23978, 0xbfd23d99, 513, 0xbfd23972)
error.c:517:SQLGetDiagRec(2, 0x8a744b0, 1, 0xbfd23d93, 0xbfd23978, 0xbfd23d99, 513, 0xbfd23972)
error.c:566:SQLGetDiagRec: "[FreeTDS][SQL Server]Unable to connect to data source"
error.c:585:SQLError((nil), 0x8a744b0, (nil), 0xbfd23d93, 0xbfd23978, 0xbfd23d99, 513, 0xbfd23972)
error.c:517:SQLGetDiagRec(2, 0x8a744b0, 1, 0xbfd23d93, 0xbfd23978, 0xbfd23d99, 513, 0xbfd23972)
odbc.c:3980:SQLFreeHandle(2, 0x8a744b0)
odbc.c:4006:_SQLFreeConnect(0x8a744b0)
odbc.c:3980:SQLFreeHandle(1, 0x89fdba0)
odbc.c:4050:_SQLFreeEnv(0x89fdba0)
odbc.c:1454:SQLAllocHandle(1, (nil), 0x8a20c1c)
odbc.c:1543:_SQLAllocEnv(0x8a20c1c, 3)
locale.c:69:Attempting to read locales.conf file
odbc.c:1240:SQLSetEnvAttr(0x8a39c88, 200, 0x3, 0)
odbc.c:1279:SQLGetEnvAttr(0x8a39c88, 200, 0xbfd21778, 0, (nil))
odbc.c:1454:SQLAllocHandle(2, 0x8a39c88, 0x8a20c20)
odbc.c:1482:_SQLAllocConnect(0x8a39c88, 0x8a20c20)
odbc.c:527:SQLDriverConnect(0x8a1a200, (nil), DRIVER=   {FreeTDS};SERVER=xx.xxx.xxx.xxx;DATABASE=xxxxx;UID=xxxxxxx;PWD=xxxxxxx;, -3, 0xbfd23f6c, 2048, (nil), 0)
config.c:714:Setting 'dump_file' to '/tmp/freetds.log' from $TDSDUMP.
iconv.c:330:tds_iconv_open(0x8a58840, ISO-8859-1)
iconv.c:349:setting up conversions for client charset "ISO-8859-1"
iconv.c:351:preparing iconv for "ISO-8859-1" <-> "UCS-2LE" conversion
iconv.c:391:preparing iconv for "ISO-8859-1" <-> "ISO-8859-1" conversion
iconv.c:394:tds_iconv_open: done
login.c:436:invalid port number
mem.c:615:tds_free_all_results()
error.c:412:odbc_errs_add: "Unable to connect to data source"
error.c:585:SQLError((nil), 0x8a1a200, (nil), 0xbfd22513, 0xbfd220f0, 0xbfd22599, 513, 0xbfd220ea)
error.c:517:SQLGetDiagRec(2, 0x8a1a200, 1, 0xbfd22513, 0xbfd220f0, 0xbfd22599, 513, 0xbfd220ea)
error.c:566:SQLGetDiagRec: "[FreeTDS][SQL Server]Unable to connect to data source"
error.c:585:SQLError((nil), 0x8a1a200, (nil), 0xbfd22513, 0xbfd220f0, 0xbfd22599, 513, 0xbfd220ea)
error.c:517:SQLGetDiagRec(2, 0x8a1a200, 1, 0xbfd22513, 0xbfd220f0, 0xbfd22599, 513, 0xbfd220ea)
odbc.c:3980:SQLFreeHandle(2, 0x8a1a200)
odbc.c:4006:_SQLFreeConnect(0x8a1a200)
odbc.c:3980:SQLFreeHandle(1, 0x8a39c88)
odbc.c:4050:_SQLFreeEnv(0x8a39c88)

odbc.log文件的輸出為:

[ODBC][15328][1369647481.150055][__handles.c][450]
    Exit:[SQL_SUCCESS]
        Environment = 0x9198d88
[ODBC][15328][1369647481.150109][SQLSetEnvAttr.c][182]
    Entry:            
        Environment = 0x9198d88            
        Attribute = SQL_ATTR_ODBC_VERSION            
        Value = 0x3            
        StrLen = 4
[ODBC][15328][1369647481.150139][SQLSetEnvAttr.c][349]
    Exit:[SQL_SUCCESS]
[ODBC][15328][1369647481.150167][SQLAllocHandle.c][364]
    Entry:
        Handle Type = 2
        Input Handle = 0x9198d88
[ODBC][15328][1369647481.150194][SQLAllocHandle.c][482]
    Exit:[SQL_SUCCESS]
        Output Handle = 0x91956c0
[ODBC][15328][1369647481.150229][SQLDriverConnectW.c][286]
    Entry:            
        Connection = 0x91956c0            
        Window Hdl = (nil)            
        Str In = [DRIVER={FreeTDS};SERVER=xxxxxxxx;DATABASE=xxxxxx;UID=xxxxxxxxx;PWD=xxxxxxxx;][length = 91]            
        Str Out = (nil)            
        Str Out Max = 0            
        Str Out Ptr = (nil)            
        Completion = 0
    UNICODE Using encoding ASCII 'ISO8859-1' and UNICODE 'UCS-2LE'

[ODBC][15328][1369647481.152483][SQLDriverConnectW.c][777]
    Exit:[SQL_ERROR]
[ODBC][15328][1369647481.152523][SQLDriverConnect.c][687]
    Entry:            
        Connection = 0x91956c0            
        Window Hdl = (nil)            
        Str In = [DRIVER={FreeTDS};SERVER=xxxxxxxxxx;DATABASE=xxxxxxxxxx;UID=xxxxxx;PWD=**********;][length = 91 (SQL_NTS)]            
        Str Out = 0xbfa8336c            
        Str Out Max = 2048            
        Str Out Ptr = (nil)            
        Completion = 0
    UNICODE Using encoding ASCII 'ISO8859-1' and UNICODE 'UCS-2LE'

    DIAG [08001] [FreeTDS][SQL Server]Unable to connect to data source

[ODBC][15328][1369647481.153501][SQLDriverConnect.c][1273]
    Exit:[SQL_ERROR]
[ODBC][15328][1369647481.153536][SQLGetDiagRec.c][661]
    Entry:                
        Connection = 0x91956c0                
        Rec Number = 1                
        SQLState = 0xbfa83f46                
        Native = 0xbfa83f3c                
        Message Text = 0xbfa83f4c                
        Buffer Length = 1023                
        Text Len Ptr = 0xbfa83f3a
[ODBC][15328][1369647481.153569][SQLGetDiagRec.c][698]
    Exit:[SQL_SUCCESS]                    
        SQLState = 08001                    
        Native = 0xbfa83f3c -> 0                    
        Message Text = [[unixODBC][FreeTDS][SQL Server]Unable to connect to data source]
[ODBC][15328][1369647481.153606][SQLFreeHandle.c][279]
    Entry:
        Handle Type = 2
        Input Handle = 0x91956c0
[ODBC][15328][1369647481.153630][SQLFreeHandle.c][330]
    Exit:[SQL_SUCCESS]
[ODBC][15362][1369647710.940455][__handles.c][450]
    Exit:[SQL_SUCCESS]
        Environment = 0x8a23f08
[ODBC][15362][1369647710.940519][SQLSetEnvAttr.c][182]
    Entry:            
        Environment = 0x8a23f08            
        Attribute = SQL_ATTR_ODBC_VERSION            
        Value = 0x3            
        StrLen = 4
[ODBC][15362][1369647710.941280][SQLSetEnvAttr.c][349]
    Exit:[SQL_SUCCESS]
[ODBC][15362][1369647710.941324][SQLAllocHandle.c][364]
    Entry:
        Handle Type = 2
        Input Handle = 0x8a23f08
[ODBC][15362][1369647710.941352][SQLAllocHandle.c][482]
    Exit:[SQL_SUCCESS]
        Output Handle = 0x8a206c0
[ODBC][15362][1369647710.941390][SQLDriverConnectW.c][286]
    Entry:            
        Connection = 0x8a206c0            
        Window Hdl = (nil)            
        Str In = [DRIVER={FreeTDS};SERVER=xxxxxxx;DATABASE=xxxxxxx;UID=xxxxxx;PWD=xxxxxxx;][length = 91]            
        Str Out = (nil)            
        Str Out Max = 0            
        Str Out Ptr = (nil)            
        Completion = 0
    UNICODE Using encoding ASCII 'ISO8859-1' and UNICODE 'UCS-2LE'

[ODBC][15362][1369647710.943626][SQLDriverConnectW.c][777]
    Exit:[SQL_ERROR]
[ODBC][15362][1369647710.943668][SQLDriverConnect.c][687]
    Entry:            
        Connection = 0x8a206c0            
        Window Hdl = (nil)            
        Str In = [DRIVER={FreeTDS};SERVER=xxxxxxxx;DATABASE=xxxxxxx;UID=xxxxxx;PWD=**********;][length = 91 (SQL_NTS)]            
        Str Out = 0xbfd23f6c            
        Str Out Max = 2048            
        Str Out Ptr = (nil)            
        Completion = 0
    UNICODE Using encoding ASCII 'ISO8859-1' and UNICODE 'UCS-2LE'

    DIAG [08001] [FreeTDS][SQL Server]Unable to connect to data source

[ODBC][15362][1369647710.944642][SQLDriverConnect.c][1273]
    Exit:[SQL_ERROR]
[ODBC][15362][1369647710.944675][SQLGetDiagRec.c][661]
    Entry:                
        Connection = 0x8a206c0                
        Rec Number = 1                
        SQLState = 0xbfd24b46                
        Native = 0xbfd24b3c                
        Message Text = 0xbfd24b4c                
        Buffer Length = 1023                
        Text Len Ptr = 0xbfd24b3a
[ODBC][15362][1369647710.944706][SQLGetDiagRec.c][698]
    Exit:[SQL_SUCCESS]                    
        SQLState = 08001                    
        Native = 0xbfd24b3c -> 0                    
        Message Text = [[unixODBC][FreeTDS][SQL Server]Unable to connect to data source]
[ODBC][15362][1369647710.944744][SQLFreeHandle.c][279]
    Entry:
        Handle Type = 2
        Input Handle = 0x8a206c0
[ODBC][15362][1369647710.944766][SQLFreeHandle.c][330]
    Exit:[SQL_SUCCESS]

我的odbcinst.ini文件是:

[ODBC]
Trace = Yes
TraceFile = /tmp/odbc.log
[FreeTDS]
Description = TDS driver (Sybase/MS SQL)
Driver = /usr/lib/i386-linux-gnu/odbc/libtdsodbc.so  
Setup = /usr/lib/i386-linux-gnu/odbc/libtdsS.so      
UsageCount = 1
CPTimeout =
CPReuse =
TDS Version = 7.2
client charset = utf-8

請幫助我消除此錯誤。

您是否嘗試了以下連接字符串。 我具有TDS驅動程序版本8.0,並且以下連接字符串可在我的系統上工作:

username = "domain\\user"
connection_string = "DRIVER={FreeTDS};SERVER=xxxx;PORT=xx;DATABASE=xxxx;UID=%s;PWD=xxx;CHARSET=UTF8;TDS_Version=8.0" % username

當以某種方式連接到Microsoft SQL Server 2008時,我需要按照Tushar的建議將選項;PORT=1433添加到連接字符串中。 1433是默認端口,並且無需顯式定義到另一台服務器的端口,連接就可以正常工作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM