开发者

Importing CSV using LOAD DATA INFILE quote problem

开发者 https://www.devze.com 2023-03-26 23:17 出处:网络
I\'m trying to get this CSV file that I exported from excel loaded into my database and I can\'t seem to get the formatting correct no matter what I try.

I'm trying to get this CSV file that I exported from excel loaded into my database and I can't seem to get the formatting correct no matter what I try.

Here is the SQL:

LOAD DATA INFILE 'path/file.csv'
INTO TABLE tbl_name 
FIELDS TERMINATED BY ',' 
ENCLOSED BY '"' 
LINES TERMINATED BY '\n' 
(column1, column2, column3); 

This works fine but then I run into trouble when the end of a line (column 3) ends in a quote. For example:

Actual value: These are "quotes"

Val开发者_运维知识库ue in CSV: "These are ""quotes"""

What happens is that I will get an extra quote on that value in the database and also any additional lines until it reaches another quote in the CSV. Any ideas on how to solve this?


Hmm. I tried to duplicate this problem but can't. Where does my data differ from yours? Can you provide sample data to duplicate this? Here's what I did:

> cat /tmp/data.csv
"aaaa","bbb ""ccc"" ddd",xxx
xxx,yyy,"zzz ""ooo"""
foo,bar,baz

mysql> CREATE TABLE t2 (a varchar(20), b varchar(20), c varchar(20));
Query OK, 0 rows affected (0.01 sec)

mysql> LOAD DATA INFILE '/tmp/data.csv' INTO TABLE t2 FIELDS TERMINATED BY ',' ENCLOSED BY '"' LINES TERMINATED BY '\n' (a, b, c);
Query OK, 3 rows affected (0.00 sec)
Records: 3  Deleted: 0  Skipped: 0  Warnings: 0

mysql> select * from t2;
+------+---------------+-----------+
| a    | b             | c         |
+------+---------------+-----------+
| aaaa | bbb "ccc" ddd | xxx       |
| xxx  | yyy           | zzz "ooo" |
| foo  | bar           | baz       |
+------+---------------+-----------+
3 rows in set (0.00 sec)

Looks ok to me(?)

Also note that if you're working on a Windows platform you might need to use
LINES TERMINATED BY '\r\n' instead.

0

精彩评论

暂无评论...
验证码 换一张
取 消