Unicode编码-错误1366:字符串值不正确:列的'\ xF0'

I am experiencing some issue with MySQL encoding parsing data and then storing data from the Twitter API.

The tweet that struggles to get stored into the db is:

   INSERT INTO `statuses` (`status_id`,`text`) VALUES('93332222111111','The beers are on me in this case!�')

The character is this one. whereas the following got stored successfully:

INSERT INTO statuses (status_id,text) VALUES('485072105225921','RT @someone: 

Two suggestions possible to solve your problem:

  • Use UTF16 charset;
  • Use utf8mb4 as char set and utf8mb4_unicode_ci as collation.

You can use the following code as an example, extracted from an online tutorial:

# For each database:
ALTER DATABASE database_name CHARACTER SET = utf8mb4 COLLATE = utf8mb4_unicode_ci;
# For each table:
ALTER TABLE table_name CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
# For each column:
ALTER TABLE table_name CHANGE column_name column_name VARCHAR(191) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

As your problem is not with the database, you have to use the right code representing the image. I suggest you use "emoji-java": a lightweight java library that helps you use Emojis in your java applications.

An example:

String str = "An :grinning:awesome :smiley:string 😄with a few :wink:emojis!";
String result = EmojiParser.parseToUnicode(str);
System.out.println(result);
// Prints:
// "An

Do not use utf16 for anything.

Use MySQL's CHARACTER SET utf8mb4; it is equivalent to the outside world's UTF-8, and includes the characters that begin with hex F0. (MySQL's utf8 does not include them.)