Python 解决Mysql 存储读取乱码问题

1.设置mysql的编码为utf8

mysql> show variables like "%char%";
+--------------------------+---------------+
| Variable_name | Value |
+--------------------------+---------------+
| character_set_client | gbk |
| character_set_connection | gbk |
| character_set_database | utf8 |
| character_set_filesystem | binary |
| character_set_results | gbk |
| character_set_server | utf8 |
| character_set_system | utf8 |
+--------------------------+-------------+

 

 

SET character_set_client='utf8';
SET character_set_connection='utf8';
SET character_set_results='utf8';

2.数据库链接时,设置编码格式为utf8

conn = MySQLdb.connect(
        host = 'localhost', #本地主机,默认为'localhost'
        port = 3306,        #端口,默认为3306
        user = 'root',      #用户名,默认为'root'
        passwd = '123456',  #密码,自设
        db ='test',         #连接名为'test'的数据库
        charset='utf8'
    )

 

posted @ 2017-11-18 15:49  123木头人啊  阅读(305)  评论(0编辑  收藏  举报