解决mysql客户端执行插入中文数据命令出错以及中文乱码问题

时间:2019年8月16号 星期五 22:40

建表语句和插入数据语句如下:

create table account(
    id int primary key auto_increment,
    name varchar(100) not null,
    money float not null
)character set utf8 collate utf8_general_ci;
insert into account(name, money) values("Dylan1", 10000);
insert into account(name, money) values("Dylan2", 10000);
insert into account(name, money) values("Dylan3", 10000);
insert into account(name, money) values("张三", 10000);
insert into account(name, money) values("李四", 10000);

一、存在问题:

问题1:在Windows 10 的dos下用命令行向mysql数据库中插入中文数据出现如下错误:

mysql> insert into account(name, money) values("张三", 10000);
ERROR 1366 (HY000): Incorrect string value: '\xC7\xF1\xBC\xD2\xB1\xA6' for column 'name' at row 1

问题2:在Windows 10 的dos下用命令行显示数据表数据出现中文数据乱码现象如下:

mysql> select * from account;
+----+-----------+-------+
| id | name      | money |
+----+-----------+-------+
|  1 | Dylan1    | 10000 |
|  2 | Dylan2    | 10000 |
|  3 | Dylan3    | 10000 |
|  4 | 閭卞瀹?   | 10000 |
|  5 | 閭卞瀹?   | 10000 |
+----+-----------+-------+

二、解决思路:

1、使用“show create table 表名;”命令查看所创建表格的编码格式

mysql> show create table account;
+---------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table   | Create Table                                                                                                                                                                                                            |
+---------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| account | CREATE TABLE `account` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(100) CHARACTER SET gbk NOT NULL,
  `money` float NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=utf8 |
+---------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+

2、使用"show variables like ‘character%’;"命令查看mysql编码设置

mysql> show variables like 'character%';
+--------------------------+---------------------------------------------------------+
| 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       | D:\Program Files\MySQL\MySQL Server 5.5\share\charsets\ |
+--------------------------+---------------------------------------------------------+

下图出自https://blog.csdn.net/u010682362/article/details/42125317关于MySQL如何修改character_set_client的编码问题

img

3、解决方法

(1)方法一(ps:此方法存在弊端,只能暂时性地解决当前操作的问题,需要每次进入mysql客户端时重新设置):

​ 需要注意的是,mysql客户端默认的编码格式和我们的Windows操作系统一样,都是gbk,而mysql数据库的编码格式一般是utf8。只要保证mysql客户端的编码格式和mysql数据库的编码格式一样就能解决问题。我们只需要改变如下两个变量编码格式为gbk即可:

mysql> set character_set_client=gbk; 
//用命令行向mysql数据库中插入中文数据不再出现ERROR 1366
mysql> set character_set_results=gbk;
//用命令行返回数据表数据不再出现中文数据乱码现象

(2)方法二(ps:此方法可以使设置永久有效):

​ 找到mysql安装目录下的my.ini配置文件,并打开找到default-character-set,把编码改为gbk即可

[外链图片转存失败(img-KaOdUrtY-1566048345371)(C:\Users\宝爷\AppData\Roaming\Typora\typora-user-images\1565966897866.png)]

[外链图片转存失败(img-tGQ16Knz-1566048345374)(C:\Users\宝爷\AppData\Roaming\Typora\typora-user-images\1565967048382.png)]

mysql> show variables like'character%';
+--------------------------+---------------------------------------------------------+
| Variable_name            | Value                                                   |
+--------------------------+---------------------------------------------------------+
| character_set_client     | gbk                                                     |
| character_set_connection | gbk                                                     |
| character_set_database   | utf8                                                    |
| character_set_filesystem | binary                                                  |
| character_set_results    | gbk                                                     |
| character_set_server     | utf8                                                    |
| character_set_system     | utf8                                                    |
| character_sets_dir       | D:\Program Files\MySQL\MySQL Server 5.5\share\charsets\ |
+--------------------------+---------------------------------------------------------+
mysql> select * from account;
+----+--------+-------+
| id | name   | money |
+----+--------+-------+
|  1 | Dylan1 | 10000 |
|  2 | Dylan2 | 10000 |
|  3 | Dylan3 | 10000 |
|  4 | 张三       | 10000 |
|  5 | 李四       | 10000 |
+----+--------+-------+
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值