1. 程式人生 > >Emoji 表情入 Mysql 庫問題

Emoji 表情入 Mysql 庫問題

問題原因

emoji表情字元無法直接入utf8編碼的mysql資料庫,因為其utf8編碼長度大多為4個位元組(Emoji Unicode Tables),而utf8編碼的mysql資料庫每個字元的最大長度是3個位元組。這種情況下會報 error code [1366]; Incorrect string value: '\xF0\x9F\x8D\xB0' 類似的錯誤。

mysql utf8

The utf8 character set in MySQL has these characteristics:

mysql utf8mb4

The character set named utf8 uses a maximum of three bytes per character and contains only BMP characters. The utf8mb4 character set uses a maximum of four bytes per character and supports supplementary characters:

  • For a BMP character, utf8 and utf8mb4 have identical storage characteristics: same code values, same encoding, same length.

  • For a supplementary character, utf8 cannot store the character at all, whereas utf8mb4 requires four bytes to store it. Because utf8 cannot store the character at all, you have no supplementary characters in utf8 columns and need not worry about converting characters or losing data when upgrading utf8 data from older versions of MySQL.

    ——

    The utf8mb4 Character Set (4-Byte UTF-8 Unicode Encoding)

解決方法

修改資料庫編碼

將資料庫編碼改為utf8mb4

存取時做轉換

Java轉換工具: