gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 21:41:12 25 4
gpt4 key购买 nike

安装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 |
+------------+---------+-------

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

最佳答案

创建表时使用 UTF-8。

create table table_name () CHARACTER SET = utf8;

插入表时使用 UTF-8

set username utf8; INSERT INTO table_name (ABC,VAL);

关于mysql - 如何解决MySQL中的汉字显示问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34478556/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com