先在数据库中查询字符编码:
show VARIABLES like ‘char%’;
结果如下:
±-------------------------±---------------------------------------+
| Variable_name | Value |
±-------------------------±---------------------------------------+
| character_set_client | utf8 |
| character_set_connection | utf8 |
| character_set_database | utf8 |
| character_set_filesystem | binary |
| character_set_results | utf8 |
| character_set_server | latin1 |
| character_set_system | utf8 |
| character_sets_dir | J:\mysql-5.7.16-winx64\share\charsets\ |
±-------------------------±---------------------------------------+
发现character_set_server这是个叛徒!原因是MySQL的默认编码是拉丁,所以打开mysql的安装目录,找到my.ini,在[mysqld]最后下面加上
character_set_server=utf8
init_connect=‘SET NAMES utf8’
之后重启MySQL。
再查看一下编码,结果变为:
±-------------------------±---------------------------------------+
| Variable_name | Value |
±-------------------------±---------------------------------------+
| character_set_client | utf8 |
| character_set_connection | utf8 |
| character_set_database | utf8 |
| character_set_filesystem | binary |
| character_set_results | utf8 |
| character_set_server | utf8 |
| character_set_system | utf8 |
| character_sets_dir | J:\mysql-5.7.16-winx64\share\charsets\ |
之后在插入数据正常了。