Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Importing CSV using LOAD DATA INFILE quote problem

Tags:

mysql

csv

excel

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"

Value 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?

like image 607
Vanessa Avatar asked Jan 20 '23 00:01

Vanessa


1 Answers

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.

like image 119
Casper Avatar answered Feb 08 '23 16:02

Casper