欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

ERROR 1366 (HY000): Incorrect string value: '\xE9\x83\x91\xE5\xB7\x9E' for column 'aa' at row 1 MySQL 字符集

程序员文章站 2022-11-07 07:59:15
ERROR 1366 (HY000): Incorrect string value: '\xE9\x83\x91\xE5\xB7\x9E' for column 'aa' at row 1创建表之后不能插入中文字符?为啥呢?了解字符集的重要性。它必须在建库之前要确定好,恢复备份时也需要注意 mys ......

error 1366 (hy000): incorrect string value: '\xe9\x83\x91\xe5\xb7\x9e' for column 'aa' at row 1
创建表之后不能插入中文字符?为啥呢?
了解字符集的重要性。它必须在建库之前要确定好,恢复备份时也需要注意

mysql> use test
database changed
mysql> create table zgy(aa char(20));
query ok, 0 rows affected (0.01 sec)

mysql> insert into zgy values('zgzg');
query ok, 1 row affected (0.01 sec)

mysql> insert into zgy values('郑州');
error 1366 (hy000): incorrect string value: '\xe9\x83\x91\xe5\xb7\x9e' for column 'aa' at row 1
mysql> show variables like '%char%';
+--------------------------+----------------------------------+
| variable_name | value |
+--------------------------+----------------------------------+
| character_set_client | utf8 |
| character_set_connection | utf8 |
| character_set_database | latin1 |
| character_set_filesystem | binary |
| character_set_results | utf8 |
| character_set_server | latin1 |
| character_set_system | utf8 |
| character_sets_dir | /usr/local/mysql/share/charsets/ |
+--------------------------+----------------------------------+
8 rows in set (0.00 sec)

这部分已执行

set character_set_client=utf8;
set character_set_connection=utf8;
set character_set_database=utf8;
set character_set_results=utf8;
set character_set_server=utf8;
set character_set_system=utf8;
set collation_connection=utf8;
set collation_database=utf8;
set collation_server=utf8;
set character_set_server=utf8;

mysql> 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 | utf8 |
| character_set_system | utf8 |
| character_sets_dir | /usr/local/mysql/share/charsets/ |
+--------------------------+----------------------------------+
8 rows in set (0.00 sec)

仍然报错?
mysql> insert into zgy values('郑州');
error 1366 (hy000): incorrect string value: '\xe9\x83\x91\xe5\xb7\x9e' for column 'aa' at row 1

这时候只是将db的改成utf8了,而zgy表是之前创建的

mysql> alter database test character set utf8;
query ok, 1 row affected (0.00 sec)

mysql> show create database test;
+----------+---------------------------------------------------------------+
| database | create database |
+----------+---------------------------------------------------------------+
| test | create database `test` /*!40100 default character set utf8 */ |
+----------+---------------------------------------------------------------+
1 row in set (0.00 sec)

mysql> show create table zgy;
+-------+------------------------------------------------------------------------------------------+
| table | create table |
+-------+------------------------------------------------------------------------------------------+
| zgy | create table `zgy` (
`aa` char(20) default null
) engine=innodb default charset=latin1 |
+-------+------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

改变表的字符集,发现还不行
mysql> alter table zgy character set utf8;
query ok, 0 rows affected (0.00 sec)
records: 0 duplicates: 0 warnings: 0

mysql> insert into zgy values('郑州');
error 1366 (hy000): incorrect string value: '\xe9\x83\x91\xe5\xb7\x9e' for column 'aa' at row 1
mysql>
mysql> show create table zgy;
+-------+-------------------------------------------------------------------------------------------------------------+
| table | create table |
+-------+-------------------------------------------------------------------------------------------------------------+
| zgy | create table `zgy` (
`aa` char(20) character set latin1 default null
) engine=innodb default charset=utf8 |
+-------+-------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

修改字段的字符集
创建库时默认已经指定了表和字段等对象的字符集
mysql> alter table zgy aa char(20) character set utf8;
error 1064 (42000): you have an error in your sql syntax; check the manual that corresponds to your mysql server version for the right syntax to use near 'aa char(20) character set utf8' at line 1
mysql> alter table zgy change aa aa char(20) character set utf8;
query ok, 2 rows affected (0.02 sec)
records: 2 duplicates: 0 warnings: 0

mysql> show create table zgy;
+-------+----------------------------------------------------------------------------------------+
| table | create table |
+-------+----------------------------------------------------------------------------------------+
| zgy | create table `zgy` (
`aa` char(20) default null
) engine=innodb default charset=utf8 |
+-------+----------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

mysql> insert into zgy values('郑州');
query ok, 1 row affected (0.01 sec)

mysql> insert into zgy values('深圳');
query ok, 1 row affected (0.00 sec)

mysql> select * from zgy;
+--------------+
| aa |
+--------------+
| zgzg |
| asasas121212 |
| 郑州 |
| 深圳 |
+--------------+
4 rows in set (0.00 sec)

小常识:
看到是gbk和latin1说明不支持中文,所以需要修改字符类型
在latin1中,每个字符只有一个字节长。在utf8中,一个字符可以由多个字节组成。因此,utf8具有比latin1更多的字符(并且它们具有的字符不一定由相同的字节/字节序列表示)。但是我们的中文汉字是要2个字节还是3个字节哦,所有latin1就不能存储汉字啦。

优点:一劳永逸,在创建数据库时就设置为utf8格式,以后再次数据库下创建表时以及添加表数据时都不用担心中文字符问题。
create database testdb default character set utf8;

一些关于查看和修改字符集的mysql知识:
查看mysql的字符集:show variables where variable_name like '%char%';
查看某一个数据库字符集:show create database enterprises;(注:enterprises为数据库)
查看某一个数据表字符集:show create table employees;(注:employees为数据表)

修改mysql的字符集
set character_set_client=utf8;
set character_set_connection=utf8;
set character_set_database=utf8;
set character_set_results=utf8;
set character_set_server=utf8;
set character_set_system=utf8;
set collation_connection=utf8;
set collation_database=utf8;
set collation_server=utf8;

修改数据库enterprises的字符集:
alter database enterprises character set utf8

修改数据表employees的字符集:
alter table employees character set utf8

修改字段的字符集
alter table employees change name name char(10) character set utf-8;