pymssql 报20002错误解决办法:DB-Lib error message 20002, severity 9

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。

成功!

热门相关:国破山河在   苍穹龙骑   至尊龙帝陆鸣   龙骑战机   你好,墨先生