简体   繁体   中英

LOAD DATA INFILE when fields real escaped fields contain \n and \"

Schema for my sms table is - sms( id , mobileNumber, text)

where text is of type text .

One of the row has below text -

Foo bar\nLorem ipsum dolor sit amet.\n\nconsectetur adipisicing elit, said:\n\n\" sed do eiusmod tempor incididunt ut labore et dolore magna aliqua don\'t \"

When I run this query -

select 'SMS ID', 'Mobile Number', 'SMS Text' 
union 
select id, ifnull(mobileNumber, 'Not Available'), text 
from sms
into outfile '/tmp/SMSUsage.csv' 
fields terminated by ',' enclosed by '"' 
lines terminated by '\n';

and open the CSV file in Libreoffice , I see the text in more than one cells like -

Foo bar\\\\nLorem ipsum dolor sit amet.\\\\n\\\\nconsectetur adipisicing elit, said:\n\n\

in one column and then each word after above sentence into a separate column.

Also Why does it put three extra '\\' before '\\n'? How do I get the text in a single cell?

Try this:

SELECT 'SMS ID', 'Mobile Number', 'SMS Text' 
UNION 
    SELECT id, IFNULL(mobileNumber, 'Not Available'), text 
    FROM sms
INTO OUTFILE '/tmp/SMSUsage.csv' 
FIELDS TERMINTATED by ',' 
OPTIONALLY ENCLOSED BY '"' 
ESCAPED BY '"' 
LINES TERMINATED BY '\n';

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