当前位置:网站首页>解决MySQL向表中增加数据插入中文乱码问题
解决MySQL向表中增加数据插入中文乱码问题
2022-07-23 06:48:00 【爱吃水果的cc】
文章目录
前言
在命令行使用MySQL创建表后向表中增加数据时出现的中文乱码问题,我安装的MySQL版本是MySQL 5.7,在MySQL 8.0版本之后的不会出现这种问题,所以这是基于MySQL 5.7版本而言的
mysql> insert into student values(1002,'张三');
ERROR 1366 (HY000): Incorrect string value: '\xD5\xC5\xC8\xFD' for column 'name' at row 1
一、为什么会出现这种ERROR
这是源于MySQL中默认的字符集是Latin1所致,当我们使用中文添加入表中时就会出现中文乱码,从而报错,产生ERROR

二、解决步骤
1:查看编码命令
在命令行中使用如下命令
show variables like 'character_%';
show variables like 'collation_%';

2.修改MySQL的数据目录下的my.ini配置文件
修改配置文件使用高级文本编辑器,我这里使用的是Sublime Text 3,使用记事本等软件打开修改后可能会导致文件编码修改为“含BOM头”的编码,从而服务重启失败。
default-character-set=utf8 #默认字符集
[mysqld] # 大概在76行左右,在其下添加
...
character-set-server=utf8
collation-server=utf8_general_ci

3.重启MySQL服务
这里可以手动关闭或者使用命令行来重启MySQL服务,下图是使用命令行重启MySQL服务
4.重新查看编码命令
继续在命令行中使用如下命令
show variables like 'character_%';
show variables like 'collation_%';

5.重新向表中添加中文数据
注意
这里要注意噢,要注意之前创建的表的字符集是Latin1,所以在添加中文的时候会报ERROR,这里的解决方法就是直接删除数据库,重新创建库,默认的就是utf8啦
重新创建数据库

重新创建表后向表中添加中文数据

总结
以上就是解决MySQL向表中增加数据插入中文乱码问题,仅针对MySQL 5.7 版本,MySQL 8.0版本默认的字符集就是utf8,可以直接向表中插入中文数据。原理还是修改字符集的默认方式,utf8字符集。
如果遗忘修改默认的编码,就会出现乱码的问题。从MySQL 8.0开始,数据库的默认编码改为 utf8mb4 ,从而避免了上述的乱码问题。
边栏推荐
- Ros2 self study notes: URDF robot modeling
- Introduction to radar part vii 4 SAR system design
- 知识图谱:基本概念
- 机器学习,吴恩达逻辑回归
- [jzof] 10 Fibonacci series
- vs2019:constexpr 函数“qCountLeadingZeroBits”不能生成常量表达式
- 【可视化调度软件】上海道宁为SMB组织带来NETRONIC下载、试用、教程
- [jzof] 11 minimum number of rotation array
- NFT 交易市场的格局之变:从一家独大到百家争鸣
- [图形学]ASTC纹理压缩格式
猜你喜欢

Qt Creator .pro文件根据kit添加对应库
![[图形学]ASTC纹理压缩格式](/img/d1/734fe91d56716c610980696a8e0d06.png)
[图形学]ASTC纹理压缩格式

Debug No5 basic lighting

网易白帽子黑客训练营笔记(2)

Course design - push box C (win form)

"100 Android interview questions" I brushed angrily for Dachang

Google play app store may delete the overview of APP permissions and use a new combination of data security information

Point target simulation of SAR imaging (II) -- matlab simulation
![[daily training] 814. Binary tree pruning](/img/c1/d71e6190a1855a392689b55503f75e.png)
[daily training] 814. Binary tree pruning

The principle of Google interview questions is to analyze 12 table tennis balls, one of which is defective. Weigh it with a balance for 3 times to find it
随机推荐
Point target simulation of SAR imaging (II) -- matlab simulation
NFT 交易市场的格局之变:从一家独大到百家争鸣
【MUDUO】Poller抽象类
动态规划每日一练(1)
GLIB-CRITICAL g_file_test:assertion ‘filename != null‘ failed
基于BIM+3DGIS的智慧城市基础设施管理
Debug No5基础光照
vs2019:constexpr 函数“qCountLeadingZeroBits”不能生成常量表达式
Typora modify table width
[jzof] 10 Fibonacci series
Google Play应用商店可能会删除应用权限概述 转而使用新的数据安全信息组合
Beifu PLC and C transmit structure type variables through ads communication
Ros2 self study notes: gazebo physical simulation platform
com.mysql.cj.jdbc.exceptions. MysqlDataTruncation: Data truncation: Truncated incorrect DOUBLE value:
High school Chinese teaching material examination outline
Ros2 self study notes: URDF robot modeling
Point target simulation of SAR imaging (I) -- mathematical model
MySQL的索引事务&&JDBC编程
[图形学]ASTC纹理压缩格式
PHP framework MVC class code audit