首页 > 代码库 > 修改mysql的数据编码格式

修改mysql的数据编码格式

在mysql中插入数据时出现

mysql> insert into user values ("1","李红","23","吉林长春","2");

ERROR 1366 (HY000): Incorrect string value: ‘\xE6\x9D\x8E\xE7\xBA\xA2‘ for column ‘name‘ at row 1

 因为该列中插入的是中文,所以要修改表user的编码格式

用alter table user default character set utf8;修改

用 show create table user;查询

mysql> alter table user default character set utf8;

Query OK, 0 rows affected (0.03 sec)

Records: 0  Duplicates: 0  Warnings: 0

 

mysql> show create table user;

+-------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+

| Table | Create Table                                                                                                                                                                                                                                                        |

+-------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+

| user  | CREATE TABLE `user` (

  `id` int(11) DEFAULT NULL,

  `name` char(16) CHARACTER SET latin1 DEFAULT NULL,

  `age` int(11) DEFAULT NULL,

  `address` char(32) CHARACTER SET latin1 DEFAULT NULL,

  `wage` tinyint(4) DEFAULT NULL

) ENGINE=InnoDB DEFAULT CHARSET=utf8 |

+-------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+

1 row in set (0.01 sec)

 

 发现name列,和address列仍是latin1的编码格式

用下列两行修改两列的编码格式

 alter table user change name name char(16) character set utf8;
alter table user change address address char(32) character set utf8;

 这样就可以了

修改mysql的数据编码格式