How to solve the problem of Chinese garbled characters when inserting table data into MySQL

How to solve the problem of Chinese garbled characters when inserting table data into MySQL

1. Problem

During development, when inserting data from other databases into the MySQL database table, an error similar to the following is always reported:

Incorrect string value: '\xE6\x88\x91' for column 'name' at row 1

2. Analysis

1. I first checked the types of this field in the two database tables and found that they are both varchar, so there is no problem of insertion error caused by inconsistent types.

2. After eliminating the error caused by inconsistent field types, I guessed that it was a problem with Chinese garbled characters. Then I inserted pure English data and found that there was no error, so I determined that it was a problem with Chinese garbled characters. Then I searched Baidu and found that the garbled Chinese characters were caused by the default encoding problem of MySQL.

3. Solution

1. Check the my.ini configuration file in the local MySQL installation file directory to see if the default encoding of the server and client is utf8.

[mysqld]
# The default character set used by the server is UTF8
character-set-server=utf8

[client]
# Set the default port used by the mysql client to connect to the server default-character-set=utf8

2. After the previous step is correct, open the command line and enter: net start mysql to start the MySQL service;

After the message "The service has been successfully started" is displayed, enter the command "mysql" to use the database;

Check show create table test.xtt_test_copy; "

As shown in the figure above, it is found that the default is "latin1" instead of "utf8". You need to continue to modify it manually. Enter the command " ALTER TABLE test.xtt_test_copy CONVERT TO CHARACTER SET utf8 COLLATE utf8_general_ci; ", and continue to enter the command " show create table test.xtt_test_copy; has been changed to "utf8";

Check show creat database test; "

As shown in the figure above, the default is "latin1" instead of "utf8". You need to continue to modify it manually. Enter the command " ALTER DATABASE test DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci; ", and continue to enter the command " SHOW CREATE database test; " to check that the character set encoding of the default database table has been changed to "utf8";

3. Finally, restart the service.

mysql> net stop mysql;
mysql> net start mysql;

In our daily use of MySQL, we often encounter Chinese garbled characters. Based on my usual work experience, I have summarized the following four points that need attention:

1. The character set when MySQL is started, configure default-character-set=character set in the myini file and put it above [WinMySQLadmin];

2. Set the character set during database table creation, in the CHARSET= character set statement at the end of the CREATE statement;

3. Set the character set in the options in the driver url, jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=character set;

4. Set the displayed character set in the web display page, <%@ page contentType="text/html; charset=character set" language="java" import="java.sql.*" errorPage="" %>In servlet, it is response.setContentType("text/html; charset=character set");

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:
  • Solution to the problem that Chinese characters are converted into full question marks when inserted into Mysql???
  • Five ways to insert Chinese characters into MySQL without garbled characters
  • Solve the problem that MySQL cannot insert Chinese incorrect string value
  • mysql cannot insert Chinese characters
  • Mysql insert Chinese and Chinese query (modification + debugging)
  • Teach you a trick to permanently solve the problem of MySQL inserting Chinese characters

<<:  MySQL/MariaDB Root Password Reset Tutorial

>>:  Detailed explanation of two ways to implement session persistence in Nginx reverse proxy

Recommend

Five practical tips for web form design

1. Mobile selection of form text input: In the te...

W3C Tutorial (12): W3C Soap Activity

Web Services are concerned with application-to-ap...

Detailed explanation of this pointing in JS arrow function

Arrow function is a new feature in ES6. It does n...

JS realizes the front-end paging effect

This article example shares the specific code of ...

MySQL Tutorial: Subquery Example Detailed Explanation

Table of contents 1. What is a subquery? 2. Where...

Docker installation of Nginx problems and error analysis

question: The following error occurred when insta...

JavaScript countdown to close ads

Using Javascript to implement countdown to close ...

Implementation of JavaScript downloading linked images and uploading them

Since we are going to upload pictures, the first ...

An example of how to optimize a project after the Vue project is completed

Table of contents 1. Specify different packaging ...

Use vue3 to implement a human-cat communication applet

Table of contents Preface Initialize the project ...

Troubleshooting MySQL high CPU load issues

High CPU load caused by MySQL This afternoon, I d...

Sharing several methods to disable page caching

Today, when developing, I encountered a method wh...

Summary of changes in the use of axios in vue3 study notes

Table of contents 1. Basic use of axio 2. How to ...