如何解决MySQL中的汉字显示问题?

时间:2015-12-27 07:44:33

标签: mysql sql nscharacterset

安装Mysql之后,我创建了一个表并添加了一些这样的记录

create table score (
    student_id int,
    subject varchar(10),
    subject_score int
);

insert into score values (001,'数学',50),(002,'数学',60),(003,'数学',70);

,结果是

mysql> select * from score;
+------------+---------+---------------+
| student_id | subject | subject_score |
+------------+---------+---------------+
|          1 | ??      |            50 |
|          2 | ??      |            60 |
|          3 | ??      |            70 |
+------------+---------+---------------+
3 rows in set (0.00 sec)

然后我改变了“字符集”

mysql> alter table score character set utf8;
mysql> alter table score modify subject varchar(10) character set utf8;
mysql> set character_set_server=utf8;


mysql> SHOW VARIABLES LIKE 'character_set%';
+--------------------------+---------------------------------------------------------+
| Variable_name            | Value                                                   |
+--------------------------+---------------------------------------------------------+
| character_set_client     | utf8                                                    |
| character_set_connection | utf8                                                    |
| character_set_database   | utf8                                                    |
| character_set_filesystem | binary                                                  |
| character_set_results    | utf8                                                    |
| character_set_server     | utf8                                                    |
| character_set_system     | utf8                                                    |
| character_sets_dir       | /usr/local/mysql-5.5.47-linux2.6-x86_64/share/charsets/ |
+--------------------------+---------------------------------------------------------+
8 rows in set (0.00 sec)

我的表格结构是

mysql> show create table score\G;
*************************** 1. row ***************************
       Table: score
Create Table: CREATE TABLE `score` (
  `student_id` int(11) DEFAULT NULL,
  `subject` varchar(10) DEFAULT NULL,
  `subject_score` int(11) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8
1 row in set (0.00 sec)

我认为一切都很好但是当我查询我的桌子时,汉字仍然无法正确显示

mysql> select * from score;
+------------+---------+---------------+
| student_id | subject | subject_score |
+------------+---------+---------------+
|          1 | ??      |            50 |
|          2 | ??      |            60 |
|          3 | ??      |            70 |
+------------+---------+---------------+
3 rows in set (0.00 sec)

但是当我再次插入相同的记录时,新记录就可以了!

mysql> insert into score values (001,'数学',50),(002,'数学',60),(003,'数学',70);
Query OK, 3 rows affected (0.00 sec)
Records: 3  Duplicates: 0  Warnings: 0

mysql> select * from score;
+------------+---------+---------------+
| student_id | subject | subject_score |
+------------+---------+---------------+
|          1 | ??      |            50 |
|          2 | ??      |            60 |
|          3 | ??      |            70 |
|          1 | 数学    |            50 |
|          2 | 数学    |            60 |
|          3 | 数学    |            70 |
+------------+---------+-------

如何保存我的旧记录?我需要帮助,非常感谢你

1 个答案:

答案 0 :(得分:0)

创建表时使用UTF-8。

  

create table table_name()CHARACTER SET = utf8;

插入表格时使用UTF-8

  

设置用户名utf8; INSERT INTO table_name(ABC,VAL);