How to store Emoji Character in MySQL Database

后端 未结 16 2013
迷失自我
迷失自我 2020-11-22 05:34

I am using Emoji character in my project. That characters are saved (??) into mysql database. I had used database Default collation in utf8mb4_general_ci. It sh

16条回答
  •  [愿得一人]
    2020-11-22 06:27

    Both the databases and tables should have character set utf8mb4 and collation utf8mb4_unicode_ci.

    When creating a new database you should use:

    CREATE DATABASE mydb CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
    

    If you have an existing database and you want to add support:

    ALTER DATABASE database_name CHARACTER SET = utf8mb4 COLLATE = utf8mb4_unicode_ci;
    

    You also need to set the correct character set and collation for your tables:

    CREATE TABLE IF NOT EXISTS table_name (
        ...
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE utf8mb4_unicode_ci;
    

    or change it if you've got existing tables with a lot of data:

    ALTER TABLE table_name CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
    

    Note that utf8_general_ci is no longer recommended best practice. See the related Q & A:

    What's the difference between utf8_general_ci and utf8_unicode_ci on Stack Overflow.

提交回复
热议问题