Jde to třeba nějak takhle:
unit frmMain;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, sqlite3conn, sqldb, DB, FileUtil, Forms, Controls,
Graphics, Dialogs, DBGrids, StdCtrls;
type
{ TMainForm }
TMainForm = class(TForm)
btnCreateTable: TButton;
btnReadData: TButton;
btnInsertRecord: TButton;
conDatabase: TSQLite3Connection;
dsGet: TDatasource;
DBGrid1: TDBGrid;
qrSend: TSQLQuery;
qrGet: TSQLQuery;
trnDatabase: TSQLTransaction;
procedure btnCreateTableClick(Sender: TObject);
procedure btnReadDataClick(Sender: TObject);
procedure btnInsertRecordClick(Sender: TObject);
procedure FormCreate(Sender: TObject);
private
{ private declarations }
public
{ public declarations }
end;
var
MainForm: TMainForm;
implementation
{$R *.lfm}
{ TMainForm }
procedure TMainForm.btnCreateTableClick(Sender: TObject);
begin
try
if not conDatabase.Connected then
conDatabase.Connected := True;
qrSend.SQL.Clear;
qrSend.SQL.Add(
'create table if not exists test (id integer primary key, name varchar(20))');
qrSend.ExecSQL;
trnDatabase.CommitRetaining;
except
trnDatabase.Rollback;
end;
end;
procedure TMainForm.btnReadDataClick(Sender: TObject);
begin
try
if qrGet.Active then
qrGet.Close;
qrGet.SQL.Clear;
qrGet.SQL.Add('Select * from test order by id');
qrGet.Open;
except
end;
end;
procedure TMainForm.btnInsertRecordClick(Sender: TObject);
begin
try
if qrSend.Active then
qrSend.Close;
qrSend.SQL.Clear;
qrSend.SQL.Add('insert into test (id, name) values (1, ''Petr'')');
qrSend.ExecSQL;
trnDatabase.CommitRetaining;
except
on E:Exception do
begin
MessageDlg('Chyba',Format('Data nelze vložit! %s',[#13#10 + E.Message]), mtError, [mbOK],0);
trnDatabase.Rollback;
end;
end;
end;
procedure TMainForm.FormCreate(Sender: TObject);
begin
// tohle neni myslim nutne, ale pokud by bylo treba pouzit dll urcite verze tak tudy vede cesta
SQLiteLibraryName := 'sqlite3.dll';
//naledujici kroky lze naklikat na formu, zde je to jen pro ukazku
//nastavim jmeno databaze (vcetne cesty, ja mam db u exe, tak staci jen nazev souboru)
conDatabase.DatabaseName:= 'data.sdb';
//propojim komponnetu transakce s komponentou pripojeni
conDatabase.Transaction := trnDatabase;
//propojim komponenty dotazu (query) s pripojenim
qrGet.DataBase := conDatabase;
qrSend.DataBase := conDatabase;
//propojim komponentu datasource s dotazem
dsGet.dataset := qrGet;
//propojim DBgrid s datasource
DBGrid1.DataSource := dsGet;
end;
end.