The solution to Chinese garbled characters in mysql import files: first create a database and specify the encoding; then before importing the database file, specify the encoding set names utf8.
Recommended: "mysql video tutorial"
Continuing yesterday's question, after the database is configured, the query table found The Chinese characters are garbled, and I tried several methods on the Internet but couldn't solve it.
It seems that it is a problem with the imported sql file, so think in reverse, use commands to create a database, add some data, and then export it to see what happens.
When inserting data into the table, an error was reported: ERROR 1366 (HY000): Incorrect string value: '\xE6\xB5\x8B\xE8\xAF\x95' for column 'bookname' at row 1
This is strange, check the table structure: show create table book;
#I saw an encoding format that I didn’t want to see. latin1, change it decisively, pass the command:
alter table book default character set utf8;After the change, you can see a refreshing result. The encoding of the table has been changed, but there is still a " What the hell?", the field has a garbled latin1:
alter table book change bookname bookname varchar(32) character set utf8;Complete modification I didn’t look at it anymore and tried to insert the data directly:
1 2 3 4 5 |
|
Then proceed to the next step, export the sql file, enter the bin directory of mysql, and start exporting through the command. This process requires entering a password
1 2 |
|
The exported file has a table encoding format of utf8. Comparing it with the previously imported file, no problem can be seen.
Then, it is the process of importing files before. In addition to the problem of creating the database, check the mydb database structure of mydb.sql imported before. It is indeed wrong:1 2 3 4 5 6 7 8 9 |
|
Then I checked the encoding of the table and found something strange:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
|
First check the encoding settings of the database:
I found it very confusing:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
|
set character_set_client = utf8;Get the result
1 2 3 4 5 6 7 8 9 10 11 12 |
|
创建数据库并制定编码:CREATE DATABASE test2 CHARACTER SET utf8 COLLATE utf8_general_ci;
使用:use test2
在导入数据库文件之前,制定编码set names utf8;
导入:source F:xxxx\xxxx\mydb.sql;
不算漫长的等待之后,查询,不乱码了
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
|
实战项目可以继续进行,遂生法喜。
The above is the detailed content of What to do if mysql import file contains Chinese garbled characters. For more information, please follow other related articles on the PHP Chinese website!