{$mode objfpc}{$H+}
uses sysutils,sqlite3conn,sqldb,db,crt;
const FspecDB = 'd:\test.sqlite'; {select your database (it is created automatically)}
var DataSourceX: TDataSource;
DBConnectionX: TSQLite3Connection;
SQLQueryX: TSQLQuery;
SQLTransactionX: TSQLTransaction;
procedure sql_init(fspecDB: string);
{complete initialization of the SQLite-database in file 'fspecDB'}
begin
DataSourceX:=TDataSource.Create(nil); // create all vars:
DBConnectionX:=TSQLite3Connection.Create(nil);
SQLQueryX:=TSQLQuery.Create(nil);
SQLTransactionX:=TSQLTransaction.Create(nil);
DBConnectionX.Transaction:=SQLTransactionX; // connect all vars:
SQLQueryX.Database:=DBConnectionX;
// SQLQueryX.Transaction:=SQLTransactionX; // happens automatically
DataSourceX.Dataset:=SQLQueryX;
DBConnectionX.Name:='DBConnection';
DBConnectionX.DatabaseName:=fspecDB; // assign filespec
end; {sql_init}
function sql_commit: boolean;
{commits and returns if successful}
begin
write('[commit] ');
try
SQLTransactionX.Commit;
exit(true); {everything was OK}
except
on E: Exception do
begin
writeln('COMMIT-Error: ', E.message);
end;
end; {try}
exit(false); {an Error occured}
end; {sql_commit}
function sql_exec(sql: string; commit: boolean): boolean;
{executes a non-SELECT-SQL-command and returns if successful}
begin
write(sql, ' '); {show SQL-command}
try
SQLQueryX.Close;
SQLQueryX.SQL.Text:=sql;
DBConnectionX.Connected:= True; // establish the connection to the DB
SQLTransactionX.Active:= True; // activate the transaction
SQLQueryX.ExecSQL;
if commit then
if not sql_commit then exit(false); {if an Error occured}
writeln('<OK>');
exit(true); {everything was OK}
except
on E: Exception do
begin
writeln('SQL-Error: ', E.message);
end;
end; {try}
exit(false); {an Error occured}
end; {sql_exec}
procedure sql_close;
{closes the DB-Connection}
begin
write('<A> ');
SQLQueryX.Close; // close SQL-query (makes no problems)
write('<B> ');
try
SQLTransactionX.Active:=False; // close Transaction:
except
on E:Exception do
begin
writeln('ERROR B = ', E.Message);
writeln('B) ', SQLTransactionX.Active);
end;
end; {try}
write('<C> ');
try
DBConnectionX.Connected:=False; // close DB-connection:
except
on E:Exception do
begin
writeln('ERROR C = ', E.Message);
writeln(DBConnectionX.Connected);
end;
end; {try}
writeln('<D>');
end;
procedure sql_done;
{closes the DB-Connection and free's all vars}
begin
sql_close; {close the DB-Connection}
write('<E>'); DataSourceX.Free;
write('<F>'); DBConnectionX.Free;
write('<G>'); SQLQueryX.Free;
write('<H>'); SQLTransactionX.Free; writeln('<I>');
end;
var nr: longint;
ok: boolean;
begin {main}
sql_init(FspecDB); // init SQL-DB
{create 1 table with 1 record: }
ok:=sql_exec('CREATE TABLE IF NOT EXISTS table1 (count INTEGER)',true);
ok:=sql_exec('DELETE FROM table1',true);
ok:=sql_exec('INSERT INTO table1 VALUES (1)',true);
nr:=0;
repeat inc(nr); {update 1 record every 3 seconds: }
write(nr, ') ');
ok:=sql_exec('UPDATE table1 SET count=' + IntToStr(nr),false);
{update here more other records ...}
if sql_commit then writeln('<OK>'); {use only 1 common commit}
sql_close; {allow database-access to others while sleeping}
sleep(3000);
writeln;
until crt.KeyPressed;
sql_done; // closes the DB-Connection and free's all vars
end.