I'm trying to load a CSV into MySQL using the LOAD DATA INFILE technique. It's working fine, but I have a problem where some columns use double quotes and some do not.
Example:
something,123,something,"Bauer, Jack",123,something
What happens is the commas inside the quotes break the import, so my data is all jacked up at the end. Not sure how to get the import to escape commas inside the double quotes.
mysql --user= --password= -e "LOAD DATA INFILE '' INTO TABLE FIELDS TERMINATED BY ',' LINES TERMINATED BY '\r\n' (col1, col2, col3, ...)"
解决方案
You need to execute the statement LOAD DATA INFILE with the additional option
FIELDS OPTIONALLY ENCLOSED BY '"'
Thus the whole statement becoming
LOAD DATA INFILE '' INTO TABLE
FIELDS TERMINATED BY ','
OPTIONALLY ENCLOSED BY '"'
LINES TERMINATED BY '\r\n'
(col1, col2, col3, ...)
For further readings, please consult the excellent MySQL Reference Manual e.g. for MySQL 5.1 GA.