1、创建utf8的数据库
create database `exam` default character set utf8 collate utf8_general_ci;
2、use exam;然后创建表
create table a (id int(11), name varchar(20));
3、插入中文数据,会显示报错:
insert into a values(1,'朝阳');
ERROR 1366 (HY000): Incorrect string value: '\xB3\xAF\xD1\xF4' for column 'name' at row 1
4、查看数据库的字符集
show variables like 'char%';
+--------------------------+---------------------------------------------------------+
| Variable_name | Value |
+--------------------------+---------------------------------------------------------+
| character_set_client | utf8mb4 |
| character_set_connection | utf8mb4 |
| character_set_database | utf8 |
| character_set_filesystem | binary |
| character_set_results | utf8mb4 |
| character_set_server | utf8 |
| character_set_system | utf8 |
| character_sets_dir | C:\Program Files\MySQL\MySQL Server 5.7\share\charsets\ |
+--------------------------+---------------------------------------------------------+
5、修改MySQL字符集
set character_set_client=gbk;
set character_set_connection=gbk;
set character_set_results=gbk;
6、再次插入,发现问题已经解决!
insert into a values(1,'朝阳');