sae python中Mysql中文乱码的解决

一開始我用的是:

db=MySQLdb.connect(db=sae.const.MYSQL_DB,user=sae.const.MYSQL_USER,passwd=sae.const.MYSQL_PASS,host=sae.const.MYSQL_HOST,port=int(sae.const.MYSQL_PORT))

数据库编码与python代码的编码都已设为utf8,在PHPMyAdmin中加入中文记录,在网页查询显示中却显示乱码。我在网上各种搜索都找不到解决方法。最后最终发如今连接数据库时加入charset參数为utf8就能够了。

db=MySQLdb.connect(db=sae.const.MYSQL_DB,user=sae.const.MYSQL_USER,passwd=sae.const.MYSQL_PASS,host=sae.const.MYSQL_HOST,port=int(sae.const.MYSQL_PORT),charset='utf8')


原文地址:https://www.cnblogs.com/yxysuanfa/p/7300405.html