My Envirionment
- System: Windows 7 64-bit
- Database: MySQL 5.7.20 64-bit
- Locale: Chinese
- cmd code page: CP936
MySQL setting
system variables: enter image description here
table info: enter image description here
My Problem
Under the environment and db setting described above, I tried to insert an record into the created table cs_2, however, it failed.
The SQL statement is as follow:
insert into cs_2 (direction) values('出');
The result is:
ERROR 1366 (HY000): Incorrect string value: '\xBA' for column 'direction' at row 1
Personal analysis
After doing some researching on mysql character conversion and Windows cmd input conversion, I have got the following guidances:
- First, it is the character conversion when I input the string on the cmd:
Keyborad typing -> inputmethod conversion -> system inner encoding
in my case, the system inner encoding is the UTF-16 LE formated byte stream of ‘出’
- Second, it is the conversion in the MySQL communication according to the system variables
system inner encoding -> character_set_client -> character_set_connection -> character set of table
in my case, this is
UTF-16 LE -> gbk -> gbk ->gbk
only at the beginning between the system inner encoding and the character_set_client the conversion happened. And I thought that conversion was performed by the system itself.
Thus, under this assumption, the insert SQL should have run successfully, but it failed! I don't know why, I have struggled for this problems for several days but I can't find the solution.
If anyone happened to have encountered this problem, please give me some clues. Great thanks in advance!