2013-08-22 40 views
2

我有一個csv文件,其中包含一些位於Unicode BMP之外的字符,例如字符。它們是SMP字符,因此它們需要存儲在MySQL中的utf8mb字符集和utf8mb_general_ci,而不是utf8字符集和utf8_general_ci整理。utf8mb characaters not surviviving「LOAD DATA INFILE」

所以這裏是我的SQL查詢。

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文件。爲了測試,該文件只有一行。

MariaDB [tweets]> load data local infile 't.csv' into table wzyboy character set utf8mb4 fields terminated by ',' enclosed by '"' lines terminated by '\n\n' (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

警告消息是:

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

我所有的工作環境(OS,吡嗪酰胺等)使用UTF-8。我在每個我能想到的地方都指定了utf8mb4,如果我手動使用INSERT INTO,它就可以工作。但是,當我使用LOAD DATA INFILE [...] CHARACTER SET utf8mb4 [...]它只是失敗,錯誤「不正確的字符串值」。

任何人都可以幫忙嗎?

回答

1

問題已解決。

這是一個錯誤。在實驗過程中,我只是TRUNCATE TABLE但不能重新創建它。所以數據庫和表都是utf8mb4,但列仍然是utf8 ...