Home  >  Q&A  >  body text

MySQL插入中文问题!

我在MySQL建了一个表categories,结构见下:

+-------+--------------+------+-----+---------+----------------+
| Field | Type         | Null | Key | Default | Extra          |
+-------+--------------+------+-----+---------+----------------+
| id    | int(11)      | NO   | PRI | NULL    | auto_increment |
| name  | varchar(128) | YES  | UNI | NULL    |                |
+-------+--------------+------+-----+---------+----------------

然而我插入中文的时候,会报错:

mysql> insert into categories(name) values('测试');
ERROR 1062 (23000): Duplicate entry '??' for key 'name'

但是插入英文不会报错:

mysql> insert into categories(name) values('test');
Query OK, 1 row affected (0.01 sec)

我的MySQL字符集设置如下:

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     | utf8                       |
| character_set_system     | utf8                       |
| character_sets_dir       | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+

这个问题如何解决啊?

迷茫迷茫2716 days ago341

reply all(2)I'll reply

  • 高洛峰

    高洛峰2017-04-17 13:50:34

    character_set_database set to utf8

    reply
    0
  • 天蓬老师

    天蓬老师2017-04-17 13:50:34

    It has nothing to do with the character set and Chinese half cent

    The tips are all there:

    Duplicated entry
    

    It means your UNIQUE field is repeated!

    Learn to interpret mistakes, young man

    reply
    0
  • Cancelreply