Solve the problem of inserting Chinese characters in the MySQL table Incorrect string value: '\xE4\xBE\xB5\xE5\xAE\xB3...' for column 'name' at row 1 Details

When importing the data of the Excel file into MySQL today, I encountered a simple and depressing problem.

[2023-08-04 10:22:58.566] [IMP] Import table [table_name]
[2023-08-04 10:22:58.647] [ERR] 1366 - Incorrect string value: '\xE4\xBE\xB5\xE5\xAE\xB3...' for column 'name' at row 1

 The imported data is in Chinese, the MySQL format is incorrect, and an error is reported.

Checked the table format, utf8mb4, there is no problem

 1. Convert Excel file to UTF-8 format

 

 This operation doesn't work for me, it's still wrong

2. Separate encoding format for fields

Later, I found the source of this problem, the separate encoding of the field, each field has a separate character set and sorting rules, Chinese needs to be changed to UTF-8, so the problem is solved after the change, and there is no conversion to UTF-8 format The Excel file can also be imported normally.

 3. Some possible MySQL commands

Some friends like to use commands instead of visualization tools, so here are some commands to troubleshoot this problem

Check the character set settings: Make sure that the character set of your MySQL database matches the character set in your Excel table. You can set the character set of the MySQL database to UTF-8 through the following steps:

  • In a MySQL database, check the current character set setting with the following command:
(查看各类的字符集)
SHOW VARIABLES LIKE 'character_set_%';
SHOW VARIABLES LIKE 'char%';

(直接查看表结构)
show create table my_table;
  • If the charset is not UTF-8, you can use the following command to modify the charset setting:
  • (数据库的字符集修改)
    ALTER DATABASE your_database_name CHARACTER SET utf8mb4 COLLATE utf8_general_ci;
    
    (表的字符集修改)
    ALTER TABLE tablename CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8_general_ci;
    
    (字段的字符集修改)
    alter table my_table modify column_name varchar(20) character set utf8mb4;
    

    The above are most of the solutions for this kind of situation, let's go and try it.

Guess you like

Origin blog.csdn.net/weixin_45740811/article/details/132100170