MySQL抛出Incorrect string value异常分析
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了MySQL抛出Incorrect string value异常分析相关的知识,希望对你有一定的参考价值。
参考技术A 之前还以为从上至下统一用上UTF-8就高枕无忧了,哪知道今天在抓取新浪微博的数据的时候还是遇到字符的异常。从新浪微博抓到的数据在入库的时候抛出异常:
Incorrect
string
value:
'\xF0\x90\x8D\x83\xF0\x90...'
发现导致异常的字符不是繁体而是某种佛经文字。。。额滴神。。。但是按道理UTF-8应该能支持才对啊,他不是万能的么?
原来问题出在mysql上,mysql如果设置编码集为utf8那么它最多只能支持到3个字节的UTF-8编码,而4个字节的UTF-8字符还是存在的,这样一来如果你建表的时候用的utf8字符集出异常就理所当然了。
解决方法很简单,修改字段或者表的字符集为utf8mb4。
比较蛋疼的是,字符集utf8mb4在mysql
5.5.3之后才支持。
MySQL字符编码问题,Incorrect string value
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:server使用该变量取得链接中客户端的字符集
character_set_connection:server将客户端的query从character_set_client转换到该变量指定的字符集。
character_set_results:server发送结果集或返回错误信息到client之前应该转换为该变量指定的字符集
有两个语句能够设置连接字符集。例如以下:
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时,server对返回结果集不做不论什么转换
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
9
If you change the default character set or collation for a database,
stored routines that use the database defaults must be dropped and
recreated so that they use the new defaults. (In a stored routine,
variables with character data types use the database defaults if the
character set or collation are not specified explicitly. See [HELP
CREATE PROCEDURE].)
參考:
http://dev.mysql.com/doc/refman/5.5/en/globalization.html
http://dev.mysql.com/doc/refman/5.5/en/alter-database.html
-----------------
blog.csdn.net/beiigang
以上是关于MySQL抛出Incorrect string value异常分析的主要内容,如果未能解决你的问题,请参考以下文章
MySQL数据库插入中文时出现Incorrect string value: 'xE6x97xB7xE5x85xA8' for column 'sz_name
MySQL字符编码问题,Incorrect string value
表情存储异常--mybatis抛出异常(java.sql.SQLException: Incorrect string value: 'xF0x9Fx92x94' for co(示例代
关于MYSQL数据库编码(Incorrect string value 错误)
mysql中Incorrect string value乱码问题解决方案
Mysql 的MYISAM引擎拷贝出现异常——Incorrect information in file 'xxx.frm'