unit ArrayDMLThreadUnit;

interface

uses
  System.SysUtils, System.Classes, FireDAC.Stan.Intf, FireDAC.Stan.Option,
  FireDAC.Stan.Error, FireDAC.UI.Intf, FireDAC.Phys.Intf, FireDAC.Stan.Def,
  FireDAC.Phys, FireDAC.Comp.Client, FireDAC.Phys.MySQLDef, FireDAC.Phys.MySQL,
  FireDAC.Stan.Param, FireDAC.DatS, FireDAC.DApt.Intf, FireDAC.Stan.Async,
  FireDAC.DApt, Data.DB, FireDAC.Comp.DataSet,vcl.dialogs;



type
  TArrayDMLThread = class(TThread)
  FDConnectionDML:TFDConnection;
  FDQueryDML:TFDQuery;
  procedure UpdateLabel;

  private
    { Private declarations }
  protected
    procedure Execute; override;
  end;

 Var ArraySize:Integer;


implementation

{ 
  Important: Methods and properties of objects in visual components can only be
  used in a method called using Synchronize, for example,

      Synchronize(UpdateCaption);  

  and UpdateCaption could look like,

    procedure TArrayDML.UpdateCaption;
    begin
      Form1.Caption := 'Updated in a thread';
    end; 
    
    or 
    
    Synchronize( 
      procedure 
      begin
        Form1.Caption := 'Updated in thread via an anonymous method' 
      end
      )
    );

  where an anonymous method is passed.
  
  Similarly, the developer can call the Queue method with similar parameters as 
  above, instead passing another TThread class as the first parameter, putting
  the calling thread in a queue with the other thread.
    
}

uses DataModule, MainUnit;

{ TArrayDML }

procedure TArrayDMLThread.Execute;

var i:integer;

begin

//Создаем соединение
   FDConnectionDML:=TFDConnection.Create(nil);
   FDConnectionDML.ConnectionDefName:='MyConnDefName';
   FDConnectionDML.Connected:=true;
//if FDConnectionDML.Connected then showmessage('Connected');


//Создаем FDQuery

   FDQueryDML:=TFDQuery.Create(nil);
   FDQueryDML.Connection:=FDConnectionDML;

//Чистим базу
   FDQueryDML.SQL.Clear;
   FDQueryDML.SQL.Add('delete from users');
   FDQueryDML.ExecSQL;

//Чистим параметры

   FDQueryDML.Params.Clear;

//Заполняем
FDQueryDML.SQL.Text:=(

   'INSERT INTO `aphina_db`.`users` (`idUsers`,`Name`, `Family`, `Role`, `Login`, `Password`, `E-mail`, `Balance`)'+
   'VALUES (:idUsers,:name, :family, :role, :login, :password, :email, :balance);'

   );


   FDQueryDML.Params.ArraySize:=ArraySize;

   for i := 0 to ArraySize-1 do

     begin
      //Так
      FDQueryDML.ParamByName('idUsers').AsIntegers[i]:=i+1;
      FDQueryDML.ParamByName('name').AsStrings[i]:='name';
      FDQueryDML.ParamByName('family').AsStrings[i]:='family';
      FDQueryDML.ParamByName('role').AsStrings[i]:='role';
      FDQueryDML.ParamByName('login').AsStrings[i]:='login';
      FDQueryDML.ParamByName('password').AsStrings[i]:='password';
      FDQueryDML.ParamByName('email').AsStrings[i]:='email';
      FDQueryDML.ParamByName('balance').AsFloats[i]:=2000.12;



      { //Либо так...
      FDQueryDML.Params[0].AsIntegers[i]:=i+1;
      FDQueryDML.Params[1].AsStrings[i]:='someName';
      FDQueryDML.Params[2].AsStrings[i]:='someFamily';
      FDQueryDML.Params[3].AsStrings[i]:='someRole';
      FDQueryDML.Params[4].AsStrings[i]:='someLogin';
      FDQueryDML.Params[5].AsStrings[i]:='somePassword';
      FDQueryDML.Params[6].AsStrings[i]:='someE-mail';
      FDQueryDML.Params[7].AsFloats[i]:=10.12;

      }

     end;

FDQueryDML.ResourceOptions.ArrayDMLSize:=2147483647;

FDQueryDML.Execute(ArraySize,0);

FDQueryDML.Free;
FDConnectionDML.Free;

Synchronize(UpdateLabel);
  { Place thread code here }
end;

{ TMyClass }


procedure TArrayDMLThread.UpdateLabel;
begin
MainUnit.Form1.StatusBar1.Panels[0].Text:='Записи успешно добавлены в таблицу';
end;

end.
