简体   繁体   中英

MySQL “LOAD DATA INFILE” and Missing Double Quotes

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=<USER> --password=<PASS> -e "LOAD DATA INFILE '<FILENAME>' INTO TABLE <TABLENAME> FIELDS TERMINATED BY ',' LINES TERMINATED BY '\r\n' (col1, col2, col3, ...)" <DATABASE>

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 '<FILENAME>' INTO TABLE <TABLENAME>
FIELDS TERMINATED BY ','
OPTIONALLY ENCLOSED BY '"'
LINES TERMINATED BY '\r\n'
(col1, col2, col3, ...)

For further readings, please consult the excellent MySQL Reference Manual eg for MySQL 5.1 GA.

Hopefully you have figured out what to do by now, but If you haven't hopefully this will help you out.

I have had trouble in Excel 2007 exporting to a customized CSV file. I found that you can change the fields terminator here: http://www.tek-tips.com/viewthread.cfm?qid=1635599&page=15

I prefer to use the pipe | character as it is uncommon, and a little more flexible when dealing with inch", foot" measurements.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM