Recent

Author Topic: [SOLVED] SQLite Sum()  (Read 758 times)

Pe3s

  • Hero Member
  • *****
  • Posts: 533
[SOLVED] SQLite Sum()
« on: November 23, 2022, 05:48:04 pm »
Hello, I have a question if it is possible to display the result of the summation in a control other than dbgrid?
Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button10Click(Sender: TObject);
  2. begin
  3.   ZQuery1.SQL.Clear;
  4.   ZQuery1.SQL.Text:= 'SELECT SUM(amount) FROM product';
  5.   ZQuery1.Open;
  6. end;
  7.  
« Last Edit: November 24, 2022, 04:28:29 pm by Pe3s »

Thaddy

  • Hero Member
  • *****
  • Posts: 14210
  • Probably until I exterminate Putin.
Re: SQLite Sum()
« Reply #1 on: November 23, 2022, 06:39:24 pm »
In any text based control, like a memo or even a caption.
(And plz do not use Zeos, use what Freepascal has a default. Zeos is a pain debugging and never has been fit for purpose)
« Last Edit: November 23, 2022, 06:48:23 pm by Thaddy »
Specialize a type, not a var.

Pe3s

  • Hero Member
  • *****
  • Posts: 533
Re: SQLite Sum()
« Reply #2 on: November 23, 2022, 07:17:16 pm »
But when I execute this code it still displays in dbgrid

Pe3s

  • Hero Member
  • *****
  • Posts: 533
Re: SQLite Sum()
« Reply #3 on: November 23, 2022, 08:21:36 pm »
What do I need to change in the code to make it add up the time?
Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button10Click(Sender: TObject);
  2. var
  3.   bm: TBookmark;
  4.   Total: Integer;
  5. begin
  6.   bm := ZQuery1.GetBookmark;
  7.   ZQuery1.DisableControls;
  8.   try
  9.     Total := 0;
  10.     ZQuery1.First;
  11.     while not ZQuery1.Eof do
  12.     begin
  13.       Total := Total + ZQuery1.FieldByName('Wiek').AsInteger;
  14.       ZQuery1.Next;
  15.     end;
  16.     Form1.Caption := IntToStr(Total);
  17.   finally
  18.     ZQuery1.GotoBookmark(bm);
  19.     ZQuery1.EnableControls;
  20.   end;
  21. end;      
  22.  

paweld

  • Hero Member
  • *****
  • Posts: 970
Re: SQLite Sum()
« Reply #4 on: November 23, 2022, 09:09:24 pm »
This is probably because you are using a only one a TZQuery component in the application, which is connected to the grid via datasource.
Place another ZQuery component on the form, such as ZQuery2
Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button10Click(Sender: TObject);
  2. begin
  3.   ZQuery2.SQL.Clear;
  4.   ZQuery2.SQL.Text:= 'SELECT SUM(wiek) suma_lat FROM pracownik';
  5.   ZQuery2.Open;
  6.   try
  7.     Form1.Caption := ZQuery2.FieldByName('suma_lat').AsString;
  8.   finally
  9.     ZQuery2.Close;
  10.   end;
  11. end;      
  12.  
Best regards / Pozdrawiam
paweld

dseligo

  • Hero Member
  • *****
  • Posts: 1196
Re: SQLite Sum()
« Reply #5 on: November 23, 2022, 09:19:46 pm »
Place another ZQuery component on the form, such as ZQuery2

And don't forget to connect it to ZConnection.

Oh, and Zeos works just fine.

Pe3s

  • Hero Member
  • *****
  • Posts: 533
Re: SQLite Sum()
« Reply #6 on: November 24, 2022, 04:28:15 pm »
Thank you :)

 

TinyPortal © 2005-2018