Show: Delphi C++
Display Preferences

Tutorial: TSQLMonitor Support for SQLite Databases

From RAD Studio
Jump to: navigation, search

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 VCL Form Applications 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

  1. Select File > New > VCL Forms Application - Delphi.
  2. Add the following controls to the form: TSQLConnection, TSQLMonitor, and a TButton.
  3. From the Object Inspector:
  4. 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

Personal tools
RAD Studio 10.2 Tokyo
In other languages
Previous Versions
Assistance