Forum > Databases

How I can insert data in a database by code in SQLite?

(1/1)

kakarotv5:
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.

kakarotv5:
I have found the solution:


--- Code: ---
 dsTest.ExecSQL('INSERT INTO TableName(Field1,Field2,...,Fieldn) values'+'('''+Edit1.Text+''','''+Edit2.Text+''','''+ ... +Editn.Text+''')');
 dsTest.RefetchData;

--- End code ---

windy:
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:
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

Navigation

[0] Message Index

Go to full version