简体   繁体   中英

LOAD DATA INFILE and Double Quotes

When I export table to csv, fputcsv adds double quotes to values with space, for example:

day|night|summer|winter
something|123|something|"Bauer Jack"
foo|bla|5|dooper

I figured out I cannot avoid that.

The problem becomes when I try to import this csv with LOAD DATA INFILE, it does not import a line that contains double quotes. So line with "Bauer Jack" example is not imported into mysql.

$query = <<<eof
LOAD DATA LOCAL INFILE '$filename' INTO TABLE `table_name`
FIELDS TERMINATED BY '|' 
OPTIONALLY ENCLOSED BY '"'
LINES TERMINATED BY '\r\n'
IGNORE 1 LINES;
eof;

Can you suggest a solution? Why is line with double quotes not imported?

Everything works fine when I remove double quotes from csv file.

Try putting the OPTIONALLY ENCLOSED BY clause first.

$query = <<<eof
LOAD DATA LOCAL INFILE '$filename' INTO TABLE `table_name`
FIELDS OPTIONALLY ENCLOSED BY '"' TERMINATED BY '|' 
LINES TERMINATED BY '\r\n'
IGNORE 1 LINES;
eof;

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