program HelloWorld;
{$MODE Delphi}
uses
cthreads, cmem, SysUtils, Classes, db, sqldb, sqlite3conn, sqlite3dyn;
Var
C : TSQLConnector;
T1 : TSQLTransaction;
// T1, T2 : TSQLTransaction;
// Q1, Q2 : TSQLQuery;
TH1,TH2,TH3,TH4 : TThread;
threadsafe: integer;
procedure CreateConnection();
begin
C := TSQLConnector.Create(nil);
C.ConnectorType := 'SQLite3';
C.DatabaseName := 'mydatabase.db';
// add a general purpose transaction
T1 := TSQLTransaction.Create(C);
T1.Options := [stoUseImplicit];
C.Transaction := T1;
C.ExecuteDirect(
'CREATE TABLE IF NOT EXISTS USERS (U_NAME VARCHAR(255) NOT NULL)'
);
C.ExecuteDirect(
'INSERT OR IGNORE INTO USERS (U_NAME) VALUES (''hello'');'
);
C.ExecuteDirect(
'INSERT OR IGNORE INTO USERS (U_NAME) VALUES (''world'');'
);
// open connection/database
C.Connected := True;
end;
procedure DoQuery();
var
T2 : TSQLTransaction;
Q1 : TSQLQuery;
begin
// Create a second transaction.
T2 := TSQLTransaction.Create(C);
T2.Options := [stoUseImplicit];
// Point to the database instance
T2.Database := C;
// Create a query to return data
Q1 := TSQLQuery.Create(C);
// Point to database and transaction.
Q1.Database := C;
Q1.Transaction := T2;
// Set the SQL select statement
Q1.SQL.Text := 'SELECT * FROM USERS';
try
Q1.Open;
While not Q1.EOF do
begin
Writeln(IntTostr(TThread.CurrentThread.ThreadID) + ' - Username is: ' + Q1.FieldByName('U_NAME').AsString);
Q1.Next
end;
Q1.Close;
except on E: Exception do
Writeln(E.Message);
end;
end;
begin
writeln('Start!');
// create SQLite3 connection + set a basic Transaction
CreateConnection();
threadsafe := sqlite3_threadsafe();
if threadsafe > 0 then
writeln('Your SQLite3 does support multithreading with mode: ' + IntToStr(threadsafe))
else
writeln('Sorry, no support for multithreading');
try
TH1:=TThread.ExecuteInThread(@DoQuery, nil);
TH2:=TThread.ExecuteInThread(@DoQuery, nil);
TH3:=TThread.ExecuteInThread(@DoQuery, nil);
TH4:=TThread.ExecuteInThread(@DoQuery, nil);
Writeln('Main thread done');
TH1.WaitFor;
TH2.WaitFor;
TH3.WaitFor;
TH4.WaitFor;
finally
C.Free;
end;
writeln('End!');
end.