我在向列表中输入汉子的时候出现错误,例如:
我的列表信息是
mysql> desc class; +---------------+----------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +---------------+----------+------+-----+---------+----------------+ | id | int(11) | NO | PRI | NULL | auto_increment | | name | char(32) | NO | | NULL | | | age | int(11) | NO | | NULL | | | register_date | date | NO | | NULL | | +---------------+----------+------+-----+---------+----------------+ 4 rows in set (0.00 sec)
再随便插入一条信息时出现错误
mysql> insert into class (name,age,register_date) values ('喊',22,'2018.02.01'); ERROR 1366 (HY000): Incorrect string value: '\xE5\x96\x8A' for column 'name' at row 1
查看表中列的字段编码发现并不是utf8
mysql> show full columns from class; +---------------+----------+-------------------+------+-----+---------+----------------+---------------------------------+---------+ | Field | Type | Collation | Null | Key | Default | Extra | Privileges | Comment | +---------------+----------+-------------------+------+-----+---------+----------------+---------------------------------+---------+ | id | int(11) | NULL | NO | PRI | NULL | auto_increment | select,insert,update,references | | | name | char(32) | latin1_swedish_ci | NO | | NULL | | select,insert,update,references | | | age | int(11) | NULL | NO | | NULL | | select,insert,update,references | | | register_date | date | NULL | NO | | NULL | | select,insert,update,references | | +---------------+----------+-------------------+------+-----+---------+----------------+---------------------------------+---------+ 4 rows in set (0.00 sec)
修改表的字段编码
1,改变表的编码格式
alter table table_name default character set utf8; #将表table_name 的编码格式改为utf8
2,改变表中某一列的编码格式
alter table table_name change columns_name columns_name varchar(32) character set utf8 not null; #将columns_name 字段的编码方式改为utf8.
mysql> alter table class change name name varchar(32) character set utf8 not null; Query OK, 7 rows affected (1.09 sec) Records: 7 Duplicates: 0 Warnings: 0 mysql> show full columns from class; +---------------+-------------+-----------------+------+-----+---------+----------------+---------------------------------+---------+ | Field | Type | Collation | Null | Key | Default | Extra | Privileges | Comment | +---------------+-------------+-----------------+------+-----+---------+----------------+---------------------------------+---------+ | id | int(11) | NULL | NO | PRI | NULL | auto_increment | select,insert,update,references | | | name | varchar(32) | utf8_general_ci | NO | | NULL | | select,insert,update,references | | | age | int(11) | NULL | NO | | NULL | | select,insert,update,references | | | register_date | date | NULL | NO | | NULL | | select,insert,update,references | | +---------------+-------------+-----------------+------+-----+---------+----------------+---------------------------------+---------+ 4 rows in set (0.00 sec)