3

I've a CSV file where values may have comma as part of value

"09200, France, Paris", "Tower ""Olivia"""
"09200, Spain, Barselona", Shop - perfect

However once I import data it breaks value on 4 columns (based on number of comma in row). What do I do wrong? Please see my import command below.

LOAD DATA LOCAL INFILE '~/Downloads/file.csv' INTO TABLE my_table CHARACTER SET utf8 FIELDS TERMINATED BY ',' LINES TERMINATED BY '\n' (@col1,@col2) set address=@col1,name=@col2;

1 Answer 1

4

Add an ENCLOSED BY clause to your query:

LOAD DATA LOCAL INFILE '~/Downloads/file.csv' 
  INTO TABLE my_table 
  CHARACTER SET utf8 
  FIELDS TERMINATED BY ',' 
  OPTIONALLY ENCLOSED BY '"'
  LINES TERMINATED BY '\n'
  (@col1,@col2) set address=@col1,name=@col2;
Sign up to request clarification or add additional context in comments.

2 Comments

It did not work for me, however once I've changed FIELDS TERMINATED BY ',' to FIELDS TERMINATED BY ', ' - it started to work. Do you know why it is so?
That would work only if the CSV is formatted so there is a space after a comma between separations eg "1", "2", "3" would work but not "1","2","3". The first case solves the issue because the parser is looking to break up fields when it sees ...", "... which isn't inside of your field that contains commas with a space after it

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.