Steven Steven - 2 months ago 8
MySQL Question

MySQL escape line breaks exporting into CSV?

I have a problem I have a "text" field with a bunch of "stories" that are pasted from word and such and as such include things like line breaks and tabs. I want to export to CSV maintaining these or be able to get them back.

But I can't figure out how to escape them so it works? When I export CSV I get a CSV with a bunch of single sentences on multiple rows/columns.

I tried replacing the news lines with other stuff but I kept messing up. Is there an easy way to do this without destroying data?

Answer

CSV is not a good format for arbitrary text, but you can TRY

SELECT ... INTO OUTFILE '/tmp/text.csv'
   FIELDS ESCAPED BY '""' TERMINATED BY ','  OPTIONALLY ENCLOSED BY '"'
   LINES TERMINATED BY '\n' 
FROM yourtable