First add on the blank form: tfdconnection, Tfdphyssqlitedriverlink, Tfdguixwaitcursor, Tfdquery, Tdatasource, TDBGrid (and associated at design time).
You can also copy the contents of the text box below and paste it directly onto the form to quickly complete the add-in process:
Object Dbgrid1:tdbgrid left = all Top = Width = 361 Height = 329 DataSource = DataSource1 TabOrder = 0 Titlefont . Charset = Default_charset Titlefont.color = Clwindowtext Titlefont.height = -11 titlefont.name = ' Tahoma ' titlefont.st YLE = []endobject fdconnection1:tfdconnection left = Top = 24endobject Fdphyssqlitedriverlink1:tfdphyssqlitedriverl Ink left = 143 top = 24endobject fdguixwaitcursor1:tfdguixwaitcursor Provider = ' Forms ' left = 260 top = 24endobject Fdquery1:tfdquery Connection = FDConnection1 left = 344 Top = 24endobject Datasource1:tdatasource DataSet = fdquery 1 left = 420 top = 24endobject Button1:tbutton left = top = Width = Height = Caption = ' Button1 ' Tab Order = 1 OnClick = Button1clickendobject Button2:tbutton left = Top = 136 Width = Height = Caption = ' Bu Tton2 ' TabOrder = 2 OnClick = Button2clickendobject Button3:tbutton left = Top = 192 Width = Height = Ca ption = ' Button3 ' TabORder = 3 OnClick = Button3clickendobject Button4:tbutton left = Top = Width = Height = + Caption = ' but Ton4 ' TabOrder = 4 OnClick = Button4clickendend
Code:
{set up}
procedure TForm1.FormCreate (Sender: TObject);
const
dbPath = ‘C: \ Temp \ SQLiteTest.sdb’;
strTable = ‘CREATE TABLE MyTable (Id integer PRIMARY KEY AUTOINCREMENT, Name string (10), Age byte)’; // Id, Name, Age three fields
// integer PRIMARY KEY AUTOINCREMENT: Increment field
begin
if FileExists (dbPath) then DeleteFile (dbPath);
FDConnection1.ConnectionString: = ‘DriverID = SQLite; Database =‘ + dbPath;
FDConnection1.ExecSQL (strTable);
FDQuery1.Open (‘SELECT * FROM MyTable’);
end;
{insert}
procedure TForm1.Button1Click (Sender: TObject);
const
strInsert = ‘INSERT INTO MyTable (Name, Age) VALUES (: name,: age)’;
begin
FDConnection1.ExecSQL (strInsert, [‘AAA‘, 11]);
FDConnection1.ExecSQL (strInsert, [‘BBB‘, 22]);
FDConnection1.ExecSQL (strInsert, [‘CCC’, 33]);
FDConnection1.ExecSQL (strInsert, [‘DDD’, 44]);
FDConnection1.ExecSQL (strInsert, [‘EEE’, 55]);
FDQuery1.Refresh;
end;
{Update}
procedure TForm1.Button2Click (Sender: TObject);
begin
FDConnection1.ExecSQL (‘UPDATE MyTable SET Age =: a WHERE Name =: n’, [Random (100), ‘AAA’]);
FDQuery1.Refresh;
end;
{delete}
procedure TForm1.Button3Click (Sender: TObject);
begin
FDConnection1.ExecSQL (‘DELETE FROM MyTable WHERE Age> 33’);
FDQuery1.Refresh;
end;
{Query the first result that meets the conditions}
procedure TForm1.Button4Click (Sender: TObject);
var
V: Variant;
begin
V: = FDConnection1.ExecSQLScalar (‘SELECT Age FROM MyTable WHERE Name =: x’, [‘BBB‘]);
ShowMessage (V);
end;