1.設置mysql的編碼爲utf8mysql
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 |
+--------------------------+-------------+sql
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' )