我的连接是这样设置的:Connection conn = DriverManager.getConnection(url + dbName + "?useUnicode=true&characterEncoding=utf-8", userName, password);
在为表添加行时,我得到了以下错误:Incorrect string value: 'xF0x90x8Dx83xF0x90...' for column 'content' at row 1
我插入了数千条记录,当文本包含\xF0(即不正确的字符串值始终以\xF0开头)时,我总是会收到这个错误。
该列的排序规则为utf8_General_ci。
这可能是什么问题?
21条答案
按热度按时间a6b3iqyw1#
I had this problem with my PLAY Java application. This is my stack trace for that exception:
I was trying to save a record using io.Ebean. I fixed it by re creating my database with utf8mb4 collation, and applied play evolution to re create all tables so that all tables should be recreated with utf-8 collation.
lfapxunr2#
如果您在Java中遇到了类似的问题,并且不能灵活地更改数据库的字符集和排序编码,那么这个答案很适合您。
您可以使用Emoji Java库(如果您不使用Java,也可以使用类似的库)来实现相同的功能。您可以在保存/更新到数据库之前转换为别名,然后转换回Unicode POST保存/更新/从数据库加载。主要的好处是即使在编码之后文本的可读性也是如此,因为这个库只给表情符号起别名,而不是整个字符串。
代码快照示例:
字符串/Unicode到别名(保存/更新到数据库之前)
别名到Unicode/字符串(保存/更新/从数据库加载之后)
注意:如果您使用的是Spring Boot,则可以在enitity中使用@PrePersist、@PreUpdate、@PostPersists、@PostUpdate、@PostLoad来执行别名和Unicode转换。
qlzsbp2j3#
最后,在尝试了许多查询之后,这个方法奏效了
0yg35tkg4#
但是,重要的是要注意,MySQL连接器驱动程序版本必须早于5.1.47或更高版本。
efzxgjgh5#
我还必须删除并重新创建数据库的所有存储过程(以及函数),以便它们在utf8mb4的新字符集内执行。
运行:
…查看哪些过程尚未更新为服务器的新CHARACTER_SET_CLIENT、COLLATION_CONNECTION和数据库归类的值。
qvk1mo1f6#
删除模式并使用
utf8mb4
字符集重新创建模式解决了我的问题。disho6za7#
您需要在meta html中设置utf8mb4,还需要在服务器中更改tabel,并将排序规则设置为utf8mb4
72qzrwbm8#
如果您要创建一个新的MySQL表,您可以在创建时指定所有列的字符集,这为我解决了这个问题。
你可以阅读更多详细信息:https://dev.mysql.com/doc/refman/8.0/en/charset-column.html
dzhpxtsq9#
my solution is change the column type from varchar(255) to blob
sxissh0610#
Hint: OnAWS RDSyou need a newParameter Groupfor your MySQL DB with the params (instead of editing a my.cnf)
Note: character_set_system stays "utf8"
These SQL commands doNOT**WORK PERMANENTLY - only in a session:
soat7uwm11#
this is not the recommendation solution.. But worth to share. Since my project are upgrade the DBMS from old Mysql to newest (8). But I cant change the table structure, only the DBMS config (mysql). The solution for mysql server.
test onWindowsmysql8.0.15on mysqlconfigsearch for
sql-mode="....."
uncomment it. Or in my case just type/add
sql-mode="NO_ENGINE_SUBSTITUTION"
why not recommended solution. because if you use latin1 (my case).. the data insert successly but not the content (mysql not respond with error!!) . for example you type info like this
bla \x12
it save
bla [] (box)
okay.. for my problem.. I can change the field to UTF8.. But there is small problem.. see above answer about other solution is failed because the word is not inserted because contain more than 2 bytes (cmiiw).. this solution make your insert data become box. The reasonable is to use blob.. and you can skip my answer.
Another testing related to this were.. usingutf8_encodeon your code before save. I use on latin1 and it was success (I'm not usingsql-mode)! same as above answer usingbase64_encode.
My suggestion to analys your table requirement and tried to change from other format to UTF8
8ulbf1ek12#
MySQL的
utf8
只允许使用UTF-8中可以用3个字节表示的Unicode字符。这里有一个需要4个字节的字符:\xF0\x90\x8D\x83(U+10343 GOTHIC LETTER SAUIL)。如果您有MySQL5.5或更高版本,则可以将列编码从
utf8
更改为utf8mb4
。此编码允许存储UTF-8中占用4个字节的字符。您可能还必须在MySQL配置文件中将服务器属性
character_set_server
设置为utf8mb4
。看来Connector/J defaults to 3-byte Unicode otherwise:例如,要在Connector/J中使用4字节UTF-8字符集,请使用
character_set_server=utf8mb4
配置MySQL服务器,并在Connector/J连接字符串中去掉characterEncoding
。连接器/J随后将自动检测UTF-8设置。06odsfpq13#
I you only want to apply the change only for one field, you could try serializing the field
ha5z0ras14#
Its mostly caused due to some unicode characters. In my case it was the Rupee currency symbol.
To quickly fix this, I had to spot the character causing this error. I copy pasted the entire text in a text editor like vi and replaced the troubling character with a text one.
ca1c2owp15#
Assuming you are usingphpmyadminto solve this error, follow these steps:
1.phpMyAdmin
1.your_table
1."Structure tab"
latin1_swedish_ci
(or whatever it is) toutf8_general_ci