我正在使用shell文件导出数据库报告。如果我在PHPMyAdmin中运行查询,文件就会很好,仅在数据库的每一行末尾添加新行。
然而,当我在shell脚本中使用outfile运行查询来生成文件时,我在某些列内容中得到/ n,/ r和/ r / n。我无法弄清楚导致这种情况的原因或如何避免它。
问题似乎只是在颜色列中导致,这是示例导出中的第三个。
查询:
mysql $MYSQLOPTS << EOFMYSQL
SELECT Product_Name, Item_Size, Item_Colour, Item_Price, Current_Stock, Item_Price * Current_Stock AS Stock_Value
FROM Items
ORDER BY Product_Name
INTO OUTFILE '$FILE'
FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"'
LINES TERMINATED BY '\n'
EOFMYSQL
示例结果:
"Scarf_in_Peach","ONE SIZE","12/04-B2B2 ",10.00,3,30.00
"Scarf_in_Pink","ONE SIZE ","11/06-odds-C1C12100",10.00,0,0.00
"Scarf_in_Red","ONE SIZE ","11/06-B7B2-C1C12100",10.00,0,0.00
"Scarf_in_Sand_","ONE SIZE","11/06-B1I3-C1C12100
",10.00,0,0.00
"Scarf_in_Sand_/_Blue_Flowers","ONE SIZE","12/04-B2E2-C1C12100 ",10.00,4,40.00
"Scarf_in_Teal","ONE SIZE","11/06-B5G1-C1C12100
",10.00,0,0.00
"Scarf_in_Teal_/_Red_Flowers","ONE SIZE","12/04 - B2B2 ",10.00,1,10.00
"Sunrise_Skinnies","16","ODD-R1S009-1-BLUE",20.00,0,0.00
"Sunrise_Skinnies","8","ODD-R1S009-1
BLUE",20.00,0,0.00
答案 0 :(得分:0)
您有两个选择:
在查询中用空字符串替换回车符和换行符。亲:完全取决于你过滤掉哪些字符和哪些字段。 Con:您必须手动为每个受影响的字段创建表达式。
使用SELECT ... INTO OUTFILE ...
命令的FIELDS ESCAPED BY character选项:
FIELDS ESCAPED BY控制如何编写特殊字符。如果 FIELDS ESCAPED BY字符不为空,在必要时使用 避免歧义作为前面跟随字符的前缀 输出:
FIELDS ESCAPED BY character
字符所包含的字段[OPTIONALLY]
FIELDS TERMINATED BY和LINES TERMINATED BY值的第一个字符
ASCII NUL(零值字节;转义字符后实际写入的内容是ASCII“0”,而不是零值字节)
终止,封闭,退出或终止的字段 BY字符必须进行转义,以便您可以重新读取该文件 可靠。 ASCII NUL被转义,以便更容易查看 寻呼机。
Pro:这是一种快速且标准的方法,您可以使用此方法轻松应用于所有导出功能。骗局:灵活性较差。例如,如果按选项终止的行设置为\n
,那么\r
将不会被转义,这仍然会导致某些系统出现问题。
答案 1 :(得分:0)
问题是,在您的数据中,您在字段中有换行符, phpmyadmin 会自动为您处理。您应该通过尝试从字段中删除额外的换行符来执行查询。