MySQL(5.6)“select * into outfile ..”不创建文件

时间:2015-07-20 23:32:28

标签: mysql csv export file-location

当我在本地主机上使用此命令作为root用户时,它运行时没有问题,但我似乎无法找到实际文件。

SELECT * INTO OUTFILE 'C:\...\tableName.txt' 
FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"'
LINES TERMINATED BY '\r\n'
FROM tableName;

当我尝试再次运行查询时,它表示文件已经创建,即使它显然不是。

编辑:修复了我的查询语法

2 个答案:

答案 0 :(得分:1)

FROM之后有一个不必要的*。您的查询应该更像这样:

SELECT * INTO OUTFILE 'C:\...\tableName.txt'
FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '\r\n'
FROM tableName;

注意:确保mysql有权写入'C:\...\tableName.txt'

至于已经创建的文件错误:

该文件可能是在mysql实际上有权写入的另一个目录中创建的,例如数据目录。这就是为什么您现在已经创建了已经创建了多次运行查询的文件的消息。

从mysql命令行运行show variables like '%dirdata%';,你应该看到类似于:

的输出
mysql> show variables like '%datadir%';
+---------------+-------------------------------------+
| Variable_name | Value                               |
+---------------+-------------------------------------+
| datadir       | c:\wamp\bin\mysql\mysql5.6.17\data\ |
+---------------+-------------------------------------+
1 row in set (0.35 sec)

在Windows中导航到该文件夹​​,你应该在那里找到你的文件。

答案 1 :(得分:1)

SELECT r INTO OUTFILE 'c:\dev\myA2.txt'
  FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"'
  LINES TERMINATED BY '\n'
  FROM a2;

-- leads me to believe it works first time with rowcount output
-- but it does not put file in dev
-- run it again it says error 1086: File 'c:devmyA2.txt' already exists

so that means it wrote it to c:

the default of what mysql query engine has for c: at that time

I did not hunt for it !

the following works great (note the double slashes \\):

SELECT r INTO OUTFILE 'c:\\dev\\drew_so_answers\\myA2.txt'
  FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"'
  LINES TERMINATED BY '\n'
  FROM a2;