I'm trying to parse the CSV output of SELECT INTO OUTFILE. (The purpose is to bulk-load the data into an external NoSQL database; the bulk loader requires a specific file format so I'm writing a generator)
My main problem is the handling of escaped characters - specifically, the FIELDS TERMINATED BY character. MySQL does not seem to escape the delimiter when it occurs in a column value.
The export options of my query are as follows:
FIELDS
TERMINATED BY ','
ENCLOSED BY '"'
ESCAPED BY '\\'
LINES
TERMINATED BY '\n'
I get CSV lines like:
"value1","some, value","another value","value3"
This breaks my CSV line parser that simply splits the line using the FIELDS TERMINATED BY character.
ie my parser produces the following column values:
"some" and "value" should have been parsed as a whole string "some, value"
What is the correct way to parse the output of SELECT INTO OUTFILE?
Anything else that I should worry about given the export options above? My code already handles the following cases within a column value:
(in exact order)
Note:
If anyone can provide the pseudocode of MySQL's LOAD DATA INFILE, that would be fantastic. The exact behavior of that statement is what I'm trying to emulate.
Note:
My parser is written in Java, but I can understand PHP much better
I ended up using OpenCSV ; although I had to write a special handler for NULL value which MySQL dumps as "\\N". Fortunately, my schema does not have any nullable string field so I can safely assume that all occurrences of "\\N" are NULLs.
Thanks to @Doge for reminding me not to reinvent the wheel :)
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.