Currently, the use of a remote database for storing various types of data (such as product and user information for an online store, patient details in a clinic, etc.) is highly relevant. Also, more and more developers want to create cross platform apps to access the data on device targets such as Windows, macOS, iOS, and Android.
For this purpose, both traditional relational databases and document-oriented databases can be employed. Document-oriented databases are more flexible as they provide users with a REST API for interaction.
Therefore, for data manipulation, it is necessary to use such requests as PUT, POST, GET, DELETE. They allow users to write, receive, and delete data. In contrast to relational databases, there is no need for additional drivers here. This significantly simplifies the development of software that should utilize a remote database. Data in such a database is represented in the JSON format.
In this article, we will demonstrate how to write, read, and delete data in a document-oriented Firebase Realtime Database using the Firebase API with an RAD Studio with Delphi FMX cross-platform application.
Table of Contents
What is the Firebase Database API?
To work with the Firebase Realtime Database, we will use Firebase API.
How to create a Firebase project?
To leverage the capabilities of the document-oriented Firebase Realtime database, it is necessary to create a Firebase project.
The process of creating a new Firebase project is explained in an article dedicated to user registration and authentication using the Firebase API.
In the existing project MyNewSoftacomProj
, let’s select the Realtime Database tab.
Now let’s click on Create Database
Then, we will need to choose the location for our database. Let’s leave as it is and we will have the United States. Now, let’s click on Next.
Now you need to choose either the locked mode (Start in locked mode) or the test mode (Start in test mode). In the test mode, the database will be available for 30 days. To ensure better security, it is recommended to choose the locked mode (in this mode, a secret key will be used to access the data).
How to find the URL for your Firebase database?
The Firebase Realtime Database has been created. You need to use a URL to access the database.
To access the database in the secure mode, it is necessary to use a secret key. We should copy and save it for future access to the database from our Embarcadero Delphi FMX application. To do this, go to Project Settings.
Now let’s open the Service accounts tab.
Now let’s click on Database secrets.
How to find the Firebase API code?
Here we can find our secret code. To view it we need to click on Show.
Now we should click on Copy and save our secret code for its further use in our Embarcadero Delphi FMX app.
How to create a Delphi class save, read, and delete records in a document-oriented Firebase Realtime database?
As an example, we will be saving information about people (their names, surnames, professions) and retrieving this information from the Firebase Realtime Database. We will also implement the option to delete records from the database in our Delphi FMX application using the API.
For ease of working with the Firebase Realtime Database API, we have developed a special class called TFirebaseHelper.
This class contains a constructor that takes an object of the TNetHttpClient class and string constant DatabaseURL and SecretKey as parameters. These constants hold the URL of the database and the secret key of the database for accessing it in the secure mode.
The FormatJSON function allows formatting the response received from the Firebase Realtime Database and presenting it to the user in a convenient and readable form.
The Put method takes an object Data of the TJsonObject class, which includes the necessary data to be added to the database (in our example, this is a name, surname, profession). In the Data object, parameters are stored in the “key-value” format.
The second input parameter is a string constant Path, which contains the path and name of the record being saved in the database. The parameters that should be written to the database (the Data object) are transmitted to the MultiPartFormData object (of the TMultiPartFormData class) with further adding to the database via a PUT request (using FNetHttpClient).
FNetHttpClient takes a string constant DatabaseURL as a parameter, which contains the URL of the Firebase Realtime Database.
In addition to DatabaseURL, another string constant Path is transmitted as well. It contains the path to the record and its name.
Moreover, FNetHttpClient accepts MultiPartFormData. After executing the PUT request, the JSON response from the server is stored in the ResponseContent of the TStringStream class.
The TFirebaseHelper class allows working with the database in the secure mode. Therefore, we will also pass the secret key (auth=SecretKey). The Put method also returns the JSON response from the Firebase Realtime Database as a string with the parameters sent to the database.
The Get method takes a string constant Path as a parameter, containing the path to the required record and its name. FNetHttpClient performs a GET request to retrieve data from the Firebase Realtime Database. The Get method returns a JSON response from the Firebase Realtime Database as a string, which contains the records obtained from the database.
The Delete method also takes a string constant Path as a parameter. It contains the path to the required record and its name for its further deletion from the Firebase Realtime Database. FNetHttpClient performs a DELETE request to remove the data from the Firebase Realtime Database.
Where can I find an example of how to access Firebase using Delphi?
Below you can find the source code of the TFirebaseHelper
class.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 |
unit FirebaseHelperClass; interface uses System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants, FMX.Types, FMX.Controls, FMX.Forms, FMX.Graphics, FMX.Dialogs, FMX.Memo.Types, FMX.ScrollBox, FMX.Memo, FMX.StdCtrls, FMX.Controls.Presentation, System.Net.URLClient, System.Net.HttpClient, System.Net.HttpClientComponent, JSON, System.Threading, System.Net.Mime, System.Generics.Collections; type IFirebaseHelper = interface function Put(const Data: TJsonObject; Path: string): string; function Get(const Path: string): string; function Delete(const Path: string): string; end; TFirebaseHelper = class(TInterfacedObject, IFirebaseHelper) private FNetHttpClient: TNetHTTPClient; FSecretKey: string; FDatabaseURL: string; function FormatJSON(const JSON: string): string; public constructor Create(const NetHttpClient: TNetHTTPClient; const DatabaseURL: string; const SecretKey: string); function Put(const Data:TJsonObject; Path: string): string; function Get(const Path: string): string; function Delete(const Path: string): string; end; implementation { TFirebaseHelper } constructor TFirebaseHelper.Create(const NetHttpClient: TNetHTTPClient; const DatabaseURL: string; const SecretKey: string); begin FNetHttpClient := NetHttpClient; if SecretKey <> '' then FSecretKey := SecretKey else begin ShowMessage('Secret key is empty!'); Exit; end; if DatabaseURL <> '' then FDatabaseURL := DatabaseURL else begin ShowMessage('Database URL is empty!'); Exit; end; end; function TFirebaseHelper.Delete(const Path: string): string; var ResponseContent: TStringStream; begin ResponseContent := nil; try ResponseContent := TStringStream.Create; FNetHttpClient.Delete(FDatabaseURL + Path + '.json?auth=' + FSecretKey, ResponseContent); Result := FormatJSON(ResponseContent.DataString); finally ResponseContent.Free; end; end; function TFirebaseHelper.FormatJSON(const JSON: string): string; var JsonValue: TJsonValue; JsonObject: TJsonObject; begin JsonValue := TJsonObject.ParseJSONValue(JSON); try if JsonValue is TJSONNull then Result := JSON else begin JsonObject := TJsonObject(JsonValue) as TJsonObject; Result := JsonObject.Format(); end; finally JsonValue.Free; end; end; function TFirebaseHelper.Get(const Path: string): string; var ResponseContent: TStringStream; begin ResponseContent := nil; try ResponseContent := TStringStream.Create; FNetHttpClient.Get(FDatabaseURL + Path + '.json?auth=' + FSecretKey, ResponseContent); Result := FormatJSON(ResponseContent.DataString); finally ResponseContent.Free; end; end; function TFirebaseHelper.Put(const Data: TJsonObject; Path: string): string; var MultipartFormData: TMultipartFormData; Stream, ResponseContent: TStringStream; begin Stream := nil; ResponseContent := nil; MultipartFormData := nil; try MultipartFormData := TMultipartFormData.Create; Stream := TStringStream.Create(FormatJSON(Data.ToJSON), TEncoding.UTF8); MultipartFormData.Stream.LoadFromStream(Stream); ResponseContent := TStringStream.Create; FNetHttpClient.Put(FDatabaseURL + Path + '.json?auth=' + FSecretKey, MultipartFormData, ResponseContent); Result := FormatJSON(ResponseContent.DataString); finally Stream.Free; ResponseContent.Free; MultipartFormData.Free; end; end; end. |
How to add, read, and delete records from a Firebase realtime database with Delphi?
To interact with the Firebase Realtime Database API (to send PUT, GET, DELETE requests to the database in our Delphi FMX application), the TNetHttpClient component is used.
In our application, we will transmit three parameters to the database: a name, surname, and profession. We will use TEdit components for entering these parameters.
Our application allows writing data to the database. It also enables retrieving all data from the database or a specific record based on the surname. There is also the possibility to delete a specific record using the surname. All these functions are implemented as handlers for corresponding buttons (of the TButton class).
The JSON response from the Firebase Realtime Database is displayed with the help of the TMemo component.
We will declare two fields, FSecretKey and FDatabaseURL (the string type), which store the URL of the database and the secret key.
In the OnCreate method of the main form (MainForm), we will connect the URL of the database and the secret code.
Let’s consider the implementation of a button handler that performs the writing of parameters to the database (PUT to DB). To carry out this task, we will declare a FirebaseObject object (of the IFirebaseHelper type). Also, for storing data with its further saving to the database, we will declare a JsonToDB object (of the TJSONObject type). For storing the JSON response from the service, we will declare a string variable AnswerFromFirebase.
Next, it is necessary to call the constructor of the TFirebaseHelper class and pass our NetHttpClient1 object (of the TNetHttpClient class) along with the values of the FDatabaseURL and FSecretKey fields.
We will add parameters such as a name, surname, and profession to the JsonToDB object (parameters are stored in the “key-value” format) with their further saving to the database. We will read the parameter values from the corresponding TEdit input fields (EditName, EditSurname, EditProfession). If necessary, you can send an unlimited number of values using the AddPair method. In our example, we used three parameters (Name, Surname, Profession).
Next, we will make a PUT request to add our parameters to the database. For this, we will use the Put method of the FirebaseObject. This method takes JsonToDB as input parameters (here our transmitted parameters are stored), the path, and the name of the record (in our case, the surname). It is necessary to perform all database requests using TTask to avoid blocking the interface of our Delphi FMX application. The response from the Firebase Realtime Database is stored in the string variable AnswerFromFirebase.
To display the JSON response from the Firebase Realtime Database, we will use TThread.Synchronize (to update the TMemo component in the main thread).
You can see the code for the “PUT to DB” button handler below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
procedure TMainForm.btnPutClick(Sender: TObject); var JsonToDB : TJSONObject; FirebaseObject: IFirebaseHelper; AnswerFromFirebase: string; begin Memo1.Lines.Clear; FirebaseObject := TFirebaseHelper.Create(NetHttpClient1, FDatabaseURL, FSecretKey); TTask.Run(procedure begin JsonToDB := TJSONObject.Create; try JsonToDB.AddPair('Name', EditName.Text); JsonToDB.AddPair('Surname', EditSurname.Text); JsonToDB.AddPair('Profession', EditProfession.Text); AnswerFromFirebase := FirebaseObject.Put(JsonToDB, 'MyNewData/' + EditSurname.Text); TThread.Synchronize(nil, procedure begin Memo1.Text := AnswerFromFirebase; TabControl1.GotoVisibleTab(1); end); finally JsonToDB.Free; end; end); end; |
How do I save records to a Firebase realtime database?
Let’s demonstrate the capability of our Delphi FMX application to save records to the database using the PUT method. We will show the work of the application on the Windows platform.
Let’s consider the implementation of a button handler that reads records from the database (Get All from DB). To read all records from the database using the indicated path, we will apply the Get method of the TFirebaseHelper class. We will display the obtained values of the records in the form of a JSON response from the Firebase Realtime Database using the TMemo component.
The code of the Get All from DB button handler looks like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
procedure TMainForm.btnGetAllClick(Sender: TObject); var JsonResponse: string; FirebaseObject: IFirebaseHelper; begin Memo1.Lines.Clear; FirebaseObject := TFirebaseHelper.Create(NetHttpClient1, FDatabaseURL, FSecretKey); TTask.Run( procedure begin JsonResponse := FirebaseObject.Get('MyNewData/'); TThread.Synchronize(nil, procedure begin Memo1.Lines.Add(JsonResponse); TabControl1.GotoVisibleTab(1); end); end); |
How do I read data from a Firebase realtime database?
Let’s demonstrate the process of reading data from the database in our Delphi FMX application.
Our Delphi FMX application also allows reading data from the database based on the surname. For this purpose, a similar method Get is used. We will transmit the path to the record and the name of the record (surname).
You can see the code of the Get from DB by Surname button handler below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
procedure TMainForm.btnGetBySurnameClick(Sender: TObject); var JsonResponse: string; FirebaseObject: IFirebaseHelper; begin Memo1.Lines.Clear; FirebaseObject := TFirebaseHelper.Create(NetHttpClient1, FDatabaseURL, FSecretKey); TTask.Run( procedure begin JsonResponse := FirebaseObject.Get('MyNewData/' + EditSurname.Text); TThread.Synchronize(nil, procedure begin Memo1.Lines.Add(JsonResponse); TabControl1.GotoVisibleTab(1); end); end); end; |
Let’s demonstrate reading data from the database in our Delphi FMX application based on the surname.
To delete records from the database in our Delphi FMX application, it is necessary to use the Delete method of the TFirebaseHelper class. As a parameter, we will transmit the path to the record and the surname of the person in the database whose record we want to delete.
We will display the deletion result to the user with the help of the TMemo component.
The code for the Delete from DB button handler is provided below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
procedure TMainForm.btnDeleteClick(Sender: TObject); var FirebaseObject: IFirebaseHelper; ResponseFromFirebase: string; begin Memo1.Lines.Clear; FirebaseObject := TFirebaseHelper.Create(NetHttpClient1, FDatabaseURL, FSecretKey); TTask.Run( procedure begin try ResponseFromFirebase := FirebaseObject.Delete('MyNewData/' + EditSurname.Text); finally TThread.Synchronize(nil, procedure begin if ResponseFromFireBase = '' then Memo1.Text := 'Record ' + EditSurname.Text + ' deleted!'; TabControl1.GotoVisibleTab(1); end); end; end); end; |
How do I delete data from a Firebase realtime database using Delphi?
Let’s demonstrate the process of deleting data from the database in our Delphi FMX application based on the surname.
Let’s demonstrate the work of the application on the Android and MacOS platforms.
Do you want to try some of these examples for yourself? Why not download a free trial of the latest version of RAD Studio with Delphi?
This article was written by Embarcadero Tech Partner Softacom. Softacom specialize in all sorts of software development focused on Delphi. Read more about their services on the Softacom website.
Design. Code. Compile. Deploy.
Start Free Trial Upgrade Today
Free Delphi Community Edition Free C++Builder Community Edition