Professional Documents
Culture Documents
Connecting To A SQLite Database (Delphi) - RAD Studio
Connecting To A SQLite Database (Delphi) - RAD Studio
http://docwiki.embarcadero.com/RADStudio/XE5/en/Tutorial:_Connectin...
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 TForm3.connectButtonClick(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. SQLConnection1.Params.Add('Database=full_path_to_your_database_file'); try
1 of 3
12/23/2013 5:04 PM
http://docwiki.embarcadero.com/RADStudio/XE5/en/Tutorial:_Connectin...
// Establish the connection. SQLConnection1.Connected := true; executeButton.Enabled := true; outputMemo.Text := 'Connection established!'; except on E: EDatabaseError do ShowMessage('Exception raised with message' + E.Message); end; end;
4. Add the following code to the OnClick event handler for the executeButton.
procedure TForm3.executeButtonClick(Sender: TObject); var results: TDataSet; query: String; begin outputMemo.Clear; // A random query query := 'SELECT * FROM example;'; try // Execute the query on the database. SQLConnection1.Execute(query, nil, results); except on E: Exception do outputMemo.Text := 'Exception raised with message: ' + E.Message; end; // Show the results of the query in a TMemo control. ShowSelectResults(results); end;
2 of 3
12/23/2013 5:04 PM
http://docwiki.embarcadero.com/RADStudio/XE5/en/Tutorial:_Connectin...
Uses
Data.DB.TDataSet Data.DB.TField System.Classes.TStringList Data.SqlExpr.TSQLConnection Vcl.StdCtrls.TButton Vcl.StdCtrls.TMemo Vcl.Controls.TControl.OnClick
See Also
SQLite support in RAD Studio Tutorial: TSQLMonitor Support for SQLite Databases Setting Up TSQLConnection Object Inspector System.Classes.TComponent.Name Vcl.StdCtrls.TButton.Caption Retrieved from "http://docwiki.embarcadero.com/RADStudio/XE5/e /index.php?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