您现在的位置是:首页 > 文章详情

MySQL命令窗口中文乱码或插入中文数据失败

日期:2018-11-01点击:321

在Windwos上使用命令窗口操作MySQL数据库时,如果命令窗口的字符编码与数据库不一致时,就会出现中文乱码的情况

比如查询会出现中文乱码,或者插入数据会出现错误“ERROR 1366 (HY000): Incorrect string value: '\xD5\xC5\xC8\xFD' for column 'name' at row 1”

mysql> insert into user(name,gender,age) values('张三','男',22); ERROR 1366 (HY000): Incorrect string value: '\xD5\xC5\xC8\xFD' for column 'name' at row 1 mysql> select * from user; +----+----------+--------+-----+---------------------+ | id | name | gender | age | updated_time | +----+----------+--------+-----+---------------------+ | 1 | zhangsan | M | 22 | 2018-10-31 17:03:25 | | 2 | 寮犱笁 | 鐢? | 22 | 2018-11-01 09:01:43 | +----+----------+--------+-----+---------------------+ 2 rows in set (0.00 sec)

这时可以先查看下环境编码配置,我的是utf8,结果是下面这样

mysql> show variables like 'char%'; +--------------------------+---------------------------------------------------------+ | 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 | C:\Program Files\MySQL\MySQL Server 5.5\share\charsets\ | +--------------------------+---------------------------------------------------------+ 8 rows in set (0.00 sec)

查看windwos命令窗口的的属性可知,编码格式为GBK且不能更改

这时我们可以使用“set names gbk;”来指定客户端发送给mysql语句时的编码格式,并且可以看到client、connection、results三个变量的编码格式变为了gbk,其它的没有变化

mysql> set names gbk; Query OK, 0 rows affected (0.00 sec) mysql> show variables like 'char%'; +--------------------------+---------------------------------------------------------+ | 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 | C:\Program Files\MySQL\MySQL Server 5.5\share\charsets\ | +--------------------------+---------------------------------------------------------+ 8 rows in set (0.00 sec)

再次执行下插入操作没问题了,查询数据中文也不会乱码了

mysql> insert into user(name,gender,age) values('张三','男',22); Query OK, 1 row affected (0.04 sec) mysql> select * from user; +----+----------+--------+-----+---------------------+ | id | name | gender | age | updated_time | +----+----------+--------+-----+---------------------+ | 1 | zhangsan | M | 22 | 2018-10-31 17:03:25 | | 2 | 张三 | 男 | 22 | 2018-11-01 09:01:43 | +----+----------+--------+-----+---------------------+ 2 rows in set (0.00 sec)

 

原文链接:https://my.oschina.net/lixuelong/blog/2395777
关注公众号

低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。

持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。

转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。

文章评论

共有0条评论来说两句吧...

文章二维码

扫描即可查看该文章

点击排行

推荐阅读

最新文章