Mysql CSV导入 - 最后一个字段的问题

时间:2016-04-03 07:11:41

标签: mysql csv

这就是csv数据的样子:

"COUNTRY_ALPHA2_CODE","COUNTRY_NUMERIC_CODE","COUNTRY_NAME","REGION_CODE","REGION_NAME"
"AD","020","Andorra","02","Canillo"
"AD","020","Andorra","03","Encamp"
"AD","020","Andorra","04","La Massana"
"AD","020","Andorra","05","Ordino"

当我尝试使用以下代码导入它时:

LOAD DATA LOCAL INFILE 'COUNTRIES.CSV'
INTO TABLE countries
FIELDS TERMINATED BY ','
OPTIONALLY ENCLOSED BY '"'
ESCAPED BY '"'
LINES TERMINATED BY '\n'
IGNORE 1 LINES;
select * from countries; 

我在最后一个字段上得到了非常奇怪的行为,好像它正在查找最后一个字段以获得逗号。

+-------------------------+---------------------+---------------------+----------------------+
| COUNTRY_NAME            | COUNTRY_ALPHA2_CODE | COUNTRY_ALPHA3_CODE | COUNTRY_NUMERIC_CODE |
+-------------------------+---------------------+---------------------+----------------------+
| Afghanistan             | AF                  | AFG                 | 004"
"Ant           |
| Anguilla                | AI                  | AIA                 | 660"
"Alb           |
| Aland Islands           | AX                  | ALA                 | 248"
"Aze           |
| Andorra    

我查看了CSV导入的示例,数据似乎采用相同的格式,最后没有逗号。不知道如何告诉mysql最后一个字段不会有逗号。

1 个答案:

答案 0 :(得分:1)

好的,玩“终止线”并删除“逃脱”就可以了:

LOAD DATA LOCAL INFILE 'COUNTRIES.CSV'
INTO TABLE countries
FIELDS TERMINATED BY ','
OPTIONALLY ENCLOSED BY '"'
LINES TERMINATED BY '\r\n'
IGNORE 1 LINES;

返回

+-----------------+---------------------+---------------------+----------------------+
| COUNTRY_NAME    | COUNTRY_ALPHA2_CODE | COUNTRY_ALPHA3_CODE | COUNTRY_NUMERIC_CODE |
+-----------------+---------------------+---------------------+----------------------+
| Aruba           | AW                  | ABW                 | 533                  |
| Afghanistan     | AF                  | AFG                 | 004                  |
| Angola          | AO                  | AGO                 | 024                  |
| Anguilla        | AI                  | AIA                 | 660                  |