MySQL throws Incorrect string value error

偶尔善良 提交于 2019-11-27 12:02:53
Danack

It's the character at the end of the tweet that's causing the problem.

It looks like an 'emoji' character aka japanese smiley face but it's not displaying for me in either Chrome or Safari.

There are known issues storing 4byte utf characters in some versions of MySQL. Apparently you must use utf8mb4 to represent 4 byte UTF characters, as the normal utf8 character set can only represent characters up to 3 bytes in length and so can't store character which are outside of the Basic Multilingual Plane

http://dev.mysql.com/doc/refman/5.5/en/charset-unicode-utf8mb4.html

Which is news to me as it basically means that the utf8 datatype in MySQL isn't really proper utf8.

There are suggestions of how to handle this here How to insert utf-8 mb4 character(emoji in ios5) in mysql? including:

"Also make sure your app layer sets its database connections' character set to utf8mb4. Double-check this is actually happening – if you're running an older version of your chosen framework's mysql client library, it may not have been compiled with utf8mb4 support and it won't set the charset properly. If not, you may have to update it or compile it yourself"

If you're using Connector/J you need to set character_set_server=utf8mb4 in the connection config.

All your character sets should be utf8mb4, which you may have tried but aren't currently set.

I like Danask57's answer - it's correct and the 'right' way to do it. (I up voted it myself)

However, another quick-and-dirty solution is to change the schema. use a varbinary or binary to store the tweet string:

http://dev.mysql.com/doc/refman/5.0/en/binary-varbinary.html

The upside is that you won't get any character set problems.

The downside is that your string comparison and sorting will be lost, and you won't be able to full text index the column.

Just a suggestion, but this is not the 'right' answer, just a quick and dirty solution that gets things working.

I had this exact issue. To solve, change the default encoding on the mysql server side to utf8mb4 following this excellent guide: http://mathiasbynens.be/notes/mysql-utf8mb4 .

Remember to restart your mysqld service after making changes to the configuration file.

For me, I also needed to update the mysql jdbc driver to version 5.1.18 (from version 5.1.6). I have read somewhere that you must use at least version 5.1.14 for the mysql jdbc driver to play nicely with utf8mb4 character encoding. Hope this helps!

Why do you have text outside of the quotes in your example - ie 'lol yes'

tweet="@Dorable_Dimples: Okay enough of those #IfYouWereMines I'm getting depressed. #foreveralone ?" lol yes

the problem is in string "@". the engine database interprete like a special character. i do:

   tweet="Dorable_Dimples: Okay enough of those #IfYouWereMines I'm getting dep

ressed. #foreveralone ?" lol yes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!