python 版本:3.6 win32 版本(因為一些特殊原因必須使用3.6) pymssql 版本:2.2.0 連接資料庫: import pymssql** def InitMssql(self): try: host = self.IniConfig.get('default','dbhost ...
python 版本:3.6 win32 版本(因為一些特殊原因必須使用3.6)
pymssql 版本:2.2.0
連接資料庫:
import pymssql
**
def InitMssql(self): try: host = self.IniConfig.get('default','dbhost',"host***") user= self.IniConfig.get('default','dbuser',"dbs***") password = self.IniConfig.get('default','dbpassword',"pwd***") database = self.IniConfig.get('default','dbdatabase',"db***") #self.DBconn = pymssql.connect(host,user,password,database,charset="GBK") self.DBconn = pymssql.connect(host,user,password,database) self.DBcursor = self.DBconn.cursor() sql = "SET TEXTSIZE 2147483647" self.DBcursor.execute(sql) self.DBconn.commit() except Exception as ex: if self.DBconn: self.DBconn.rollback() raise ex
報錯信息如下:
2023-11-19 22:27:06,374 - (20002, b'DB-Lib error message 20002, severity 9:\n
Adaptive Server connection failed (www.***.com)\n
DB-Lib error message 20002, severity 9:\nAdaptive Server connection failed (www.***.com)\n') Traceback (most recent call last): File "src\\pymssql\\_pymssql.pyx", line 647, in pymssql._pymssql.connect File "src\\pymssql\\_mssql.pyx", line 2109, in pymssql._mssql.connect File "src\\pymssql\\_mssql.pyx", line 701, in pymssql._mssql.MSSQLConnection.__init__ File "src\\pymssql\\_mssql.pyx", line 1818, in pymssql._mssql.maybe_raise_MSSQLDatabaseException File "src\\pymssql\\_mssql.pyx", line 1835, in pymssql._mssql.raise_MSSQLDatabaseException pymssql._mssql.MSSQLDatabaseException: (20002, b'DB-Lib error message 20002, severity 9:\n
Adaptive Server connection failed (www.*****.com)\n
DB-Lib error message 20002, severity 9:\n
Adaptive Server connection failed (www.*****.com)\n') During handling of the above exception, another exception occurred:
嘗試解決1:加charset參數,失敗
在網上搜相關信息,發現信息量很少,偶爾提到的解決方案都是加 charset參數。
譬如:
1、DB-Lib error message 20002, severity 9 - chungehpu - 博客園 (cnblogs.com) 加連接參數 charset="CP936"
2、DB-Lib error message 20002, severity 9_pymssql._pymssql.operationalerror: (20002, b'db-li-CSDN博客 也是加連接參數 charset="CP936"
3、還有github上SQL Server database encoding (GBK) is not consistent with python · Issue #820 · pymssql/pymssql · GitHub 加連接參數 charset="GBK"
嘗試加連接參數,再次報同樣的錯誤。
嘗試解決2:切換到python3.4,成功。
切換到python3.4,不加charset寫法,結果成功。
最終解決方案:python3.6,安裝pymssql低版本(pymssql-2.1.5-cp36-cp36m-win32.whl),成功
到 Archived: Python Extension Packages for Windows - Christoph Gohlke (uci.edu) 找到pymssql 2.1.5 ,卸載原有的高版本,手工安裝whl。
成功!