Recent

Author Topic: Insert with ZQuery Zeos Access (SOLVED)  (Read 1548 times)

Ame555

  • New Member
  • *
  • Posts: 12
Insert with ZQuery Zeos Access (SOLVED)
« on: October 25, 2020, 12:00:23 am »
Hello community, I'm trying to understand how to do Insert and MySQL with ZQuery and Zeos Access in lazarus, I'm connected to a table that only has two fields "idnumber" auto increment and "number" integer, I added a TEdit in which is placed the number to insert and also I put a TButton that when pressing makes the insert, looking for examples I found this code:

Code: Pascal  [Select][+][-]
  1. procedure TfrmPrueba.Button1Click(Sender: TObject);
  2. begin
  3.   if ZQuery1.Active then
  4.     ZQuery1.Close;
  5.  
  6.    with ZQuery1.SQL do
  7.      begin
  8.        Clear;
  9.        ZQuery1.Insert;
  10.        ZQuery1.FieldByName('numero').AsInteger:=StrToInt(Edit1.Text);
  11.      end;
  12. ZQuery1.Open;
  13. end;  

As those who know about this have already realized that this code is giving an error, the SQL query to do this directly is "insert into numbers (number) values (2);" but I did not find a clear example of how to accommodate this query and I will appreciate your help in this.

I am not very good with English so I apologize for that.

Greetings.

[Edited to add code tags - please see How to use the Forum.]
« Last Edit: October 30, 2020, 12:30:28 am by Ame555 »

miab3

  • Full Member
  • ***
  • Posts: 145
Re: Insert with ZQuery Zeos Access
« Reply #1 on: October 26, 2020, 12:18:43 pm »
If the SQL looks like this: 'select * from table'
then

procedure TfrmPrueba.Button1Click(Sender: TObject);
begin
     ZQuery1.Active:=true;
     ZQuery1.Insert;
     ZQuery1.FieldByName('numero').AsInteger:=StrToInt(Edit1.Text);
     ZQuery1.Post;
end; 

Michał
« Last Edit: October 26, 2020, 09:35:30 pm by miab3 »

Ame555

  • New Member
  • *
  • Posts: 12
Re: Insert with ZQuery Zeos Access
« Reply #2 on: October 28, 2020, 02:03:24 am »
Thank you very much it works well, I realized that it does not help much to have a data saved in the table if at least you do not know when it was saved and what name it has, then I added to my table the datetime fields of type "datetime" and name of type "varchar", for it I modified the code.

Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button1Click(Sender: TObject);
  2. begin
  3.   ZQuery1.Active:=true;
  4.      ZQuery1.Insert;
  5.      ZQuery1.FieldByName('numero').AsInteger:=StrToInt(Edit1.Text);
  6.      ZQuery1.FieldByName('fecha').AsDateTime:=now();
  7.      ZQuery1.FieldByName('nombre').AsString:=Edit1.Name;
  8.      ZQuery1.Post;
  9. end;    
  10.  

When compiling and executing it does not give any error but when checking the table it only saves the numerical value introduced by TEdit1.

Best regards.
« Last Edit: October 28, 2020, 02:05:35 am by Ame555 »

GAN

  • Sr. Member
  • ****
  • Posts: 370
Re: Insert with ZQuery Zeos Access
« Reply #3 on: October 28, 2020, 04:09:30 am »
Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button1Click(Sender: TObject);
  2. begin
  3.      ZQuery1.Open;  //    ZQuery.Active:=true it's the same
  4.      ZQuery1.Insert;
  5.      ZQuery1.FieldByName('numero').AsInteger:=StrToInt(Edit1.Text);
  6.      ZQuery1.FieldByName('fecha').AsDateTime:=Now;                //now();
  7.      ZQuery1.FieldByName('nombre').AsString:=Edit1.Name;  // Error
  8.      ZQuery1.Post;
  9. end;    
Lazarus 2.0.8 FPC 3.0.4 Linux Mint Mate 19.3
Zeos 7̶.̶2̶.̶6̶ 7.1.3a-stable - Sqlite 3.32.3 - LazReport

Ame555

  • New Member
  • *
  • Posts: 12
Re: Insert with ZQuery Zeos Access (SOLVED)
« Reply #4 on: October 30, 2020, 12:29:58 am »
Thank you very much it works very well, thank you miab3 and GAN without your help I would still be lost.

 

TinyPortal © 2005-2018