简体   繁体   中英

FDQuery Interface to Progress Bar - DELPHI

I am trying to make an interface for my insert Query, this moves a huge number of data.

DataModule2.FDQueryInsertExceed.Close;
 DataModule2.FDQueryInsertExceed.Sql.Clear;
 DataModule2.FDQueryInsertExceed.Sql.Add('INSERT IGNORE INTO tblLogs');
 DataModule2.FDQueryInsertExceed.Sql.Add('SELECT * FROM tbllogs WHERE Datetimelog < 
 date_sub(NOW(), INTERVAL 1 month);');
 DataModule2.FDQueryInsertExceed.ExecSQL;

Dont know where to put the progress bar, and also if you can post here a link with full manual of FD Components.

FireDAC includes the TFDEventAlerter, which allows applications to receive certain notifications from the SQL backend, including some progress notifications. Unfortunately, it does not support MySQL so you are going to have to devise a way of updating your ProgressBar yourself.

The main problem with doing it yourself is that after you call DataModule2.FDQueryInsertExceed.ExecSQL, it will not return until ExecSQL has completed execution, so you can't get any opportunity to update your ProgressBar until then and by the time that you do, the SQL backend has finished working.

So, realistically, the best you can do is to snip your db inserted up into a series of batches and update the ProgressBar after each one. Before you start the batches, you can do a SELECT COUNT(*) which should tell you at least approximately how many rows will be involved overall, so that you can calculate the percentage complete after each batch. I say "approximately" in case new inserts are required while your ExecSQL is executing,

The simplest place to start is by determining how many of your inserts can be executed in a few hundred milliseconds, parameterize your SQL to match that and use a TTimer to start each batch. So your TTimers's OnTimeer wou look something like

TForm1.OnTimer1.OnTimer(Sender : TObject);
var
  S : String;
begin
  S := 'INSERT IGNORE INTO tblLogs SELECT * FROM tbllogs WHERE Datetimelog ... //
  //  complete as necessary to add a starting DateTime and an ending one for the batch
  DataModule2.FDQueryInsertExceed.Sql.Text := S;
  DataModule2.FDQueryInsertExceed.ExecSQL;
  //  Update ProgressBar here
end;

An enhancement (but a step change in difficulty) would be to call DataModule2.FDQueryInsertExceed.ExecSQL in a background thread and, once it has executed, execute code in the main , VCL thread, not in the background thread, to update the ProgressBar. However, exactly how to do that is way beyond the scope of this answer.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM