Site icon Embarcadero RAD Studio, Delphi, & C++Builder Blogs

Learn How To Work With Powerful In Memory DataSets Using Local SQL In Delphi

The sample shows in a nice way how to use the TFDLocalSQL component to execute SQL commands on in-memory database, represented by several TFDMemTables.

This sample uses a TFDConnection to connect to the SQLite in-memory database (with empty Database parameter value). The TFDLocalSQL component is for the Local SQL engine. The TFDMemTable components are used to load and show the Brands and Models information.

The Brands and Models TFDMemTables are in-memory tables loaded on the form creation. The FieldDefs is filled on code, so there are no external tables or database. The Active property is set to True to open the dataset. The LocalSQL property is set to FDLocalSQL1 to register the dataset with the Local SQL engine.

TFDLocalSQL serves as a central hub to the Local SQL engine and provides the registered datasets. The Connection property is set to FDConnection1 SQLite connection, which will be used to query the datasets.

The TFDQuery connects to FDConnection1 and executes a query over the TFDLocalSQL connection and shows the result of the SQL query on the TDBGrid.

Location

You can find the In-memory DB sample project at:

How to Use the Sample

  1. Navigate to the location given above and open:
    • Delphi: InMemDB.dproj
  2. Press F9 or choose Run > Run.
  3. Move to the different tabs and navigate through the records with the TDBNavigators.

Implementation

The sample is divided on tabs. The TFDMemTables are defined and filled with data by code.

MemTab1

It shows on the TDBGrid the result of the Brands TFDMemTable.

To show the content from the TFDMemTable on the TDBGrid it uses the DataSource1 TDataSource. The DataSet property of the DataSource1 specifies Brands and the TDBGrid has DataSource1 as the DataSource property.

MemTab2

It shows on the TDBGrid the result of the Models TFDMemTable.

To show the content from the TFDMemTable on the TDBGrid it uses the DataSource2 TDataSource. The DataSet property of the DataSource2 specifies Models and the TDBGrid has DataSource2 as the DataSource property.

Query

It shows on the TDBGrid the result on the FDQuery1 TFDQuerySelect * from Brands B left join Models M on B.Code = M.Brand

To show the content from the TFDQuery on the TDBGrid it uses the DataSource3 TDataSource. The DataSet property of the DataSource3 specifies FDQuery1 and the TDBGrid has DataSource3 as the DataSource property.

Please refer to the ling below for more information:

http://docwiki.embarcadero.com/CodeExamples/Sydney/en/FireDAC.TFDLocalSQL_InMemDB_Sample

Head over and check out the full source for the Delphi FireDAC local SQL in memory dataset sample app over on GitHub.

Exit mobile version