admin管理员组

文章数量:1794759

python3.7连接sqlserver数据库失败报错20002, DB

python3.7连接sqlserver数据库失败报错20002, DB

python3.7连接sqlserver数据库失败报错后解决方法

1、连接数据库脚本

这是我连接sqlserver和mysql的代码;数据库连接信是调用config.ini文件的信;调用config.ini文件的信的脚本不在这描述了

def get_conn(self): try: if self.sqltype == 'SqlServer': conn = pymssql.connect(host=self.host, port=self.port, user=self.user, password=self.passwd, database=self.database, charset=self.charset,autocommit=True) else: conn = pymysql.connect(host=self.host, port=self.port, user=self.user, password=self.passwd, database=self.database, charset=self.charset,autocommit=True) print('数据库已连接!') return conn except Exception as e: print('%s', e)

config.ini配置文件里数据库信样式如下: [数据库名称xxxx] host = xxxx port = xxxx user = xxxx passwd = xxxx db = xxxx charset = xxxx sqltype = SqlServer(可自行标记,为了区分连接哪种数据库的标志)

或者 [数据库名称xxxx] host = xxxx port = xxxx user = xxxx passwd = xxxx db = xxxx charset = xxxx sqltype = MySql(可自行标记,为了区分连接哪种数据库的标志)

2、连接sqlserver数据库失败,报错信如下:

%s (20002, b’DB-Lib error message 20002, severity 9:\\nAdaptive Server connection failed (ip:port)\\n’)

出现问题的原因:charset未配置或者配置错误,都会导致数据库连接失败,报上面的问题; 解决问题的方案:在 connect代码里设置连接数据库正确的charset,就可以连接上了;我连接数据库时charset=utf8报错了,然后将charset=CP936或者GBK(注意大小写)就能正常连接了

sqlserver数据库查询charset名称语句:select SERVERPROPERTY('Sqlcharsetname') MySql数据库查询charset语句:show variables like '%character%',请参考该大神的博客:blog.csdn/huzecom/article/details/100089325?utm_medium=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-1.nonecase&depth_1-utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-1.nonecase

本文标签: 报错数据库sqlserverDB