【MySQL】ERROR 1366 (HY000): Incorrect string value: '\xC0\xEE\xC7\xBF' for column 'SNAME' at row 1
今天在向MySQL数据库中插入数据失败,提示为ERROR 1366 (HY000): Incorrect string value: '\xC0\xEE\xC7\xBF' for column 'SNAME' at row 1
从MySQL官网上查看错误码信息http://dev.mysql.com/doc/refman/5.6/en/error-messages-server.html看到
Error: 1366
SQLSTATE: HY000
(ER_TRUNCATED_WRONG_VALUE_FOR_FIELD
) Message: Incorrect %s value: '%s' for column '%s' at row %ld
从当前的提示错误上看,怀疑可能应该是跟汉字字符串的编码方式有关系:(据说mysql的UTF8也解决不了中文的编码问题,用gbk才能解决。)
1、查看当前服务器状态
status或者\s
mysql> \s
--------------
mysql Ver 14.14 Distrib 5.6.19, for Win32 (x86)
Connection id: 1
Current database: test
Current user: ODBC@localhost
SSL: Not in use
Using delimiter: ;
Server version: 5.6.19 MySQL Community Server (GPL)
Protocol version: 10
Connection: localhost via TCP/IP
Server characterset: latin1
Db characterset: latin1
Client characterset: gbk
Conn. characterset: gbk
TCP port: 3306
Uptime: 4 hours 48 min 12 sec
Threads: 1 Questions: 40 Slow queries: 0 Opens: 76 Flush tables: 1 Open tables: 66 Queries per second avg: 0.002
--------------
从查询的结果上看,当前数据库为:test;Server characterset和Db characterset都是latin1,不是gbk。我们来看看latin1是什么编码方式:
我们还可以看看当前系统的字符集都有哪些,使用命令show character set;
从查询结果中找到latin1,它的描述是:cp1252 West European 看来它是西欧编码规则,不是中文的编码,因此我们这里才会乱码了。gbk,是简体中文编码。
mysql> show character set;
+----------+-----------------------------+---------------------+--------+
| Charset | Description | Default collation | Maxlen |
+----------+-----------------------------+---------------------+--------+
| big5 | Big5 Traditional Chinese | big5_chinese_ci | 2 |
| dec8 | DEC West European | dec8_swedish_ci | 1 |
| cp850 | DOS West European | cp850_general_ci | 1 |
| hp8 | HP West European | hp8_english_ci | 1 |
| koi8r | KOI8-R Relcom Russian | koi8r_general_ci | 1 |
| latin1 | cp1252 West European | latin1_swedish_ci | 1 |
| latin2 | ISO 8859-2 Central European | latin2_general_ci | 1 |
| swe7 | 7bit Swedish | swe7_swedish_ci | 1 |
| ascii | US ASCII | ascii_general_ci | 1 |
| ujis | EUC-JP Japanese | ujis_japanese_ci | 3 |
| sjis | Shift-JIS Japanese | sjis_japanese_ci | 2 |
| hebrew | ISO 8859-8 Hebrew | hebrew_general_ci | 1 |
| tis620 | TIS620 Thai | tis620_thai_ci | 1 |
| euckr | EUC-KR Korean | euckr_korean_ci | 2 |
| koi8u | KOI8-U Ukrainian | koi8u_general_ci | 1 |
| gb2312 | GB2312 Simplified Chinese | gb2312_chinese_ci | 2 |
| greek | ISO 8859-7 Greek | greek_general_ci | 1 |
| cp1250 | Windows Central European | cp1250_general_ci | 1 |
| gbk | GBK Simplified Chinese | gbk_chinese_ci | 2 |
| latin5 | ISO 8859-9 Turkish | latin5_turkish_ci | 1 |
| armscii8 | ARMSCII-8 Armenian | armscii8_general_ci | 1 |
| utf8 | UTF-8 Unicode | utf8_general_ci | 3 |
| ucs2 | UCS-2 Unicode | ucs2_general_ci | 2 |
| cp866 | DOS Russian | cp866_general_ci | 1 |
| keybcs2 | DOS Kamenicky Czech-Slovak | keybcs2_general_ci | 1 |
| macce | Mac Central European | macce_general_ci | 1 |
| macroman | Mac West European | macroman_general_ci | 1 |
| cp852 | DOS Central European | cp852_general_ci | 1 |
| latin7 | ISO 8859-13 Baltic | latin7_general_ci | 1 |
| utf8mb4 | UTF-8 Unicode | utf8mb4_general_ci | 4 |
| cp1251 | Windows Cyrillic | cp1251_general_ci | 1 |
| utf16 | UTF-16 Unicode | utf16_general_ci | 4 |
| utf16le | UTF-16LE Unicode | utf16le_general_ci | 4 |
| cp1256 | Windows Arabic | cp1256_general_ci | 1 |
| cp1257 | Windows Baltic | cp1257_general_ci | 1 |
| utf32 | UTF-32 Unicode | utf32_general_ci | 4 |
| binary | Binary pseudo charset | binary | 1 |
| geostd8 | GEOSTD8 Georgian | geostd8_general_ci | 1 |
| cp932 | SJIS for Windows Japanese | cp932_japanese_ci | 2 |
| eucjpms | UJIS for Windows Japanese | eucjpms_japanese_ci | 3 |
+----------+-----------------------------+---------------------+--------+
40 rows in set (0.00 sec)
既然知道了是服务器编码方式的问题,那么下面来对它进行处理。
2、修改数据库的默认编码方式
使用命令alter database test character set gbk;
mysql> alter database test character set gbk;
Query OK, 1 row affected (0.09 sec)
查看修改是否成功,使用命令:\s或则status;可以看到Db characterset:gbk 设置成功了。
mysql> status
--------------
mysql Ver 14.14 Distrib 5.6.19, for Win32 (x86)
Connection id: 1
Current database: test
Current user: ODBC@localhost
SSL: Not in use
Using delimiter: ;
Server version: 5.6.19 MySQL Community Server (GPL)
Protocol version: 10
Connection: localhost via TCP/IP
Server characterset: latin1
Db characterset: gbk
Client characterset: gbk
Conn. characterset: gbk
TCP port: 3306
Uptime: 5 hours 9 min 13 sec
Threads: 1 Questions: 46 Slow queries: 0 Opens: 76 Flush tables: 1 Open tables: 66 Queries per second avg: 0.002
--------------
这下应该可以执行插入操作了吧?执行插入后,发现还是一样的报错!!!
mysql> insert into s values(1,'李强','男',23);
ERROR 1366 (HY000): Incorrect string value: '\xC0\xEE\xC7\xBF' for column 'SNAME' at row 1
报错的原因是,在修改数据库test编码方式前,表s就已经建立了。因此,表s它的编码还是数据库之前默认的编码方式latin1,这个可以通过下面的命令来得到确认:
mysql> show create table s;
+-------+-----------------------------------
| Table | Create Table
+-------+-----------------------------------
| s | CREATE TABLE `s` (
`Sno` varchar(20) DEFAULT NULL,
`SNAME` varchar(20) DEFAULT NULL,
`SEX` varchar(10) DEFAULT NULL,
`AGE` int(11) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1 |
+-------+-----------------------------------
1 row in set (0.00 sec)
因此,还需要修改表格的编码方式。
3、修改表格的默认编码方式
对整个表格的编码方式修改之前,这里先说一下,也可以对单独的某个属性列进行编码方式修改。
方法是:(例如,修改表s的SNAME列的编码方式为gbk)alter table s modify SNAME varchar(20) character set gbk;
mysql> alter table s modify SNAME varchar(20) character set gbk;
Query OK, 0 rows affected (0.66 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> show create table s;
+-------+---------------------------------------------------------
| Table | Create Table
+-------+---------------------------------------------------------
| s | CREATE TABLE `s` (
`Sno` varchar(20) DEFAULT NULL,
`SNAME` varchar(20) CHARACTER SET gbk DEFAULT NULL,
`SEX` varchar(10) DEFAULT NULL,
`AGE` int(11) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1 |
+-------+---------------------------------------------------------
1 row in set (0.06 sec)
针对表格修改默认编码方式方法:alter table s character set gbk;从下面的表格的查询结果看,其他的非gbk编码方式的属性列(除了AGE 是定义为int类型,这个默认可以进行转换的之外),其他的:Sno和SEX两个列都是latin1编码方式。因此,需要手动对这些列进行编码方式的修改。(方法请参见本节开头的讨论。)
mysql> alter table s character set gbk;
Query OK, 0 rows affected (0.08 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> show create table s;
+-------+-----------------------------------------------
| Table | Create Table
+-------+-----------------------------------------------
| s | CREATE TABLE `s` (
`Sno` varchar(20) CHARACTER SET latin1 DEFAULT NULL,
`SNAME` varchar(20) DEFAULT NULL,
`SEX` varchar(10) CHARACTER SET latin1 DEFAULT NULL,
`AGE` int(11) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=gbk |
+-------+-----------------------------------------------
1 row in set (0.00 sec)
我这里需要执行的两条列属性级的编码方式修改命令是:
alter table s modify sex varchar(10) character set gbk;
alter table s modify SNAME varchar(20) character set gbk;
而后执行插入操作即可成功:insert into s values(1,'李强','男',23);
mysql> insert into s values(1,'李强','男',23);
Query OK, 1 row affected (0.08 sec)
mysql> select * from s;
+------+-------+------+------+
| Sno | SNAME | sex | AGE |
+------+-------+------+------+
| 1 | 李强 | 男 | 23 |
+------+-------+------+------+
1 row in set (0.00 sec)
4、修改表格属性列的编码方式
方法是:(例如,修改表s的SNAME列的编码方式为gbk)alter table s modify SNAME varchar(20) character set gbk;
5、从源头上避免这个问题发生-建立数据库的时候就设置好编码方式
如果是一个好的数据库设计者,应该在数据库一开始的时候就对数据库中的表格属性的使用进行详细的规划,比如哪些表格属性会使用中文,
哪些属性会有其他的业务规则等等。当然,这个不是本文要做的。下面介绍一下如何在数据库创建阶段就设置编码方式:
create database test_code character set gbk;
执行完这条命令后,后面在该数据库中创建的表格都默认是gbk编码方式。
mysql> create database test_code character set gbk;
Query OK, 1 row affected (0.06 sec)
mysql> use test_code;
Database changed
mysql> status;
--------------
mysql Ver 14.14 Distrib 5.6.19, for Win32 (x86)
Connection id: 1
Current database: test_code
Current user: ODBC@localhost
SSL: Not in use
Using delimiter: ;
Server version: 5.6.19 MySQL Community Server (GPL)
Protocol version: 10
Connection: localhost via TCP/IP
Server characterset: latin1
Db characterset: gbk
Client characterset: gbk
Conn. characterset: gbk
TCP port: 3306
Uptime: 6 hours 1 min 34 sec
Threads: 1 Questions: 75 Slow queries: 0 Opens: 88 Flush tables: 1 Open tables: 68 Queries per second avg: 0.003
--------------
mysql> create table t_code(strA varchar(20) );
Query OK, 0 rows affected (0.20 sec)
mysql> show create table t_code;
+--------+-----------------------------------------------------------------------------------------------+
| Table | Create Table |
+--------+-----------------------------------------------------------------------------------------------+
| t_code | CREATE TABLE `t_code` (
`strA` varchar(20) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=gbk |
+--------+-----------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
另外,表格创建的时候,也可以指定编码方式,方法如下:create table t_code2(strB varchar(20) character set utf8);
mysql> create table t_code2(strB varchar(20) character set utf8);
Query OK, 0 rows affected (0.19 sec)
mysql> show create table t_code2;
+---------+-------------------------------------------------------------------------------------------------------------------+
| Table | Create Table |
+---------+-------------------------------------------------------------------------------------------------------------------+
| t_code2 | CREATE TABLE `t_code2` (
`strB` varchar(20) CHARACTER SET utf8 DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=gbk |
+---------+-------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
另外,这里也可以从配置文件的配置上做修改,MySql的配置文件Windows下一般在系统目录下或者在MySql的安装目录下名字叫my.ini,
可以搜索,Linux下一般是 /etc/my.cnf 这里就先不介绍了。
从本问题可以看到,一开始对数据库规划好了,是可以省去很多麻烦的事的。
参考资料:
1. http://zhidao.baidu.com/link?url=Jg-IUuge0d5hT-r2oApoohHVw1U_hfMZAFqIJak_S9-UVRr_ijDRhvIzFhyuQtjB_ogmVa0y6w_8r82z8f-DoK
2. http://blog.csdn.NET/red4711/article/details/6007248