The Podcast at Delphi.org

The Podcast about the Delphi programming language, tools, news and community.

6 2017

Simple Mobile FireDAC App

by Jim McKeeth

The DocWiki has a great mobile tutorial on building a mobile To Do app with FireDAC and SQLite. I find I usually build it a little differently though, so I thought I would share my code here.

Go ahead and lay the UI out the same, and put down most of the same FireDAC components, with the visual LiveBindings. I love how it uses the LiveBindings wizard to create the data source. What I change is the code in the event handlers, but what you don’t need is the Insert and Delete FDQuery components. While that code all works, it uses an older model that doesn’t take full advantage of the amazing features of FireDAC.

So your form will look something like this . . .

FireDAC-ToDo-1

Add an implementation uses clause

[code language=”Delphi”]uses FMX.DialogService.Async, IOUtils;[/code]

And then your addButtonClick event handler will look like this . . .

[code language=”Delphi”]procedure TForm42.addButtonClick(Sender: TObject); begin TDialogServiceAsync.InputQuery(‘New item’,[‘Name’], [’’], procedure (const AResult: TModalResult; const AValues: array of string) begin if (AResult = mrOK) and (Length(AValues) > 0) and (Length(Trim(AValues[0])) > 0) then begin FDQuery1.InsertRecord([AValues[0]]); end; UpdateDeleteButton; end); end;[/code]

You’ll notice a few changes. First of all, I used an anonymous method for the async callback. Also, instead of using an entirely different insert query, I just call InsertRecord on the existing FDQuery1, passing in the value. For simple tables like this one this is so much easier. This really simplified the code in my opinion. Also instead of spreading the code to update the Delete button’s visibility all over the place I used a procedure called UpdateDeleteButton. Here is the rest of the code with a few comments.

[code language=”Delphi”]procedure TForm42.deleteButtonClick(Sender: TObject); begin // Again we use the built in Delete method instead of a separate // Delete query. This just deletes the currently active record. FDQuery1.Delete; UpdateDeleteButton; end;

procedure TForm42.FDConnection1AfterConnect(Sender: TObject); begin // This makes sure our table exists, just in case the data file // no longer exists. Makes the app more resilient. FDQueryCreateTable.ExecSQL; // And let’s not assume anything about that Delete button status UpdateDeleteButton; end;

procedure TForm42.FDConnection1BeforeConnect(Sender: TObject); begin // We don’t need to use a compiler directive here, but that does // mean our database file is in our documents folder, which // doesn’t make as much since for a desktop. {$if DEFINED(iOS) or DEFINED(ANDROID)} FDConnection1.Params.Values[‘Database’] := TPath.Combine(TPath.GetDocumentsPath, ‘todolist.sdb’);
{$ENDIF} end;

procedure TForm42.ListView1Click(Sender: TObject); begin UpdateDeleteButton; end;

procedure TForm42.UpdateDeleteButton; begin deleteButton.Visible := ListView1.Selected <> nil; end; [/code]

The beauty of calling the FDQueryCreateTable.ExecSQL is that you don’t need to deploy an empty database file. It just creates an empty one the first time it runs.

So what do you think, is this simpler? What would you do differently?

tags: FireDAC