Recent

Author Topic: How I can insert data in a database by code in SQLite?  (Read 4200 times)

kakarotv5

  • New Member
  • *
  • Posts: 42
How I can insert data in a database by code in SQLite?
« on: November 04, 2007, 04:46:54 pm »
I use SQLite.

I want insert values into the database fields but I do not want use a TDBgrid control or similar Data Controls.

I want to insert values by means of code.

How I do it?

Thanks.
I have installed:
Lazarus-0.9.23-fpc-2.1.5-win32
Lazarus-0.9.23-fpc-2.1.5-cross-arm-wince-win32

I do not have a PDA but I use the Microsoft Windows Mobile 5.0 MSFP Emulator Images and Microsoft Windows Mobile 6 Emulator

kakarotv5

  • New Member
  • *
  • Posts: 42
How I can insert data in a database by code in SQLite?
« Reply #1 on: November 04, 2007, 11:40:39 pm »
I have found the solution:

Code: [Select]

 dsTest.ExecSQL('INSERT INTO TableName(Field1,Field2,...,Fieldn) values'+'('''+Edit1.Text+''','''+Edit2.Text+''','''+ ... +Editn.Text+''')');
 dsTest.RefetchData;
I have installed:
Lazarus-0.9.23-fpc-2.1.5-win32
Lazarus-0.9.23-fpc-2.1.5-cross-arm-wince-win32

I do not have a PDA but I use the Microsoft Windows Mobile 5.0 MSFP Emulator Images and Microsoft Windows Mobile 6 Emulator

windy

  • Full Member
  • ***
  • Posts: 186
How I can insert data in a database by code in SQLite?
« Reply #2 on: November 05, 2007, 10:04:55 am »
just the info I was looking for :)
after using sql prepare statements to use the database name needed, I was then able to use the insert command shown here

LuizAmérico

  • Sr. Member
  • ****
  • Posts: 458
How I can insert data in a database by code in SQLite?
« Reply #3 on: November 08, 2007, 02:04:09 am »
You can also do:
  dsTest.Append;
  dsText.FieldByName('Field1').AsString := Edit1.Text;
  dsText.FieldByName('Field2').AsString := Edit2.Text;
  etc
  dsTest.Post;
  ApplyUpdates;

Some notes:
 - in this way is necessary to have a AutoInc field or the PrimaryKey must be set to the proper field
 - ApplyUpdates can be applied later, eg when the form closes etc
 - is possible to set SaveOnClose and SaveOnRefetch to true that will call applyupdates in the necessary places

 

TinyPortal © 2005-2018