unit Unit1;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, DB, SQLDB, SQLite3Conn, Forms, Controls, Graphics, Dialogs,
DBGrids, StdCtrls, JvDBSearchEdit;
type
{ TForm1 }
TForm1 = class(TForm)
DataSource1: TDataSource;
DBGrid1: TDBGrid;
JvDBSearchEdit1: TJvDBSearchEdit;
Memo1: TMemo;
SQLite3Connection1: TSQLite3Connection;
SQLQuery1: TSQLQuery;
SQLTransaction1: TSQLTransaction;
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
procedure JvDBSearchEdit1Change(Sender: TObject);
private
procedure CreateDB;
public
end;
var
Form1: TForm1;
implementation
{$R *.lfm}
{ TForm1 }
procedure TForm1.FormCreate(Sender: TObject);
begin
// Clear/init Memo
Memo1.Clear;
Memo1.Lines.Values['Status'] := 'Entering form creation';
// Setup transaction
SQLTransaction1.Active := false;
SQLTransaction1.Action := caCommitRetaining;
SQLTransaction1.DataBase := SQLite3Connection1;
// Setup connection
SQLite3Connection1.Connected := false;
SQLite3Connection1.LoginPrompt :=false;
SQLite3Connection1.DatabaseName := ':memory:';
SQLite3Connection1.Transaction := SQLTransaction1;
// Setup query
SQLQuery1.DataBase := SQLite3Connection1;
SQLQuery1.Transaction := SQLTransaction1;
// Setup dataset
DataSource1.DataSet := SQLQuery1;
// Setup grid
DBGrid1.DataSource := DataSource1;
// Setup jvdbsearchedit
JvDBSearchEdit1.DataSource := DataSource1;
JvDBSearchEdit1.DataField := 'Name';
// Create the database
CreateDB;
SQLQuery1.Close;
SQLQuery1.SQL.Text:= 'select * from DATA';
SQLQuery1.Open;
Memo1.Lines.Values['RecordCount'] := SQLQuery1.RecordCount.ToString;
Memo1.Lines.Values['Status'] := 'Leaving form creation';
end;
procedure TForm1.FormDestroy(Sender: TObject);
begin
SQLQuery1.Close;
SQLTransaction1.Active := False;
SQLite3Connection1.Connected := False;
end;
procedure TForm1.JvDBSearchEdit1Change(Sender: TObject);
begin
// Memo1.Lines.Values['jvDBSearch.DataResult'] := (Sender as TJvDBSearchEdit).DataResult;
Memo1.Lines.Values['jvDBSearch.DataResult'] := (Sender as TJvDBSearchEdit).Text;
Memo1.Lines.Values['Status'] := 'Updated jvDBSearchEdit';
end;
procedure TForm1.CreateDB;
const
ItemNames: array of string =
(
'Bill','John','Eliza','george','Melinda',
'Sunshine','Omega','Alice','Fabio','Alvin',
'medusa', 'Gary', 'michael'
);
var
ItemName: string;
begin
// Setup a table "DATA" in newyl created database
SQLite3Connection1.ExecuteDirect
(
'CREATE TABLE "DATA"'+
'(' +
' "id" Integer NOT NULL PRIMARY KEY AUTOINCREMENT,' +
' "Name" Char(128) NOT NULL' +
');'
);
// Populate table
for ItemName in ItemNames do
begin
SQLQuery1.SQL.text := 'INSERT INTO ' + '"DATA"' + '(Name) VALUES (:Name);';
SQLQuery1.Params.ParamByName('Name').AsString := ItemName;
SQLQuery1.ExecSQL;
end;
// Make database magic happen
SQLTransaction1.Commit;
end;
end.