Tutorial: Connecting to a SQLite Database (Delphi) - RAD Studio
[Link]
Show: Delphi C++ Display Preferences
Tutorial: Connecting to a SQLite Database (Delphi)
From RAD Studio Go Up to Tutorials#SQLite and SQLMonitor This tutorial is a simple Delphi VCL Form application that shows how to establish a connection to a SQLite database and execute a simple query. In this example an SQLite database is used, but you can use any SQLite database available to you. The database used in this example is located at D:\test.s3db and has one table named example.
Steps
1. Select File > New > VCL Forms Application - Delphi. 2. Add the following components to the form: Two TButton controls; from the Object Inspector, set the Name properties of the buttons to executeButton and connectButton, and their Caption properties, to Execute and Connect, respectively. A TSQLConnection control; from the Object Inspector, set the Driver property to Sqlite. A TMemo control; from the Object Inspector, set the Name to outputMemo. At this point the form should look like this:
3. Add the following code to the OnClick event handler for the connectButton.
procedure [Link](Sender: TObject); begin // Set the path of your database file. // Replace "full_path_to_your_database_file" with the absolute path // to your SQLite database file. [Link]('Database=full_path_to_your_database_file'); try
1 of 3
12/23/2013 5:04 PM
Tutorial: Connecting to a SQLite Database (Delphi) - RAD Studio
[Link]
// Establish the connection. [Link] := true; [Link] := true; [Link] := 'Connection established!'; except on E: EDatabaseError do ShowMessage('Exception raised with message' + [Link]); end; end;
4. Add the following code to the OnClick event handler for the executeButton.
procedure [Link](Sender: TObject); var results: TDataSet; query: String; begin [Link]; // A random query query := 'SELECT * FROM example;'; try // Execute the query on the database. [Link](query, nil, results); except on E: Exception do [Link] := 'Exception raised with message: ' + [Link]; end; // Show the results of the query in a TMemo control. ShowSelectResults(results); end;
Here is the code for the ShowSelectResults procedure:
procedure [Link](results: TDataSet); var names: TStringList; i: Integer; currentField: TField; currentLine: string; begin if not [Link] then begin [Link]; names := [Link]; [Link](names); while not [Link] do begin currentLine := ''; for i := 0 to [Link] - 1 do begin currentField := [Link](names[i]); currentLine := currentLine + ' ' + [Link]; end; [Link](currentLine); [Link]; end; end; end;
2 of 3
12/23/2013 5:04 PM
Tutorial: Connecting to a SQLite Database (Delphi) - RAD Studio
[Link]
Uses
[Link] [Link] [Link] [Link] [Link] [Link] [Link]
See Also
SQLite support in RAD Studio Tutorial: TSQLMonitor Support for SQLite Databases Setting Up TSQLConnection Object Inspector [Link] [Link] Retrieved from "[Link] /[Link]?title=Tutorial:_Connecting_to_a_SQLite_Database_(Delphi)&oldid=212404" Categories: Delphi XE3 This page was last modified on 25 October 2013, at 17:58. Help Feedback
3 of 3
12/23/2013 5:04 PM