【问题标题】:LOAD DATA INFILE when fields real escaped fields contain \n and \"当字段真实转义字段包含 \n 和 \" 时加载数据输入
【发布时间】:2013-04-03 11:16:38
【问题描述】:

我的 sms 表的架构是 - sms(id, mobileNumber, text)

其中文本的类型为 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 \"

当我运行这个查询时 -

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';

然后在Libreoffice 中打开CSV 文件,我在多个单元格中看到了文本,例如-

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

在一列中,然后将上述句子后面的每个单词放入单独的列中。

还有,为什么要在'\n'之前多加三个'\'?如何获取单个单元格中的文本?

【问题讨论】:

  • 使用哪个程序打开文件是否重要?您是否尝试过以 '\r\n'; 结尾的行? ?
  • 是的。试着把 '\r\n';那也没用。我认为问题出在enclosed by,因为当 " 发生时,它会中断流程。
  • \n 被转义到 \\n 并且看起来它再次被转义到 \\\\n

标签: mysql libreoffice into-outfile


【解决方案1】:

试试这个:

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';

【讨论】:

    猜你喜欢
    • 2012-09-10
    • 1970-01-01
    • 2019-02-13
    • 1970-01-01
    • 2021-01-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多