用了下面几个措施,保证MySQL的输出没有乱码:
1 Python文件设置编码 utf-8 (文件前面加上 #encoding=utf-8)
2 MySQL数据库charset=utf-8
1 Python文件设置编码 utf-8 (文件前面加上 #encoding=utf-8)
2 MySQL数据库charset=utf-8
3 Python连接MySQL是加上参数 charset=utf8
create database test default character set utf8;
4 设置Python的默认编码为 utf-8 (sys.setdefaultencoding(utf-8)
conn= MySQLdb.connect("localhost","root","mysql","test",charset="utf8")
连接数据库时,切记utf8而不是utf-8,否者会有如下异常:
Can't initialize character set utf-8 (path: /usr/local/mysql/share/charsets/)