MySQLでテキストファイルにデータを作成し、追加を通報しますか?

AlfaCoding:

私は2つの異なるプリペアドステートメントを使用していることは、次の手順を作成して、同じ名前を持つテキストファイルにいくつかのデータを書き込みます。

手順

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サーバ5.7 /アップロード/ 2020-04-03.txt' がすでに存在しています

ファイルが既に文で作成されているため、このエラーが発生しますaux1

どのように私は2番目の文変更することができaux2、それがすでに作成されていたファイルを知っているの?

RiggsFolly:

私はあなたが2つのクエリUNIONを可能性があり、同じファイルに追加するMySQLを頼むことができますが、と思ういけません。

あなたは追加することができます'Edit: 'し、'New: 'クエリに、したがって、あなたは1つのクエリにこれを減らすためにUNIONを使用することができます。

あなたがしたいすべての列を指定するためにあなたのクエリを変更する必要がありSELECT 'Edit: ',*文句を言わない仕事を、しかし、ここで提案です。

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 @aux = concat("select 'Edit: ', col1,col2 from movie_modification where modified = true
                    UNION ALL
                    select 'New: ', col1,col2 from movie_modification where modified = false
                 into outfile ", path, 
                 " fields terminated by ';' lines terminated by '\n';");

    prepare stmt from @aux;
    execute stmt;

    deallocate prepare stmt;
    delete from movie_modification;
end !!
delimiter ;

おすすめ

転載: http://10.200.1.11:23101/article/api/json?id=402200&siteId=1