简体   繁体   中英

LOAD DATA INFILE - Problem with optional quotes and commas

I was using:

LOAD DATA LOCAL INFILE '$filePath'
     INTO TABLE demo
     FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"'
     LINES TERMINATED BY '\r\n'

And I found out I had troubles when importing rows with cells containing JSON strings:

0, demo,"{""Name""=>""demo""}", 29, ...

Notice the "{""Name""=>""demo""}" part.

So I removed OPTIONALLY ENCLOSED BY '"' and it fixed the problem. However now I'm having troubles with a field with the value "Korea, Republic of"

13,"Korea, Republic of",demo,...  

Is there any way I can manage these two cases at the same time?

Note: the input file is external to me, so I can't "re-structure" it.

Does it work if you specify " as the escape character?

LOAD DATA LOCAL INFILE '$filePath'
     INTO TABLE demo
     FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"' ESCAPED BY '"'
     LINES TERMINATED BY '\r\n'

Actually this seems to match one specific documentation note :

To avoid ambiguity, occurrences of the ENCLOSED BY character within a field value can be doubled and are interpreted as a single instance of the character. For example, if ENCLOSED BY '"' is specified, quotation marks are handled as shown here:

 "The ""BIG"" boss" -> The "BIG" boss` The "BIG" boss -> The "BIG" boss The ""BIG"" boss -> The ""BIG"" boss

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