Recent

Author Topic: Insert/Edit in SQLite  (Read 1492 times)

amedeo

  • New Member
  • *
  • Posts: 39
Insert/Edit in SQLite
« on: December 23, 2019, 04:43:01 pm »
Help! I have no more ideas.

In my application I have a form menu to start (form A), I go to a form with a TDBGrid for the list of records (form B) and from form B I go to Insert a new record or Edit a existing db record (form C).

The problem cames with INSERT (for now).

In the form A I have the SQLite3Connection and SQLTransaction icons to connect DB. (it's ok)
In form B I have a SQLQuery and a  DataSource icon for DBGrid and this works well.
In form C I have a SQLQuery to edit/receive data from Edit fields in the form.

When I confirm INSERT operation I build a string with the data to set SQL.Text
lTextSQL:        insert into Table1 (field1, field2, ...) value (val1, val2, ...)

and after:

with SQLQuery1 do
begin
    Close;
    SQL.Text:=lTextSQL;
    Open;
    Insert;
    Post;
end

[>>> this is the last version of many that I tried<<< ]

in the Open instruction comes out the error    >:(   >:D  I do not understand why, Right now I feel stupid

Someone knows a solution?

However Merry Christmas to all

Amedeo


   

valdir.marcos

  • Hero Member
  • *****
  • Posts: 1106
Re: Insert/Edit in SQLite
« Reply #1 on: December 23, 2019, 06:11:31 pm »
Help! I have no more ideas.

In my application I have a form menu to start (form A), I go to a form with a TDBGrid for the list of records (form B) and from form B I go to Insert a new record or Edit a existing db record (form C).

The problem cames with INSERT (for now).

In the form A I have the SQLite3Connection and SQLTransaction icons to connect DB. (it's ok)
In form B I have a SQLQuery and a  DataSource icon for DBGrid and this works well.
In form C I have a SQLQuery to edit/receive data from Edit fields in the form.

When I confirm INSERT operation I build a string with the data to set SQL.Text
lTextSQL:        insert into Table1 (field1, field2, ...) value (val1, val2, ...)

and after:

with SQLQuery1 do
begin
    Close;
    SQL.Text:=lTextSQL;
    Open;
    Insert;
    Post;
end

[>>> this is the last version of many that I tried<<< ]

in the Open instruction comes out the error    >:(   >:D  I do not understand why, Right now I feel stupid

Someone knows a solution?

However Merry Christmas to all
First, give yourself some time for studying:
https://wiki.freepascal.org/Lazarus_Tutorial
https://wiki.freepascal.org/Lazarus_Database_Tutorial/pt   (Sorry, I could not find an English version)
https://wiki.freepascal.org/SqlDBHowto
https://wiki.freepascal.org/SQLdb_Tutorial0
https://wiki.freepascal.org/SQLdb_Tutorial1
https://wiki.freepascal.org/SQLdb_Tutorial2
https://wiki.freepascal.org/SQLdb_Tutorial3
https://wiki.freepascal.org/SQLdb_Tutorial4
https://wiki.freepascal.org/Category:Databases

Then, if you're using Data-ware Components:
Code: Pascal  [Select][+][-]
  1.   SQLQuery1.Edit;  // or   SQLQuery1.Insert;
  2.   // Do all the changes or add all new data you want.
  3.   SQLQuery1.Post;
  4.   SQLQuery1.ApplyUpdates;
  5.   SQLTransaction1.Commit;
  6.   SQLQuery1.Open;

If not:
Code: Pascal  [Select][+][-]
  1.   SQLTransaction1.Commit;
  2.   SQLQuery1.SQL.Text := InsertTextSQL;
  3.   SQLQuery1.ExecSQL;
  4.   SQLTransaction1.Commit;

amedeo

  • New Member
  • *
  • Posts: 39
Re: Insert/Edit in SQLite
« Reply #2 on: December 23, 2019, 07:41:17 pm »
Thanks for the reding tips, i'll study it
The problem is not resolved, a problem is still that the SQLTransaction, stated in form A is unknown in form C.
Of course I have not yet made all well

valdir.marcos

  • Hero Member
  • *****
  • Posts: 1106
Re: Insert/Edit in SQLite
« Reply #3 on: December 23, 2019, 08:28:35 pm »
Thanks for the reding tips, i'll study it
The problem is not resolved, a problem is still that the SQLTransaction, stated in form A is unknown in form C.
Of course I have not yet made all well
It would be easier to understand what you mean if you attach a small sample project.

JanRoza

  • Hero Member
  • *****
  • Posts: 724
    • http://www.silentwings.nl
Re: Insert/Edit in SQLite
« Reply #4 on: December 23, 2019, 09:20:52 pm »
It sounds as if you did not include unitA in the uses clause of unitC, but all we can do is guess without you showing us some source code.
OS: Windows 11 / Linux Mint 22.1
       Lazarus 4.0 RC FPC 3.2.2
       CodeTyphon 8.70 FPC 3.3.1

mangakissa

  • Hero Member
  • *****
  • Posts: 1131
Re: Insert/Edit in SQLite
« Reply #5 on: December 27, 2019, 09:28:10 am »
Use datamodule. This unit is global and can be use in every form if the name of the datamodule is in uses.
Lazarus 2.06 (64b) / FPC 3.0.4 / Windows 10
stucked on Delphi 10.3.1

 

TinyPortal © 2005-2018