关于mysql:utf8mb4字符未通过” LOAD DATA INFILE”保存

utf8mb4 characters not surviving “LOAD DATA INFILE”

我有一个csv文件,其中包含一些位于Unicode BMP之外的字符,例如字符??。它们是SMP字符,因此需要将它们存储在MySQL中的utf8mb4字符集和utf8mb4_general_ci归类中,而不是utf8字符集和utf8_general_ci归类中。

这是我的SQL查询。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
MariaDB [tweets]> set names 'utf8mb4';
Query OK, 0 rows affected (0.01 sec)

MariaDB [tweets]> create table test (a text) collate utf8mb4_general_ci;
Query OK, 0 rows affected (0.06 sec)

MariaDB [tweets]> insert into test (a) values ('??');
Query OK, 1 row affected (0.03 sec)

MariaDB [tweets]> select * from test;
+------+
| a    |
+------+
| ??     |
+------+
1 row in set (0.00 sec)

无警告。一切都正确。现在,我想加载该csv文件。为了进行测试,该文件只有一行。

1
2
3
4
5
MariaDB [tweets]> load data local infile 't.csv' into table wzyboy character set utf8mb4 fields terminated by ',' enclosed by '"' lines terminated by '\
\
'
(tweet_id,in_reply_to_status_id,in_reply_to_user_id,retweeted_status_id,retweeted_status_user_id,timestamp,source,text,expanded_urls);
Query OK, 1 row affected, 7 warnings (0.01 sec)      
Records: 1  Deleted: 0  Skipped: 0  Warnings: 7

警告消息是:

1
| Warning | 1366 | Incorrect string value: '\\xF0\\x9F\\x80\\x80' for column 'text' at row 1      |

我所有的工作环境(OS,Termianl等)都使用UTF-8。我在所有可以想到的地方都指定了utf8mb4,如果我手动INSERT INTO,它可以正常工作。但是,当我使用LOAD DATA INFILE [...] CHARACTER SET utf8mb4 [...]时,它只会失败,并显示错误"字符串值不正确"。


问题已解决。

这是一个错误。在实验过程中,我只是TRUNCATE TABLE,但没有重新创建它。因此数据库和表都是utf8mb4,但是列仍然是utf8 ...