It seems that it is not possible to export both line feeds and quotation marks correctly for MySQL export.
When exporting, MySQL automatically escapes both
- Field Separators and
- Line separators
By default, the escape character is a backslash. You can override this by adding ESCAPED BY '' to your query.
Unfortunately, in a โnormalโ (Excel-compatible) CSV file, you probably need different encodings for line feeds and quotation marks. In particular, you want line feeds not to be escaped, and quotation marks duplicated.
For example, if the value contains a newline, for example:
This is line 1
And this is "String 2" which contains quotation marks
it should become
"This is line 1
And this is "" Line 2 "", which contains quotation marks "
The solution I found was to pre- ESCAPED BY '' quotes and add ESCAPED BY '' (empty string) to my query.
SELECT REPLACE(field1, '"', '""'), REPLACE(field2, '"', '""'), ... FROM ... WHERE ... INTO OUTFILE '/someFile.csv' FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"' ESCAPED BY '' LINES TERMINATED BY '\n'
James beninger
source share