简体   繁体   English

utf-8字符集txt文件转换为mysql无法在macOS上显示正确

[英]the utf-8 charset txt file convert to the mysql can not show correct on macos

I create a table in mysql on macos commandline using the 'utf-8' charset, 我使用'utf-8'字符集在macOS命令行上的mysql中创建了一个表,

mysql>  CREATE TABLE tb_stu (id VARCHAR(20), name VARCHAR(20), sex CHAR(1), birthday DATE) default charset=utf8;
Query OK, 0 rows affected (0.02 sec)

mysql> SHOW TABLES;
+----------------+
| Tables_in_test |
+----------------+
| pet            |
| tb_stu         |
+----------------+
2 rows in set (0.00 sec)

mysql> show create table tb_stu \G
*************************** 1. row ***************************
       Table: tb_stu
Create Table: CREATE TABLE `tb_stu` (
  `id` varchar(20) DEFAULT NULL,
  `name` varchar(20) DEFAULT NULL,
  `sex` char(1) DEFAULT NULL,
  `birthday` date DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8
1 row in set (0.00 sec)

I want to add some values to the 'tb_stu' table, I have a txt file containing Chinese string : 我想向“ tb_stu”表中添加一些值,我有一个包含中文字符串的txt文件:

1   小明  男   2015-11-02
2   小红  女   2015-09-01
3   张三  男   2010-02-12
4   李四  女   2009-09-10

and the txt file is 'utf-8' charset too! txt文件也是'utf-8'字符集!

➜  ~ file /Users/lee/Desktop/JAVA/Java从入门到精通/第18章--使用JDBC操作数据库/Example_18_02/tb_stu.txt
/Users/lee/Desktop/JAVA/Java从入门到精通/第18章--使用JDBC操作数据库/Example_18_02/tb_stu.txt: UTF-8 Unicode text

so I execute the mysql command line: 所以我执行mysql命令行:

mysql> LOAD DATA LOCAL INFILE '/Users/lee/Desktop/JAVA/Java从入门到精通/第18章--使用JDBC操作数据库/Example_18_02/tb_stu.txt' INTO TABLE tb_stu;
Query OK, 4 rows affected, 4 warnings (0.01 sec)
Records: 4  Deleted: 0  Skipped: 0  Warnings: 4

but I get the messy code in mysql : 但是我在mysql中得到了凌乱的代码:

mysql> select * from tb_stu;
+------+----------------+------+------------+
| id   | name           | sex  | birthday   |
+------+----------------+------+------------+
| 1    | å°æ˜Ž         | ç    | 2015-11-02 |
| 2    | å°çº¢         | å    | 2015-09-01 |
| 3    | 张三         | ç    | 2010-02-12 |
| 4    | æŽå››         | å    | 2009-09-10 |
+------+----------------+------+------------+
4 rows in set (0.00 sec)

it makes me confused, the tabel in mysql and the txt are both 'utf-8' charset, why I get the messy code? 这让我感到困惑,mysql中的表格和txt都是'utf-8'字符集,为什么我会看到凌乱的代码? thanks a lot! 非常感谢!

You will need to investigate some more to understand your problem. 您将需要进行更多调查才能了解您的问题。 One of the options for example is that your data was written into DB correctly but in your command line it is just displayed incorrectly due to some wrong setting of encoding in your operating system environment. 例如,选项之一是您的数据已正确写入DB,但由于操作系统环境中某些错误的编码设置,因此在命令行中显示不正确。 Or the problem might be that the data was garbled (corrupted) when it was written and that means that it is wrongly stored in the DB. 或问题可能在于数据在写入时出现乱码(损坏),这意味着将其错误地存储在数据库中。 So I would suggest to take your original file with properly displayed Chinese characters and convert them to unicode sequence, and then take the data in DB and also convert them into unicode sequence and compare to see if your DB data is just displayed incorrectly or the data itself is corrupted. 因此,我建议您使用正确显示的汉字来获取原始文件,并将其转换为unicode序列,然后将其存储在DB中,并将它们转换为unicode序列,然后进行比较,以查看您的DB数据显示不正确还是数据正确本身已损坏。 This will help you to understand your problem and then to find a way to fix it. 这将帮助您理解问题,然后找到解决问题的方法。 Here is tool that can help you: 这是可以帮助您的工具:

There is an Open Source java library MgntUtils (written by me) that has a Utility that converts Strings to unicode sequence and vise versa: 有一个开源Java库MgntUtils(由我编写),该库具有将字符串转换为unicode序列,反之亦然的实用程序:

result = "Hello World";
result = StringUnicodeEncoderDecoder.encodeStringToUnicodeSequence(result);
System.out.println(result);
result = StringUnicodeEncoderDecoder.decodeUnicodeSequenceToString(result);
System.out.println(result);

The output of this code is: 此代码的输出是:

\u0048\u0065\u006c\u006c\u006f\u0020\u0057\u006f\u0072\u006c\u0064
Hello World

The library can be found at Maven Central or at Github It comes as maven artifact and with sources and javadoc 该库可以在Maven CentralGithub上找到。它作为Maven工件并带有源代码和javadoc

Here is javadoc for the class StringUnicodeEncoderDecoder 这是类StringUnicodeEncoderDecoder的 javadoc

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM