MySQL Import CSV With Quotes

Written by Tully on July 8, 2010 Categories: MySQL Tags: , , ,

I was tasked to write a 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.

Optionally Enclosed By Example
load data local infile 'example.csv' into table example_table
fields terminated by ','
OPTIONALLY ENCLOSED BY '"'
lines terminated by 'n'
(Column1, Column2, Column3)

No Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>