Item pit row Subtotal: mysql database insert included with general emoji expression string error occurred and solutions

First, a first call: Wuhan Come on!

Epidemic should be coming to an end, our company also ushered in the return to work, working from home these days, the bad news came suddenly in charge of the center line:

A production line interface to a problem!

After investigation, it turned out to be an interface to save the interface when it receives a micro-channel micro-channel nickname came to our central database interpolated data due to incidental emoji, being given!

I say strange, this problem should not be ah, the total is well known, currently supports mysql database is relatively complete, as long as the character set of the database is set to utf8mb4 !

Sure enough, check the database character set line, but it really is not utf8mb4 utf8!

1. Cause:

UTF-8 encoding there may be two, three, four bytes. Emoji expression is 4 bytes, and the Mysql utf8 encoding up to three bytes, so the data can not be inserted.

Corresponding Solution 1:

Very simple, just start the character set of the database table column or character set for the utf8mb4 can. The transcoding from Mysql utf8 into utf8mb4. Details are as follows:

First stop MySQL Server service, modify the mysql configuration file my.cnf (other systems) or mysql.ini (windows system)

[client]

default-character-set = utf8mb4

[mysql]

default-character-set = utf8mb4

[mysqld]

character-set-client-handshake = FALSE

character-set-server = utf8mb4

collation-server = utf8mb4_unicode_ci

init_connect='SET NAMES utf8mb4'

Note: 1. The need> = MySQL 5.5.3 version (5.5.29 can also be tested) low version does not support this character set, copy error

2. If you just need a field only need to modify the character set that field on it

Focus here

But unfortunately, this is the database we use online environment, the character set has been used once, modify the character set easily display data on the current stock of causing problems.

So we can only be changed in case of right database character set, to make up for existing deficiencies. .

Solution 2

The method of thought is: to get the emoji expression transcoding, and transcoding to a string so that it can be stored in the utf8 character set.

It comes with a seniors development of more user-friendly Toolkit:

Emoji conversion tool, to facilitate a variety of client-generated Emoji string into another format

    <dependency>
            <groupId>com.github.binarywang</groupId>
            <artifactId>java-emoji-converter</artifactId>
            <version>0.1.1</version>
            <exclusions>
                <exclusion>
                    <artifactId>xstream</artifactId>
                    <groupId>com.thoughtworks.xstream</groupId>
                </exclusion>
                <exclusion>
                    <artifactId>guava</artifactId>
                    <groupId>com.google.guava</groupId>
                </exclusion>
            </exclusions>
        </dependency>
复制代码

Write a utility method:

public class EmojiUtils {

    /**
     * emoji字符alias转unicode
     * @param alias
     * @return
     */
    public static String toUnicode(String alias){
        if(StringUtil.isNotBlank(alias)){
            EmojiConverter emojiConverter = EmojiConverter.getInstance();
            return emojiConverter.toUnicode(alias);
        }
        return alias;
    }

    /**
     * emoji字符unicode转alias
     * @param alias
     * @return
     */
    public static String toAlias(String alias){
        if(StringUtil.isNotBlank(alias)){
            EmojiConverter emojiConverter = EmojiConverter.getInstance();
            return emojiConverter.toAlias(alias);
        }
        return alias;
    }
}
复制代码

ToUnicode transcoding performed before storage to save the data,

Be on display in the field toAlias ​​query interface to use.

Pits're done

to sum up

This will pit the best preventive measures, it will be the best field to utf8mb4 time in building a database built form, prevent the emergence of this pit is the best (o'ω`o) Techno

Guess you like

Origin juejin.im/post/5e53980af265da57584da2b8