mysql 中 王数据表中插入中文字段报错:mysql> insert into a values(202,"王一"); ERROR 1366 (HY000): Incorrect string value: '\xE7\x8E\x8B\xE4\xB8\x80' for column 'tname' at row 1
报错信息:
mysql> insert into a values(202,"王一");
ERROR 1366 (HY000): Incorrect string value: '\xE7\x8E\x8B\xE4\xB8\x80' for column 'tname' at row 1
原因:编码问题
数据表a的编码默认为(latin1):
mysql> show create table a
-> ;
+-------+------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table |
+-------+------------------------------------------------------------------------------------------------------------------------+
| a | CREATE TABLE `a` (
`tno` int(4) DEFAULT NULL,
`tname` char(20) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1 |
+-------+------------------------------------------------------------------------------------------------------------------------+
1 row in set (3.91 sec)
修改表A的编码:
mysql> alter table a convert to character set utf8;
mysql> show create table a
->
->
-> ;
+-------+----------------------------------------------------------------------------------------------------------------------+
| Table | Create Table |
+-------+----------------------------------------------------------------------------------------------------------------------+
| a | CREATE TABLE `a` (
`tno` int(4) DEFAULT NULL,
`tname` char(20) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 |
+-------+----------------------------------------------------------------------------------------------------------------------+
1 row in set (0.09 sec)
插入数据
mysql> insert into a values(202,"王一");
Query OK, 1 row affected (0.04 sec)
问题解决。。