无法添加outfile准备语句mysql

时间:2014-04-10 07:09:23

标签: mysql prepared-statement

这是我的疑问:

SET @query2 = CONCAT('
    SELECT * FROM table_name 
    INTO OUTFILE "',arg_file_path,'/',@var_table_name,'_CURRENT_TIMESTAMP.csv" fields terminated by "," optionally enclosed by ''"'' lines terminated by "\n"    '); 

但它会产生以下输出:

SELECT * FROM lcs_tbl_test
            INTO OUTFILE "/data/test_outfile/lcs_tbl_test_CURRENT_TIMESTAMP.csv" fields terminated by "," optionally enclosed by '"' lines terminated by "

"

基本上我想要" \ n"在我准备好的陈述中打印出来。它正在当前代码中作为行分隔符执行\ n。

2 个答案:

答案 0 :(得分:0)

你需要使用反斜杠作为'\'的双重手段,你需要使用'\'。请使用以下声明。

SET @query2 = CONCAT('
    SELECT * FROM table_name 
    INTO OUTFILE "',arg_file_path,'/',@var_table_name,'_CURRENT_TIMESTAMP.csv" fields terminated by "," optionally enclosed by ''"'' lines terminated by "\\n"    ');

答案 1 :(得分:0)

使用\作为额外转义字符,不将\n解释为换行符。

并且,您可能还需要将current_timestamp字符串从文字值更改为动态值。更改concat参数如下:

SET @query2 = CONCAT(
    'SELECT * FROM table_name INTO OUTFILE "', 
    arg_file_path, '/', @var_table_name, '_', ( CURRENT_TIMESTAMP + 0 ), '.csv" 
    fields terminated by "," 
    optionally enclosed by ''"'' 
    lines terminated by "\\n"    '); 

它产生的输出为:

SELECT * FROM lcs_tbl_test
INTO OUTFILE "/data/test_outfile/lcs_tbl_test_20140410132736.csv" 
fields terminated by "," 
optionally enclosed by '"' 
lines terminated by "\n"