如何将 SQL 中的数据附加到现有文件

发布于 2024-10-13 00:18:58 字数 193 浏览 3 评论 0原文

SQL 可以使用 INTO OUTFILE 选项将数据转储到文件中,例如

SELECT * from FIshReport INTO OUTFILE './FishyFile'

问题是,只有当文件之前不存在时才允许使用此命令。它创建文件,然后输入数据。 那么,有没有办法以这种方式将数据附加到文件中呢?

SQL has the option to dump data into a file, using the INTO OUTFILE option, for exmaple

SELECT * from FIshReport INTO OUTFILE './FishyFile'

The problem is, this command is only allowed if the file didn't exist before it. It creates the file and then enters the data.
So, is there any way to append data to a file this way?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

︶ ̄淡然 2024-10-20 00:18:58

正如关于 SELECT 语法的 MySQL 页面所示:

http://dev.mysql。 com/doc/refman/5.0/en/select.html

另一种方法是从 MySQL 客户端发出 SELECT:

However, if the MySQL client software is installed on the remote machine,
you can instead use a client command such as mysql -e "SELECT ..." > file_name 
to generate the file on the client host. 

在您的情况下,将修改为:

mysql -e "SELECT * from FishReport" >> file_name

以便您只需附加到文件。

从您的 Tcl 脚本中,您可以简单地将其作为执行命令发出:

http:// www.tcl.tk/man/tcl/tutorial/Tcl26.html

As the MySQL page on SELECT syntax suggests:

http://dev.mysql.com/doc/refman/5.0/en/select.html

the alternative to this is to issue the SELECT from the MySQL client:

However, if the MySQL client software is installed on the remote machine,
you can instead use a client command such as mysql -e "SELECT ..." > file_name 
to generate the file on the client host. 

which, in your case, would be modified to be:

mysql -e "SELECT * from FishReport" >> file_name

so that you simply append to the file.

From your Tcl script, you could simply issue this as an exec command:

http://www.tcl.tk/man/tcl/tutorial/Tcl26.html

江南月 2024-10-20 00:18:58

我认为出于安全原因,MySQL 不允许将数据附加到现有文件或覆盖现有文件。
解决方法是将结果保存在单独的文件中,然后附加使用文件 IO。

I think MySQL does not allow appending data to an existing file or overwriting an existing file for security reasons.
A work around could be to save resuts in seperate files and then append the using file IO.

残月升风 2024-10-20 00:18:58

您始终可以使用 >> 将 SQL 脚本的输出附加到文件中

例如(对于 Sybase):

isql < script.sql >> outputfile.out

我无法告诉您 MySQL 的等效项是什么,但原则应该是相同的。

当然,输出将全部转到一个文件,因此如果您的 SQL 脚本将各种 SQL 选择输出到不同的输出文件,那么您需要将脚本拆分。

You could always append the output from your SQL script to a file using >>

For example (for Sybase):

isql < script.sql >> outputfile.out

I can't tell you what the equivalent is for MySQL but the principle should be the same.

Of course output will all go to one file so if your SQL script is outputting various SQL selects to different output files then you'd need to split the script up.

旧城空念 2024-10-20 00:18:58

您可以将其添加到变量中。然后使用带有 UNION 的 SELECT。

declare t varchar(100);

set @myvar = concat('
    select *  INTO OUTFILE \'',file,'\'
    from (
       select \'',t,'\'
       union all
       SELECT col from tbl where x      
    ) a' 
);
PREPARE stmt1 FROM @myvar;
EXECUTE stmt1;
Deallocate prepare stmt1;

You could just add it to a variable. Then use a SELECT with UNION.

declare t varchar(100);

set @myvar = concat('
    select *  INTO OUTFILE \'',file,'\'
    from (
       select \'',t,'\'
       union all
       SELECT col from tbl where x      
    ) a' 
);
PREPARE stmt1 FROM @myvar;
EXECUTE stmt1;
Deallocate prepare stmt1;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文