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)