MySQL字符编码问题,Incorrectstringvalue

页面导航:首页 > 数据库 > Mysql > MySQL字符编码问题,Incorrectstringvalue

MySQL字符编码问题,Incorrectstringvalue

来源: 作者: 时间:2016-01-25 09:47 【

MySQL上插入汉字时报错如下,具体见后面分析。Incorrect string value: xD0 xC2 xC8A xBEW for column ctnr at row 1MySQL字符集相关参数:character_set_server : 服务器字符集
MySQL上插入汉字时报错如下,具体见后面分析。
Incorrect string value: '\xD0\xC2\xC8A\xBEW' for column 'ctnr' at row 1

MySQL字符集相关参数:
character_set_server : 服务器字符集
collation_server : 服务器校对规则

character_set_database : 默认的字符集
collation_database : 默认数据库的校对规则

character_set_client:服务器使用该变量取得链接中客户端的字符集

character_set_connection:服务器将客户端的query从character_set_client转换到该变量指定的字符集。
character_set_results:服务器发送结果集或返回错误信息到客户端之前应该转换为该变量指定的字符集

有两个语句可以设置连接字符集,如下:
A
SET NAMES 'charset_name' 相当于下面三句:
mysql> SET character_set_client = x;
mysql> SET character_set_results = x;
mysql> SET character_set_connection = x; #这个也设置了collation_connection的默认值x

B
SET CHARACTER SET charset_name 相当于下面三句:
mysql> SET character_set_client = x;
mysql> SET character_set_results = x;
mysql> SET collation_connection = @@collation_database;

character_set_results为NULL时,服务器对返回结果集不做任何转换
mysql> SET character_set_results = NULL;

因为字符集编码引起的问题在pg上报的错是:invalid byte sequence for encoding "UTF8",具体见参考

http://blog.csdn.net/beiigang/article/details/39582583

MySQL上字符集编码引起的问题报:Incorrect string value,具体见下面实验:

1
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 | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+
8 rows in set (0.00 sec)

2
mysql> create table tb_tt (id int, ctnr varchar(60));
Query OK, 0 rows affected (0.06 sec)

3
mysql> show create table tb_tt;
+-------+-----------------------------------------------------------------------
-----------------------------------------------------+
| Table | Create Table
|
+-------+-----------------------------------------------------------------------
-----------------------------------------------------+
| tb_tt | CREATE TABLE `tb_tt` (
`id` int(11) DEFAULT NULL,
`ctnr` varchar(60) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 |
+-------+-----------------------------------------------------------------------
-----------------------------------------------------+
1 row in set (0.00 sec)

4
mysql> insert into tb_tt(id,ctnr) values(1,'新華網');
Query OK, 1 row affected (0.02 sec)

5
mysql> select * from tb_tt;
+------+--------+
| id | ctnr |
+------+--------+
| 1 | 新華網 |
+------+--------+
1 row in set (0.02 sec)

6
mysql> set names 'UTF8';
Query OK, 0 rows affected (0.00 sec)

7
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 | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+
8 rows in set (0.00 sec)

8
mysql> insert into tb_tt(id,ctnr) values(2,'新華網');
ERROR 1366 (HY000): Incorrect string value: '\xD0\xC2\xC8A\xBEW' for column 'ctnr' at row 1

参考:

http://dev.mysql.com/doc/refman/5.5/en/globalization. 

-----------------

转载请著明出处:
blog.csdn.net/beiigang
Tags:

文章评论

最 近 更 新
热 点 排 行
Js与CSS工具
代码转换工具

<