Incorrect string value: '\xF0\x9F\x98\x84\xF0\x9F
来源:互联网 发布:java tarInput 编辑:程序博客网 时间:2024/04/29 14:46
问题描述:从新浪微博抓取消息保存到MySQL数据中,对应数据库字段为varchar,字符编码utf-8。部分插入成功,部分插入失败,报错如标题。
在网上查询,有人说是编码问题,建议修改编码格式,比如改成gbk,UTF-8,blob等等,但是几乎没有人给出更详细的答案。在一个英文网站上,才发现真正错误的原因。链接1 链接2
错误原因:我们可以看到错误提示中的字符0xF0 0x9F 0x98 0x84 ,这对应UTF-8编码格式中的4字节编码(UTF-8编码规范)。正常的汉字一般不会超过3个字节,为什么为出现4个字节呢?实际上是它对应的是智能手机输入法中的表情。那为什么会报错呢?因为mysql中的utf-8并不是真正意义上的utf-8,它只能存储1~3个字节长度的utf-8编码,如果想存储4个字节的必须用utf8mb4类型。不而要使用utf8mb4类型,首先要保证Mysql版本要不低于 MySQL 5.5.3。
解决方案:
1)使用utf8mb4数据类型
要用这种策略,如果MySql版本低于5.5.3,首先要进行版本升级,然后将对应的数据类型改为utf8mb4类型。如果使用的是Connector/J 连接数据库,需要在配置中把编码格式改为utf8mb4(set character_set_server=utf8mb4 in the connection config)。
2)自定义过滤规则,将文本中出现的四字节UTF-8字符过滤或转化为自定义类型。
下面是将4字节字符转化为0000的测试例子。
for (int i = 0; i < b_text.length; i++){ if((b_text[i] & 0xF8)== 0xF0){ for (int j = 0; j < 4; j++) { b_text[i+j]=0x30;}i+=3; }}
2 0
- Incorrect string value: '\xF0\x9F\x98\x84\xF0\x9F
- Incorrect string value: '\xF0\x9F\x98\x84\xF0\x9F
- mysql Incorrect string value \xF0\x9F\x98\x84\xF0\x9F
- Incorrect string value: '\xF0\x9F\x98\x84\xF0\x9F
- Mysql插入数据 Incorrect string value: '\xF0\x9F\x98\x84
- mysql:Incorrect string value: '\xF0\x9F\x98\xB8' for column
- Incorrect string value: '\xF0\x9F\x98\x8A. ...' for column
- SQLException: Incorrect string value: '\xF0\x9F\x91\x8D\xF0\x9F...'
- 【Mysql】存储emoji表情报错(Incorrect string value: '\xF0\x9F\x98\x82\xF0\x9F...')的解决方案
- Incorrect string value: '\xF0\x9F\x98\x92' for column 'NIKENAME' at row 1;
- java.sql.SQLException: Incorrect string value: '\xF0\x9F\x98\x8E' for column 'nick' at row 1
- 彻底解决:Incorrect string value: '\xF0\x9F\x98\xAD",...' for column 'commentContent' at row 1
- java.sql.SQLException: Incorrect string value: '\xF0\x9F\x98\xB3' for column 'Content' at row 1
- python连接SQL报错:1366, "Incorrect string value: '\\xF0\\x9F\\x98\\x81'
- Incorrect string value: '\xF0\x9F\x91\x93\xF0\x9F...' for column 'xxx' at row 1
- java 替换四个字节的字符 '\xF0\x9F\x98\x84\xF0\x9F)的解决方案
- java.sql.SQLException: Incorrect string value: '\xF0\x9F\x90\x94"
- Mysql插入数据报错java.sql.SQLException: Incorrect string value: '\xF0\x9F\x98\x8D\xE8\xBE...'
- PGA转储与_use_realfree_heap作用
- AT88SC0104C加密原理
- NDK编译Android上运行的C可执行程序
- tomcat启动startup.bat一闪而过
- MyEclipse使用Java 通过JDBC连接MySQL数据库的基本测试
- Incorrect string value: '\xF0\x9F\x98\x84\xF0\x9F
- LeetCode 096 Unique Binary Search Trees
- 操作系统(6)同步、通信与死锁
- python list 中append与extend函数的区别赏析
- Percona XtraDB Cluster介绍
- zlib api使用方法摘要[ZT]
- UML九种图之类图
- Android系统多媒体框架添加对.wma格式的支持
- opencv-图像缩放