时间: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的编码问题
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 |
+----+--------+-------+