Tutorial: TSQLMonitor Support for SQLite Databases
Go Up to Database and LiveBindings Tutorials
In RAD Studio XE3, TSQLMonitor offers support for SQLite databases.
The following tutorial shows how TSQLMonitor can be used with an SQLite database. The example is a Windows VCL Application that contains 3 controls: TSQLConnection, TSQLMonitor, and TButton. When the button is clicked, a table is created and populated with data, and then a simple select query is executed. Finally, the TraceList of the TSQLMonitor is saved to a file on disk.
Steps
- Select File > New > Windows VCL Application - Delphi.
- Add the following controls to the form: TSQLConnection, TSQLMonitor, and a TButton.
- From the Object Inspector:
- Set the Driver property of the TSQLConnection control to Sqlite.
- Set the SQLConnection property to SQLConnection1.
- Set the Caption property of the TButton control to Execute.
 
- Add the following piece of code to the OnClick event handler of the TButton control:
procedure TForm1.Button1Click(Sender: TObject);
var
  Results: TDataSet;
begin
  // Set the database.
  SQLConnection1.Params.Add('Database=full_path_of_your_sqlite_database');
  try
    // Connect to the database.
    SQLConnection1.Connected := true;
    // Activate the monitor.
    SQLMonitor1.Active := true;
    // Create and populate a table.
    PopulateTable(SQLConnection1);
    SQLConnection1.Execute('SELECT * FROM ExampleTable', nil, Results);
    // Save the trace list to a file on disk.
    SQLMonitor1.SaveToFile('D:\\Log.txt');
  except
    on E: EDatabaseError do
      ShowMessage('Exception raised with message' + E.Message);
  end;
end;
Here is the code for the PopulateTable procedure:
procedure PopulateTable(Connection: TSQLConnection);
var
  Query: String;
begin
  Query := 'CREATE TABLE ExampleTable(id INT, name VARCHAR2(50))';
  try
    Connection.Execute(Query, nil);
    Query := 'INSERT INTO ExampleTable VALUES(1,"test1")';
    Connection.Execute(Query, nil);
    Query := 'INSERT INTO ExampleTable VALUES(2,"test2")';
    Connection.Execute(Query, nil);
    Query := 'INSERT INTO ExampleTable VALUES(3,"test3")';
    Connection.Execute(Query, nil);
  except
    on E: Exception do
      ShowMessage('Exception raised with message: ' + E.Message);
  end;
end;
See Also
- SQLite support in RAD Studio
- Tutorial: Connecting to a SQLite Database from a VCL Application
- Tutorial: Using TSQLMonitor with an ODBC Connection
- Data.SqlExpr.TSQLMonitor
- Data.SqlExpr.TSQLConnection
- FMX.Controls.TButton
- Data.SqlExpr.TSQLMonitor.TraceList
- Object Inspector
- Data.SqlExpr.TSQLConnection.DriverName
- Data.SqlExpr.TSQLMonitor.SQLConnection
- Vcl.Controls.TControl.Caption
- Vcl.StdCtrls.TButton.OnClick
- Data.SqlExpr.TSQLConnection.Execute
- Data.SqlExpr.TSQLConnection.Connected
- Data.SqlExpr.TSQLMonitor.Active
- Data.SqlExpr.TSQLMonitor.SaveToFile