unit Mainform;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, Forms, Controls, StdCtrls, Grids, SQLDB, SQLite3Conn, Info;
type
{ TfrmMainform }
TfrmMainform = class(TForm)
Label1: TLabel;
StringGrid1: TStringGrid;
procedure FormCreate(Sender: TObject);
procedure StringGrid1DblClick(Sender: TObject);
end;
var
frmMainform: TfrmMainform;
implementation
{$R *.lfm}
const
DataFileName = 'Data.sqlite';
{ TfrmMainform }
procedure TfrmMainform.FormCreate(Sender: TObject);
var
Connection: TSQLite3Connection;
Transaction: TSQLTransaction;
Query: TSQLQuery;
i, j: Integer;
begin
Label1.Caption := 'Please double click an item in the grid below:';
StringGrid1.Anchors := [akTop, akBottom, akLeft, akRight];
StringGrid1.FixedCols := 0;
StringGrid1.FixedRows := 0;
StringGrid1.Options := StringGrid1.Options + [goRowSelect];
Constraints.MinHeight := 240;
Constraints.MinWidth := 320;
// Prepare connection
Connection := TSQLite3Connection.Create(nil);
Connection.DatabaseName := ExtractFilePath(ParamStr(0)) + DataFileName;
Transaction := TSQLTransaction.Create(nil);
Query := TSQLQuery.Create(nil);
Connection.Transaction := Transaction;
Query.DataBase := Connection;
// Generate some data
if not(FileExists(Connection.DatabaseName)) then
begin
Connection.CreateDB;
Transaction.StartTransaction;
Query.SQL.Text :=
'CREATE TABLE tblUser ( ' +
'ID INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, ' +
'Name TEXT NOT NULL UNIQUE, ' +
'Email TEXT, ' +
'CHECK (LENGTH(Name) <= 20 AND LENGTH(Email) <= 30) );';
Query.ExecSQL;
Query.SQL.Text :=
'INSERT INTO tblUser (Name, Email) VALUES ' +
'(''' + 'Handoko' + ''', ''' + 'me@myemail.com' + '''), ' +
'(''' + 'Harry' + ''', ''' + 'harry@myemail.com' + '''), ' +
'(''' + 'Susan' + ''', ''' + 'susan@myemail.com' + '''), ' +
'(''' + 'Andy' + ''', ''' + 'andy@myemail.com' + ''');';
Query.ExecSQL;
Transaction.Commit;
end;
// Read data and put them into the grid
Transaction.StartTransaction;
Query.SQL.Text := 'SELECT COUNT(*) FROM tblUser;';
Query.Open;
StringGrid1.RowCount := Query.Fields[0].AsInteger;
Query.Close;
Query.SQL.Text := 'SELECT * FROM tblUser;';
Query.Open;
StringGrid1.ColCount := Query.FieldCount;
i := 0;
while not(Query.EOF) do
begin
Inc(i);
for j := 0 to Query.FieldCount-1 do
StringGrid1.Cells[j, i-1] := Query.Fields[j].AsString;
Query.Next;
end;
Query.Close;
Transaction.Commit;
// Close the connection
Query.Free;
Transaction.Free;
Connection.Free;
end;
procedure TfrmMainform.StringGrid1DblClick(Sender: TObject);
var
ID: string;
begin
ID := StringGrid1.Cells[0, StringGrid1.Row];
if not(ID = '') then
frmInfo.ShowInfo(DataFileName, ID);
end;
end.