SQLite: Tutorial Project


Preparations

  1. If you haven't done so already, create a new folder \DelphiLand.
  2. If you haven't done so already, create directory SQLiteGeo "under" \DelphiLand.
  3. Next, download sqlitegeo.zip and unzip it to this directory.

A closer look

  1. Start Delphi and open sqlitegeo.dpr.
  2. In the the menu, select Project / Options...
  3. In the dialog that appears, select Delphi Compiler / Output directory
    Clear this option and click OK
    That way, you avoid the need to enter a hardwired full path when setting FDConnection.Params.Database (see later).
  4. Open Delphi's File menu and click Save All.
  5. The structure of the table Countries of database Geo.sqlite is as follows:

    Field name Type
    name varchar(50)
    code varchar(10)
    population numeric
    currency varchar(25)
    curcode varchar(10)
  6. In Design Mode, let's have a look at the components:

    • FDConnection1 is a FireDac connection: it defines the connection parameters, establishes a connection to a DBMS (DataBase Management System) and manages the associated datasets.
    • qCountries is a TDFQuery (FireDAC Query). Its property Connection is set to FDConnection1 and its property SQL is set to
                      select * from countries
      ...meaning: select all fields from table countries
    • dsCountries is a TDataSource, the interface between a dataset component and data-aware controls. Its property DataSet is set to qCountries.
    • gridCountries is a TDBGrid (DataBase Grid) that is used to visualize the contents of the table Countries. Its property DataSource is set to dsCountries.
    • DBNavigator1 is used to move through the data in the dataset and perform operations on the data, such as inserting a record or posting a record. Its property DataSource is set to dsCountries.
    • FDGUIxWaitCursor1 is a component that is needed for every FireDac application.
  7. Since most work has been done in Design Mode, the source code for this project is really minimal:
    procedure TForm1.FormShow(Sender: TObject);
    begin
      FDConnection1.Close;
      FDConnection1.Params.Clear;
      FDConnection1.Params.Add('DriverID=SQLite');
      FDConnection1.Params.Add('Database=geo.sqlite');
      FDConnection1.Open;
      qCountries.Open;
    end;
    
    procedure TForm1.FormDestroy(Sender: TObject);
    begin
      qCountries.Close;
      FDConnection1.Close;
    end;

Table of contents

1. Intro to SQLite
2. Tutorial project
3. SQL statements
4. The WHERE clause
5. Browser for SQLite
 

Crash Course Delphi  Database tutorials  FAQ  Tips  Source Code  Downloads  Links