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

UrlRewriter caching issues and a series of related explorations

When developing a website function, the session c...

Solve the problem that PhpStorm fails to connect to VirtualBox

Problem description: When phpstorm's SFTP hos...

A brief discussion on whether CSS animation will be blocked by JS

The animation part of CSS will be blocked by JS, ...

Docker exposes port 2375, causing server attacks and solutions

I believe that students who have learned about th...

Solution to invalid margin-top of elements in div tags

Just as the title says. The question is very stran...

JavaScript to achieve simple tab bar switching case

This article shares the specific code for JavaScr...

Semantics, writing, and best practices of link A

The semantics, writing style, and best practices ...

Is it necessary to create a separate index for the MySQL partition field column?

Preface Everyone knows that the partition field m...

Docker deploys Laravel application to realize queue & task scheduling

In the previous article, we wrote about how to de...

Issues with Rancher deployment and importing K8S clusters

Rancher deployment can have three architectures: ...

Vue+el-table realizes merging cells

This article example shares the specific code of ...

MySQL 5.5 installation and configuration graphic tutorial

Organize the MySQL 5.5 installation and configura...

Comparison of the usage of EXISTS and IN in MySQL

1. Usage: (1) EXISTS usage select a.batchName,a.p...