Posted on 6,861 Comments

FireDAC Memory Table

FireDAC FDMemTable is an in-memory dataset component that allows the storage of data in table format in local memory. It does not require any database connection so it is very handy on many occasions.

In order to use it, just the fields have to be defined and ready for actions like add, edit, delete records. It can be used for storing data from external files like csv for temporary manipulations.

A. Create fields in memory table

Var
FDMemTable1 : TFDMemTable;

begin;
with FDMemTable1.FieldDefs do
begin
Add(‘EmpID’, ftInteger, 0, True);
Add(‘EmpName’, ftString, 50, False);
end;

FDMemTable1.CreateDataset;
FDMemTable1.Open;
end;

B. Add/Edit data in memory table

with FDMemTable1 do
begin
Append;
//Edit;
Fields[0].AsInteger := 1;
Fields[1].AsString := ‘Surendra’;
Post;
end;

C. Deleting data from memory table

FDMemTable1.Delete;

D. Delete all records from memory table

FDMemTable1.EmptyDataset;

E. Sorting Data
Use Index for sorting. So we can use property Indexes, IndexDefs or IndexFieldNames.

FDMemTable1.IndexFieldNames := ‘AcctID ASC’;

Or

FDMemTable1.IndexDefs.Add(‘Index1’, ‘AcctID’, [ixPrimary]);
FDMemTable1.IndexName := ‘Index1’;

Or

F. Find Data
Just like in other dataset use Locate, Lookup and Find methods to find a specific record.

Locate
FDMemTable1.Locate(‘AcctID’, 1, []);

Or

FDMemTable1.Locate(‘Name’, ’Surendra’, [loCaseInsensitive]);

Lookup

var
sName: string;
sName := FDMemTable1.Lookup(‘AcctID’, 1, ‘Name’);

FindKey
Findkey will work if only any Index is applied

FDMemTable1.FindKey([1]);
FDMemTable1.FindFirst;
FDMemTable1.FindLast;
FDMemTable1.FindNext;
FDMemTable1.FindPrev;

G. Filtering Data

FDMemTable1.Filtered := False;
FDMemTable1.Filter := ‘AcctID=1’;
FDMemTable1.Filtered := True;

H. Copy data from one FDMemTable to another

The most simple way to copy the structure and data from a TdataSet or a TFDMemTable to another TFDMemTable is to use the CopyDataSet method:

FDMemTable1.CopyDataSet(SourceFDMemTable2, [coStructure, coRestart, coAppend]);
FDMemTable1.CopyDataSet(SourceDataset2, [coStructure, coRestart, coAppend]);

Data property

// UniDirectional must be false

FDQuery1.FetchOptions.Undirectional := False;
FDQuery1.Sql.Text := ‘select * from Account’;
FDQuery1.Open;
FDMemTable1.Data := FDQuery1.Data;
FDMemTable1.First;
while not FDMemTable1.Eof do
begin
FDMemTable1.Edit;
// other codes
FDMemTable1.Post;
FDMemTable1.Next;
end;

I. Difference between CopyDataset and Data property

  1. CopyDataSet can copy from a non-FireDAC dataset but Data property allows you to copy data only from a FireDAC dataset.
  2. CopyDataSet works through TDataSet firing appropriate events but Data property works through no events are fired.
  3. CopyDataSet copies only current field values but Data property copies all record field versions and preserves the row state (inserted, deleted, updated, or unchanged).

Assigning values to Data is much faster than CopyDataSet.

Optimize the performance of TFDMemTable
Set the following properties LogChanges, FetchOptions, ResourceOptions, and UpdateOptions, DisableControls.

FDMemTable1.LogChanges := False;
FDMemTable1.ResourceOptions.SilentMode := True;
FDMemTable1.UpdateOptions.LockMode := lmNone;
FDMemTable1.UpdateOptions.LockPoint := lpDeferred;
FDMemTable1.UpdateOptions.FetchGeneratorsPoint := gpImmediate;
FDMemTable1.DisableControls;
FDMemTable1.BeginBatch;
try
for i := 1 to 100 do begin
FDMemTable1.Append;
//other codes
FDMemTable1.Post;
end;
finally
FDMemTable1.EndBatch;
FDMemTable1.EnableControls;
end;

Posted on 42 Comments

I am in love with Delphi

At the very beginning, when I entered into IT field in 1981, I started learning COBOL and C programming languages. Then after a few years, I learned dBase III and started writing codes to develop business application software. For a decade I developed software in dBase and also used CLIPPER compile to create executable files. For some companies, I used Paradox database as well. Those DOS programs worked great and customers were all happy using my software. The software I developed POS, Accounting, Tour and Travel Handling, Movie Theatre Ticketing System and many more.

In 95, I started converting my existing CLIPPER software to CA Visual Object for Window Operating System but I was not happy with Visual Object. So I switched to Delphi and started the migration. Since then I never looked back. I used Delphi for all my projects as it the best tool for database software development.

I love delphi because:

  1. Most powerful native compiler
  2. Delphi created programs executes very fast
  3. Component based and many third party components are available to make programmers life easy.
  4. Delphi is know as the best programming tool in all aspect. It was created by Anders Hejlsberg. Later Microsoft hired him to create C# because Delphi is the best.
  5. Productivity of the programmers 5 times high
  6. few programmers can product very larg software
  7. Delphi from the very begining has concept of low-code
  8. Ease of code maintenance due to readabilty
  9. One language for all cross-plateform applications. Windows, iOS, MAC, Linux, Android
Posted on 976 Comments

Deploying Delphi Application

Delphi applications can be deployed in two different ways:

  1. Standalone Compiled EXE. This is useful for small application which is easily distributed. This is the default setting of Delphi Compiler which creates a single application EXE including all required packages.
  2. Compiled EXE with runtime packages. This is done by checking the build with run time package check box in the project option. Once the check box is checked, it compiles the program and makes it very small size EXE. It does not include required packages in the EXE so the size is small. To make it runnable, you have to distribute EXE along with runtime packages. This is especially useful when you have many Delphi applications running so that they all can share the common packages.