简体   繁体   English

如何处理SQL状态[HY000]; 错误代码[1366]; 字符串值不正确?

[英]How to handle SQL state [HY000]; error code [1366]; Incorrect string value?

I'm aware this error means a mysql column doesn't accept the value, but this is strange, since the value fits in a Java UTF-8 encoded string, and the mysql column is utf8_general_ci. 我知道这个错误意味着mysql列不接受该值,但这很奇怪,因为该值适合Java UTF-8编码的字符串,而mysql列是utf8_general_ci。 Also, all utf8 characters have worked properly so far, apart from these. 此外,除了这些之外,所有utf8角色到目前为止都已正常工作。

The use-case is: I am importing tweets. 用例是:我正在导入推文。 The tweet in question is: https://twitter.com/bakervin/status/210054214951518212 - you can see the two "strange" characters (and two strange whitespaces between them). 有问题的推文是: https//twitter.com/bakervin/status/210054214951518212 - 您可以看到两个“奇怪”的字符(以及它们之间的两个奇怪的空格)。 The question is - how to handle this: 问题是 - 如何处理:

  • trim these characters (how - which are they, how does the Java UTF-8 differ from MySQL one) 修剪这些字符(如何 - 它们是什么,Java UTF-8与MySQL的不同之处)
  • make the column capable of accepting this value (how - is there anything more utf-y than utf8_general_ci) 使列能够接受这个值(如何 - 有什么比utf8_general_ci更多的东西)

These appear to be unicode surrogate characters . 这些似乎是unicode代理字符 Since they are not actual characters, and it seems MySQL doesn't support them, it is safe to trim them: 由于它们不是真正的字符,并且似乎MySQL不支持它们,因此可以安全地修剪它们:

StringBuilder sb = new StringBuilder();
for (int i = 0; i < text.length(); i++) {
    char ch = text.charAt(i);
    if (!Character.isHighSurrogate(ch) && !Character.isLowSurrogate(ch)) {
        sb.append(ch);
    }
}
return sb.toString();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 SQL错误:1364,SQLState:HY000-字段“ AccCode”没有默认值 - SQL Error: 1364, SQLState: HY000 - Field 'AccCode' doesn't have a default value SQL警告码:1364,SQLState:HY000字段“ district_code”没有默认值 - SQL Warning Code: 1364, SQLState: HY000 Field 'district_code' doesn't have a default value SQL错误:1366不正确的整数值Spring MVC休眠 - SQL Error: 1366 Incorrect integer value Spring MVC Hibernate SQL 错误:1364,SQLState:HY000 - 字段“rating_id”没有默认值/保存具有一对一关系的子实体时 - SQL Error: 1364, SQLState: HY000 - Field 'rating_id' doesn't have a default value / When saving a child entity with one-to-one relation 第1行的错误1007(HY000):无法创建数据库&#39;mgsv&#39;; 数据库存在 - ERROR 1007 (HY000) at line 1: Can't create database 'mgsv'; database exists [HY000] [1215]无法添加外键约束 - [HY000][1215] Cannot add foreign key constraint 为什么我不能在这里创建外键? 错误1215(HY000):无法添加外键约束 - Why cant i create a foreign key here? ERROR 1215 (HY000): Cannot add foreign key constraint ERROR 1126(HY000):无法打开共享库&#39;lib_mysqludf_sys.dll&#39;(错误号:126)找不到指定的模块。) - ERROR 1126 (HY000): Can't open shared library 'lib_mysqludf_sys.dll' (errno: 126) The specified module could not be found.) 已经在使用 utf8mb4 但得到 1366: Incorrect string value: &#39;\\xF0\\x9F\\x98\\x81\\xF0\\x9F...&#39; - Already using utf8mb4 but getting 1366: Incorrect string value: '\xF0\x9F\x98\x81\xF0\x9F…' 如何错误处理Java代码 - how to error handle java code
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM