How to store Emoji Character in MySQL Database

后端 未结 16 2006
迷失自我
迷失自我 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:36

    1) Database: Change Database default collation as utf8mb4.

    2) Table: Change table collation as CHARACTER SET utf8mb4 COLLATE utf8mb4_bin.

    Query:

    ALTER TABLE Tablename CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_bin
    

    3) Code:

    INSERT INTO tablename (column1, column2, column3, column4, column5, column6, column7)
    VALUES ('273', '3', 'Hdhdhdh                                                                    
    0 讨论(0)
  • 2020-11-22 06:36

    The main point hasn't been mentioned in the above answers that,

    We need to pass query string with the options "useUnicode=yes" and "characterEncoding=UTF-8" in connection string

    Something like this

    mysql://USERNAME:PASSWORD@HOSTNAME:PORT/DATABASE_NAME?useUnicode=yes&characterEncoding=UTF-8
    
    0 讨论(0)
  • 2020-11-22 06:36

    Emoji support for application having tech stack - mysql, java, springboot, hibernate

    Apply below changes in mysql for unicode support.

    1. ALTER DATABASE <database-name> CHARACTER SET = utf8mb4 COLLATE = utf8mb4_unicode_ci;
    2. ALTER TABLE <table-name> CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

    DB Connection - jdbc url change:

    jdbc:mysql://localhost:3306/<database-name>?useUnicode=yes&characterEncoding=UTF-8

    Note - If the above step is not working please update mysql-connector version to 8.0.15. (mysql 5.7 works with connector version 8.0.15 for unicode support)

    0 讨论(0)
  • 2020-11-22 06:37

    The simplest solution what works for me is to store the data as json_encode.

    later when you retrieve just make sure you json_decode it.

    Here you don't have to change the collation or the character set of the database and the table.

    0 讨论(0)
提交回复
热议问题