0

如何将 outfile 文件路径传递给 MySQL?我已经尝试过以下方法。这没用。

我的 SQL 文件

SET @query = CONCAT('SELECT *
FROM
     `registered_user`
INTO OUTFILE ',@output_location,' FIELDS TERMINATED BY \',\'
LINES TERMINATED BY \'\n\'');

PREPARE stmt FROM @query;
      EXECUTE stmt;
      DEALLOCATE PREPARE stmt;

我的命令行命令

mysql -u root -p test -e "set @output_location= '/var/tmp/out.csv'; source test2.sql;"

我收到以下错误

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '/var/tmp/out.csv FIELDS TERMINATED BY ','
LINES TERMINATED BY '
'' at line 12

如果我只给它,它会起作用out.csv。但我不知道文件存储在哪里。我错过了上述命令中的任何转义序列吗?

此问题已报告给 MySQL。但是有解决办法吗?

4

1 回答 1

1

您可以通过选择查询(如select @query它的框架方式)来简单地进行调试。

On当您有引号时,concat请尝试使用双引号"而不是单引号。'这将避免丢失'您现在拥有的。并且不要忘记在喜欢的\n时候逃跑concat\\n

SET @query = CONCAT("SELECT *
FROM
     `registered_user`
INTO OUTFILE '",@output_location,"'  -- you need to enclose path with quotes
FIELDS TERMINATED BY ','
LINES TERMINATED BY '\\n'");

PREPARE stmt FROM @query;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
于 2019-10-29T06:23:54.463 回答