MySQL Import CSV With Quotes

Posted by Tully on Thu 08 July 2010

I was tasked to write an automated script to download a CSV file and import it into our database. This is an easy task but ran into an issue I hadn't faced before. The CSV file had quotes on some columns and not on others. This was mix matched so my import script was not working properly. After doing some research I was able to get this working with the MySQL directive OPTIONALLY ENCLOSED BY.

LOAD DATA WITH OPTIONALLY ENCLOSED BY

LOAD DATA LOCAL INFILE 'example.csv' 
INTO TABLE example_table
FIELDS terminated by ','
OPTIONALLY ENCLOSED BY '"'
LINED TERMINATED BY '\n'
(Column1, Column2, Column3)`