首页 > 代码库 > MySQL字符编码问题,Incorrect string value
MySQL字符编码问题,Incorrect string value
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",具体见参考
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
参考:
blog.csdn.net/beiigang
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
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.html
-----------------
blog.csdn.net/beiigang
MySQL字符编码问题,Incorrect string value
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。