unit SQLiteForm;
interface
uses
  SysUtils, Types, UITypes, Classes, Variants, FMX_Types, FMX_Controls, FMX_Forms,
{$IFDEF FPC}
  iPhoneAll, SQLite3db, SQLite,
{$ENDIF}
  FMX_Dialogs, FMX_Layouts, FMX_ListBox;

type
  TFrmSQLite = class(TForm)
    btnCreateTable: TButton;
    btnSelectRecords: TButton;
    ListBox1: TListBox;
    procedure btnCreateTableClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure btnSelectRecordsClick(Sender: TObject);
  private
    {$IFDEF FPC}
    DB: TSQLite;
    DBFileName: String;
    {$ENDIF}
  end;

var
  FrmSQLite: TFrmSQLite;

implementation

{$R *.lfm}

procedure TFrmSQLite.FormCreate(Sender: TObject);
begin
  {$IFDEF FPC}
  DBFileName := ExtractFilePath(ParamStr(0));
  Delete(DBFileName,Length(DBFileName),1); // remove trailing slash
  DBFileName := ExtractFilePath(DBFileName) + 'Documents' + PathDelim;
  DBFileName := DBFileName + 'eBob42.sqlite';
  DB := TSQLite.Create(DBFileName);
  {$ENDIF}
end;

procedure TFrmSQLite.btnCreateTableClick(Sender: TObject);
var
  SQL: String;
  QryResult: Classes.TStringList;
begin
  {$IFDEF FPC}
  QryResult := Classes.TStringList.Create;
  try
    SQL := 'CREATE TABLE Courseware (ID INTEGER PRIMARY KEY, TITLE String);';
    if not DB.Query(SQL, QryResult) then
      ShowMessage('Create: ' + DB.LastErrorMessage);

    SQL := 'INSERT INTO Courseware VALUES(1, "Delphi XE2 Development Essentials");';
    if not DB.Query(SQL, QryResult) then
      ShowMessage('Insert #1: ' + DB.LastErrorMessage);

    SQL := 'INSERT INTO Courseware VALUES(2, "Delphi XE2 DataSnap Development");';
    if not DB.Query(SQL, QryResult) then
      ShowMessage('Insert #2: ' + DB.LastErrorMessage);

    SQL := 'INSERT INTO Courseware VALUES(3, "Delphi XE2 iOS Development");';
    if not DB.Query(SQL, QryResult) then
      ShowMessage('Insert #3: ' + DB.LastErrorMessage);
  finally
    QryResult.Free;
  end;
  {$ENDIF}
end;

procedure TFrmSQLite.btnSelectRecordsClick(Sender: TObject);
var
  SQL: String;
  QryResult: Classes.TStringList;
begin
  {$IFDEF FPC}
  QryResult := Classes.TStringList.Create;
  try
    SQL := 'SELECT * FROM Courseware;';
    if not DB.Query(SQL, QryResult) then
      ShowMessage('Select: ' + DB.LastErrorMessage);

    ListBox1.Items.Text := QryResult.Text
  finally
    QryResult.Free;
  end;
  {$ENDIF}
end;


end.
