Reasons and solutions for failure to insert emoji expressions in MySQL

Reasons and solutions for failure to insert emoji expressions in MySQL

Failure Scenario

When calling JDBC to insert emoji expressions into the MySQL database, an exception java.sql.SQLException: Incorrect string value: '\xF0\x9F\x90\x9B' is thrown.

Cause of failure

A character in MySQL's utf8 encoding is at most 3 bytes, but an emoji expression is 4 bytes, so utf8 does not support the storage of emoji expressions. However, utf8mb4, a superset of utf8, can have a maximum of 4 bytes per character, so it can support the storage of emoji expressions.

Let’s take a look at the detailed introduction.

Solution

1. Modify the database, table, and column character sets

Modify the database character set

ALTER DATABASE database name CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci;

Modify the table character set

ALTER TABLE table name CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;

Modify the column character set

ALTER TABLE table name CHANGE field name field name the original data type of the field CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;

The above three options can be modified according to your actual situation;

2. View MySQL configuration variables

SHOW VARIABLES WHERE Variable_name LIKE 'character_set_%' OR Variable_name LIKE 'collation%'; 

Note whether the value of the character_set_server variable is equal to utf8mb4. If not, see step 3.

3. Modify the mysql configuration file

Here mysql is installed under centos, the configuration file name is my.cnf, and the path is based on the directory where you installed it.

vim /etc/my.cnf /etc/mysql/my.cnf 

my.cnf

Find character_set_server, set its value to utf8mb4, save and exit; try to insert again to see if there is an error; if there is an error, you may need to restart the MySQL service

service mysqld restart

Summarize

The above is the full content of this article. I hope that the content of this article will have certain reference learning value for your study or work. If you have any questions, you can leave a message to communicate. Thank you for your support for 123WORDPRESS.COM.

You may also be interested in:
  • Detailed steps to store emoji expressions in MySQL
  • Analysis of the solution to MySQL's inability to store emoji expressions
  • How to deal with errors in MySQL's emoji table storage [change encoding to utf8mb4]
  • Analysis of solutions to the problem that MySQL cannot store Emoji expressions
  • Solution to the problem of failure to insert emoji expressions into MySQL
  • How to enable Java backend MySQL database to support emoji expressions
  • Tutorial on how to set up MySQL to store emoji characters
  • How to insert Emoji expressions into MySQL

<<:  Solution to forgetting the password of the pagoda panel in Linux 3.X/4.x/5.x

>>:  Vue uses better-scroll to achieve horizontal scrolling method example

Recommend

Implementation of nginx proxy port 80 to port 443

The nginx.conf configuration file is as follows u...

Detailed tutorial on installing Tomcat9 windows service

1. Preparation 1.1 Download the tomcat compressed...

A brief discussion on the CSS overflow mechanism

Why do you need to learn CSS overflow mechanism i...

Practical record of solving MySQL deep paging problem

Table of contents Preface Why does limit deep pag...

Vue's global watermark implementation example

Table of contents 1. Create a watermark Js file 2...

Explanation of nginx load balancing and reverse proxy

Table of contents Load Balancing Load balancing c...

WeChat applet to achieve the revolving lantern effect example

Preface In daily development, we often encounter ...

Specific example of MySQL multi-table query

1. Use the SELECT clause to query multiple tables...

Detailed explanation of MySQL database index

Table of contents 1. Introduction to MySQL Index ...

Detailed explanation of Javascript basics

Table of contents variable Data Types Extension P...

CSS isolation issue in Blazor

1. Environment VS 2019 16.9.0 Preview 1.0 .NET SD...

How to add file prefixes in batches in Linux

You need to add "gt_" in front of the f...