【MySQL】ERROR 1366 (HY000) Incorrect string value xxx... for column xxx at row 1

命令行客户端的字符集问题

插入带中文的数据时报错

mysql> insert into t_stu values(1,'法外狂徒张三','男');
ERROR 1366 (HY000): Incorrect string value: '\xA4\x96\xE7\x8B\x82\xE5...' for column 'sname' at row 1

原因应该时服务器端和客户端字符不一致

查看所有字符集:SHOW VARIABLES LIKE ‘character_set_%’;

show variables like ‘character_set_%’

mysql> show variables like 'character_set_%';
+--------------------------+-------------------------------------------+
| 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:\databases\mysql-5.7.28\share\charsets\ |
+--------------------------+-------------------------------------------+
8 rows in set, 1 warning (0.00 sec)

mysql> select * from t_stu;
+------+--------------+--------+
| sid  | sname        | gender |
+------+--------------+--------+
|    1 | ·¨Íâ¿ñͽÕÅÈý            | ÄÐ       |
+------+--------------+--------+
1 row in set (0.00 sec)

解决方案,设置当前连接的客户端字符集“SET NAMES utf8;”

mysql> set names utf8;
Query OK, 0 rows affected (0.00 sec)

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       | D:\databases\mysql-5.7.28\share\charsets\ |
+--------------------------+-------------------------------------------+
8 rows in set, 1 warning (0.00 sec)

mysql> insert into t_stu values(1,'法外狂徒张三','男');
Query OK, 1 row affected (0.00 sec)

mysql> select * from t_stu;
+------+--------------------+--------+
| sid  | sname              | gender |
+------+--------------------+--------+
|    1 | 法外狂徒张三       | 男     |
+------+--------------------+--------+
1 row in set (0.00 sec)

字符集应该一致

set names gbk
set names utf8

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值