Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

Java Storage emoji expression solution

Shulou Source: shulou.com Published: 2022-06-03 05:04:39 10月02日 Update

1. The situation of the problem

In the development of IM, some useful avatars use emoji emoticons or chat emoticons using emoji emoticons, but the mysql database uses utf8_general_ci, that is, utf-8 coding, and the result is an error. Emoji emoticons cannot be saved in the database.

Java.sql.SQLException: Incorrect string value:'\ xF0\ x9F\ x92\ xAA ",... 'for column' raw_json' at row 1

2. Why is there such a reason

Because mysql's utf8 encodes up to 3 bytes of a character, but an emoji emoji is 4 bytes, utf8 does not support storing emoji emoticons. But utf8's superset utf8mb4 can have up to 4 bytes in a character, so it can support the storage of emoji emoticons.

3. Solution

Scheme 1: modify the code set of the database to utf8mb4, whether it is a database or a table or a field. Although it will increase storage, this is negligible.

Solution 2: sometimes you can not modify the database coding and so on, you can use java to transform and encode emoji expressions, such as emoji-java, which has a special treatment for emoji expressions, and then save or remove expressions, which is also a compromise solution.

Download address of emoji-java: https://github.com/vdurmont/emoji-java

Tags: Emoji coding data database scheme storage bytes solution reason character or support special address avatar field that is case time sometimes Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Technology NVidia Shulou Information Shulou Tech Info Microsoft