我尝试使用MySQL导出sql文件。我使用此方法是因为我想导入具有LOAD DATA
功能的文件。并且文件名必须以dinamically方式生成。我将执行通过命令行生成的文件。
这是剧本:
SET @filename = 'C:/icl/myfile.CSV';
SET @str = CONCAT('LOAD DATA INFILE ',@filename);
SET @str = CONCAT(@str,' INTO TABLE icl_process_data.filecontent LINES TERMINATED BY ''\n''');
SELECT @str INTO OUTFILE 'C:/icl/tmp_script.sql';
结果如下:
LOAD DATA INFILE C:/icl/myfile.CSV INTO TABLE icl_process_data.filecontent LINES TERMINATED BY '\'
我不知道为什么断线的结果' \ n'是' \#39;。我怎么能解决这个问题?
答案 0 :(得分:0)
这是逃避序列的问题,有几种方法可以做到这一点。我选择将起始单引号放在第一路的末尾附近的外部双引号内(在concat
中有3个块)。
单引号作为第二种方式(在concat
中有2个块):
SET @filename = 'C:/icl/myfile.CSV';
-- C:/icl/myfile.CSV
SET @str = CONCAT('LOAD DATA INFILE ',@filename);
-- LOAD DATA INFILE C:/icl/myfile.CSV
-- First way is below (with the result being the line after it if you ignore the `-- ` at the beginning):
SET @str = CONCAT(@str," INTO TABLE icl_process_data.filecontent LINES TERMINATED BY '","\\n'");
-- LOAD DATA INFILE C:/icl/myfile.CSV INTO TABLE icl_process_data.filecontent LINES TERMINATED BY '\n'
-- Second way is below (with the result being the line after it if you ignore the `-- ` at the beginning):
SET @str = CONCAT('LOAD DATA INFILE ',@filename);
SET @str = CONCAT(@str,' INTO TABLE icl_process_data.filecontent LINES TERMINATED BY \'\\n\'');
-- LOAD DATA INFILE C:/icl/myfile.CSV INTO TABLE icl_process_data.filecontent LINES TERMINATED BY '\n'
来自String Literals上的mysql手册页: