Python 插入MySQL 提示错误 SQL Error: 1366: Incorrect string value: "\xE8

在以下操作后仍然提示这个错误:
1.Python文件头添加# -*- coding: utf-8 -*-
2.添加·

import sys
reload(sys)
sys.setdefaultencoding('utf-8')

3.MySQL的数据库和表都改成utf8mb4
4.MySQL的配置文件添加

[client]
default-character-set=utf8
[mysqld]
character-set-server=utf8
collation-server=utf8_general_ci 

最后在这篇博客的提示下,查看所有字段的字符集设置,发现有部分字段的字符集是latin1_swidish,造成不能插入中文。
查看所有字段的编码
show full columns from wow
修改字段编码和排序字符集
alter table wow change up up varchar(100) character set utf8mb4 collate utf8mb4_general_ci not null default '';

mysql> show full columns from wow
    -> ;
+------------+--------------+--------------------+------+-----+---------+---------------------------
| Field      | Type         | Collation          | Null | Key | Default | Extra
+------------+--------------+--------------------+------+-----+---------+---------------------------
| id         | int(11)      | NULL               | NO   | PRI | NULL    | auto_increment
| sname      | varchar(255) | utf8mb4_general_ci | YES  |     | NULL    |
| up         | tinytext     | latin1_swedish_ci  | YES  |     | NULL    |
| stype      | tinytext     | latin1_swedish_ci  | YES  |     | NULL    |
| sload      | tinytext     | latin1_swedish_ci  | YES  |     | NULL    |
| queue      | tinytext     | latin1_swedish_ci  | YES  |     | NULL    |
| updatetime | timestamp    | NULL               | YES  |     | NULL    | on update CURRENT_TIMESTAM
+------------+--------------+--------------------+------+-----+---------+---------------------------
7 rows in set (0.00 sec)

mysql> alter table wow change up up varchar(100) character set utf8mb4 collate utf8mb4_general_ci not null default '';
Query OK, 5 rows affected (0.10 sec)
Records: 5  Duplicates: 0  Warnings: 0
添加新评论