mysql之1366编码报错

环境

Mysql版本:5.7
windows10企业版
.sql文件格式为utf-8

报错

执行指令
source **.sql

ERROR 1366 (HY000): Incorrect string value: '\xAD\xA6' for column 'cname' at row 1
ERROR 1366 (HY000): Incorrect string value: '\x80\xA7\xE4\xBB\xA3\xE6...' for column 'cname' at row 1
ERROR 1366 (HY000): Incorrect string value: '\xAE\xA1\xE5\x9F\xBA\xE7...' for column 'cname' at row 1
ERROR 1366 (HY000): Incorrect string value: '\xAD\xE5\x9B\xBD\xE8\xBF...' for column 'cname' at row 1
ERROR 1366 (HY000): Incorrect string value: '\xAD\xA6\xE7\x89\xA9\xE7...' for column 'cname' at row 1
ERROR 1366 (HY000): Incorrect string value: '\xAD\xA6\xE7\x89\xA9\xE7...' for column 'cname' at row 1
ERROR 1366 (HY000): Incorrect string value: '\x80\x9D\xE8\xAF\xBE\xE6...' for column 'introduction' at row 1
ERROR 1366 (HY000): Incorrect string value: '\xAE\xBA' for column 'cname' at row 1
ERROR 1366 (HY000): Incorrect string value: '\xAD\xA6\xE8\x8B\xB1\xE8...' for column 'cname' at row 1

分析

查看客户端地连接编码格式

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.7\share\charsets\`

修改客户端地连接编码格式

mysql> set character_set_client = 'utf8';
Query OK, 0 rows affected (0.01 sec)

mysql> set character_set_connection = 'utf8'
    -> ;
Query OK, 0 rows affected (0.00 sec)

mysql> set character_set_results= 'utf8' ;
Query OK, 0 rows affected (0.00 sec)

报错

mysql> source D:\2018\DB_Course_Design\sql\course.sql;
ERROR:
Unknown command '\2'.
ERROR:
Unknown command '\D'.

修改

mysql> source D:/2018-2019-2数据库课程设计材料/DB_Course_Design/sql/course.sql;
Query OK, 1 row affected (0.01 sec)

Query OK, 1 row affected (0.00 sec)

总结

  • 使用命令统一一下数据库的字符编码,即可解决~

部分语句解释:

  • character_set_client 客户端使用的编码,如GBK, UTF8 比如你写的sql语句是什么编码的。
  • character_set_results 查询返回的结果集的编码(从数据库读取的数据是什么编码的)。
  • character_set_connection 连接使用的编码

mysql的转换过程:

  • character_set_client -> character_set_connection >内部操作字符集->character_set_results

参考:

  • https://www.cnblogs.com/lazyno/archive/2015/02/07/4278544.html
  • https://blog.csdn.net/geilivablemental/article/details/45034229

你可能感兴趣的:(mysql之1366编码报错)