关于在MySQL中创建数据并将其添加到文本文件的问题?

问题描述 投票:0回答:1

我创建了以下过程,该过程使用两个不同的准备好的语句,将一些数据写入具有相同名称的文本文件中。

步骤

delimiter !!
drop procedure if exists copyIntoFile !!
create procedure copyIntoFile()

begin
    declare path varchar(255);
    set path = concat("'C:/ProgramData/MySQL/MySQL Server 5.7/Uploads/", curdate(), ".txt'");

    set @aux1 = concat("select * from movie_modification where modified = true into outfile ", path, 
                        " fields terminated by ';' lines starting by 'Edit: ' terminated by '\n';");
    prepare stmt1 from @aux1;
    execute stmt1;
    deallocate prepare stmt1;

    set @aux2 = concat("select * from movie_modification where modified = false into outfile ", path, 
                        " fields terminated by ';' lines starting by 'New: ' terminated by '\n';");
    prepare stmt2 from @aux2;
    execute stmt2;
    deallocate prepare stmt2;

    delete from movie_modification;
end !!
delimiter ;

当我执行此过程时,语句aux1正确执行,并创建文本文件并将数据写入其中。但是,当执行转到第二条语句(aux2)时,出现以下错误:

错误代码:1086。文件'C:/ ProgramData / MySQL / MySQL Server 5.7 / Uploads / 2020-04-03.txt'已经存在

发生此错误,因为已经在语句aux1中创建了文件。

我该如何修改第二条语句aux2,知道它已经被创建了?

mysql sql prepared-statement
1个回答
0
投票

我认为您不能要求MySQL附加到同一文件,但是您可以将2个查询合并。

set @aux1 = concat("select * from movie_modification where modified = true
                    UNION
                    select * from movie_modification where modified = false
                 into outfile ", path, 
                 " fields terminated by ';' lines starting by 'Edit: ' terminated by '\n';");

prepare stmt1 from @aux1;
execute stmt1;
© www.soinside.com 2019 - 2024. All rights reserved.