Lazarus

Programming => Databases => Topic started by: kakarotv5 on November 04, 2007, 04:46:54 pm

Title: How I can insert data in a database by code in SQLite?
Post by: kakarotv5 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.
Title: How I can insert data in a database by code in SQLite?
Post by: kakarotv5 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;
Title: How I can insert data in a database by code in SQLite?
Post by: windy 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
Title: How I can insert data in a database by code in SQLite?
Post by: LuizAmérico 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