Python用pymysql连接Mysql报错 'Connection' object has no attribute 'set_character_set'
原连接代码:
self.dbc = pymysql.connect('localhost', 'root', '111111', 'my_testsql')
修改为:
self.dbc = pymysql.connect('localhost', 'root', '111111', 'my_testsql',charset='utf8')
即可。
如果使用mysqldb,则采用下面声明方法
db.set_character_set('utf8')
dbc.execute('SET NAMES utf8;') dbc.execute('SET CHARACTER SET utf8;')
dbc.execute('SET character_set_connection=utf8;')
意思就是MySQLdb正常情况下会尝试将所有的内容转为latin1字符集处理"db" is the result of MySQLdb.connect, and "dbc" is the result of db.cursor().
所以处理方法就是,设置连接和游标的charset为你所希望的编码,如utf8
db是connection连接,dbc是数据库游标
本文介绍了如何解决在使用Python的pymysql模块连接MySQL时遇到的'Connection' object has no attribute 'set_character_set'错误。通过正确设置连接的字符集参数,可以避免因字符集不匹配导致的数据乱码问题。

1447

被折叠的 条评论
为什么被折叠?



