![| Embarcadero RAD Studio Delphi C++Builder Blogs How To Use The Firebase API To Add Read and Delete Data In A Realtime Document Oriented Database](https://i0.wp.com/blogs.embarcadero.com/wp-content/uploads/2024/03/pexels-fauxels-3183153-1811089-scaled.jpg?resize=1140%2C694&ssl=1)
![Softacom article | Embarcadero RAD Studio Delphi C++Builder Blogs How To Use The Firebase API To Add Read and Delete Data In A Realtime Document Oriented Database Softacom Logo](https://i0.wp.com/blogs.embarcadero.com/wp-content/uploads/2024/01/Softacom-article-2237507.jpg?resize=300%2C195&ssl=1)
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.
![20240126 113123 | Embarcadero RAD Studio Delphi C++Builder Blogs How To Use The Firebase API To Add Read and Delete Data In A Realtime Document Oriented Database](https://i0.wp.com/blogs.embarcadero.com/wp-content/uploads/2024/01/2024-01-26-113123-8720977.jpg?resize=750%2C412&ssl=1)
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.
![20240126 114059 | Embarcadero RAD Studio Delphi C++Builder Blogs How To Use The Firebase API To Add Read and Delete Data In A Realtime Document Oriented Database](https://i0.wp.com/blogs.embarcadero.com/wp-content/uploads/2024/01/2024-01-26-114059-8285698.jpg?resize=750%2C343&ssl=1)
Now let’s click on Create Database
![20240126 114224 | Embarcadero RAD Studio Delphi C++Builder Blogs How To Use The Firebase API To Add Read and Delete Data In A Realtime Document Oriented Database](https://i0.wp.com/blogs.embarcadero.com/wp-content/uploads/2024/01/2024-01-26-114224-1699995.jpg?resize=750%2C340&ssl=1)
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.
![20240126 114322 | Embarcadero RAD Studio Delphi C++Builder Blogs How To Use The Firebase API To Add Read and Delete Data In A Realtime Document Oriented Database](https://i0.wp.com/blogs.embarcadero.com/wp-content/uploads/2024/01/2024-01-26-114322-5146973.jpg?resize=750%2C340&ssl=1)
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).
![20240126 114454 | Embarcadero RAD Studio Delphi C++Builder Blogs How To Use The Firebase API To Add Read and Delete Data In A Realtime Document Oriented Database](https://i0.wp.com/blogs.embarcadero.com/wp-content/uploads/2024/01/2024-01-26-114454-6326209.jpg?resize=750%2C338&ssl=1)
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.
![20240126 114625 | Embarcadero RAD Studio Delphi C++Builder Blogs How To Use The Firebase API To Add Read and Delete Data In A Realtime Document Oriented Database](https://i0.wp.com/blogs.embarcadero.com/wp-content/uploads/2024/01/2024-01-26-114625-7897892.jpg?resize=750%2C330&ssl=1)
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.
![20240126 114735 | Embarcadero RAD Studio Delphi C++Builder Blogs How To Use The Firebase API To Add Read and Delete Data In A Realtime Document Oriented Database](https://i0.wp.com/blogs.embarcadero.com/wp-content/uploads/2024/01/2024-01-26-114735-7737341.jpg?resize=750%2C301&ssl=1)
Now let’s open the Service accounts tab.
![20240126 114835 | Embarcadero RAD Studio Delphi C++Builder Blogs How To Use The Firebase API To Add Read and Delete Data In A Realtime Document Oriented Database](https://i0.wp.com/blogs.embarcadero.com/wp-content/uploads/2024/01/2024-01-26-114835-1693179.jpg?resize=750%2C237&ssl=1)
Now let’s click on Database secrets.
![20240126 114937 | Embarcadero RAD Studio Delphi C++Builder Blogs How To Use The Firebase API To Add Read and Delete Data In A Realtime Document Oriented Database](https://i0.wp.com/blogs.embarcadero.com/wp-content/uploads/2024/01/2024-01-26-114937-7403916.jpg?resize=750%2C431&ssl=1)
How to find the Firebase API code?
Here we can find our secret code. To view it we need to click on Show.
![20240126 115056 | Embarcadero RAD Studio Delphi C++Builder Blogs How To Use The Firebase API To Add Read and Delete Data In A Realtime Document Oriented Database](https://i0.wp.com/blogs.embarcadero.com/wp-content/uploads/2024/01/2024-01-26-115056-7668115.jpg?resize=750%2C364&ssl=1)
Now we should click on Copy and save our secret code for its further use in our Embarcadero Delphi FMX app.
![20240126 115200 | Embarcadero RAD Studio Delphi C++Builder Blogs How To Use The Firebase API To Add Read and Delete Data In A Realtime Document Oriented Database](https://i0.wp.com/blogs.embarcadero.com/wp-content/uploads/2024/01/2024-01-26-115200-8873666.jpg?resize=750%2C195&ssl=1)
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.
![20240129 125105 | Embarcadero RAD Studio Delphi C++Builder Blogs How To Use The Firebase API To Add Read and Delete Data In A Realtime Document Oriented Database](https://i0.wp.com/blogs.embarcadero.com/wp-content/uploads/2024/01/2024-01-29-125105-4975043.jpg?resize=750%2C375&ssl=1)
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.
![20240126 115501 | Embarcadero RAD Studio Delphi C++Builder Blogs How To Use The Firebase API To Add Read and Delete Data In A Realtime Document Oriented Database](https://i0.wp.com/blogs.embarcadero.com/wp-content/uploads/2024/01/2024-01-26-115501-9609049.jpg?resize=750%2C429&ssl=1)
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.
![20240129 135252 | Embarcadero RAD Studio Delphi C++Builder Blogs How To Use The Firebase API To Add Read and Delete Data In A Realtime Document Oriented Database](https://i0.wp.com/blogs.embarcadero.com/wp-content/uploads/2024/01/2024-01-29-135252-1215363.jpg?resize=750%2C442&ssl=1)
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.
![20240129 135353 | Embarcadero RAD Studio Delphi C++Builder Blogs How To Use The Firebase API To Add Read and Delete Data In A Realtime Document Oriented Database](https://i0.wp.com/blogs.embarcadero.com/wp-content/uploads/2024/01/2024-01-29-135353-3316541.jpg?resize=750%2C658&ssl=1)
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.
![20240129 135511 | Embarcadero RAD Studio Delphi C++Builder Blogs How To Use The Firebase API To Add Read and Delete Data In A Realtime Document Oriented Database](https://i0.wp.com/blogs.embarcadero.com/wp-content/uploads/2024/01/2024-01-29-135511-3850039.jpg?resize=750%2C615&ssl=1)
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.
![20240129 135642 | Embarcadero RAD Studio Delphi C++Builder Blogs How To Use The Firebase API To Add Read and Delete Data In A Realtime Document Oriented Database](https://i0.wp.com/blogs.embarcadero.com/wp-content/uploads/2024/01/2024-01-29-135642-8108684.jpg?resize=750%2C407&ssl=1)
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.
![20240126 120544 | Embarcadero RAD Studio Delphi C++Builder Blogs How To Use The Firebase API To Add Read and Delete Data In A Realtime Document Oriented Database](https://i0.wp.com/blogs.embarcadero.com/wp-content/uploads/2024/01/2024-01-26-120544-4513268.jpg?resize=750%2C329&ssl=1)
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.
![20240126 120647 | Embarcadero RAD Studio Delphi C++Builder Blogs How To Use The Firebase API To Add Read and Delete Data In A Realtime Document Oriented Database](https://i0.wp.com/blogs.embarcadero.com/wp-content/uploads/2024/01/2024-01-26-120647-5589919.jpg?resize=750%2C412&ssl=1)
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).
![20240126 120758 | Embarcadero RAD Studio Delphi C++Builder Blogs How To Use The Firebase API To Add Read and Delete Data In A Realtime Document Oriented Database](https://i0.wp.com/blogs.embarcadero.com/wp-content/uploads/2024/01/2024-01-26-120758-6623717.jpg?resize=750%2C407&ssl=1)
The JSON response from the Firebase Realtime Database is displayed with the help of the TMemo component.
![20240126 120849 | Embarcadero RAD Studio Delphi C++Builder Blogs How To Use The Firebase API To Add Read and Delete Data In A Realtime Document Oriented Database](https://i0.wp.com/blogs.embarcadero.com/wp-content/uploads/2024/01/2024-01-26-120849-7821425.jpg?resize=750%2C408&ssl=1)
We will declare two fields, FSecretKey and FDatabaseURL (the string type), which store the URL of the database and the secret key.
![20240126 120950 | Embarcadero RAD Studio Delphi C++Builder Blogs How To Use The Firebase API To Add Read and Delete Data In A Realtime Document Oriented Database](https://i0.wp.com/blogs.embarcadero.com/wp-content/uploads/2024/01/2024-01-26-120950-8714378.jpg?resize=750%2C186&ssl=1)
In the OnCreate method of the main form (MainForm), we will connect the URL of the database and the secret code.
![20240126 121046 | Embarcadero RAD Studio Delphi C++Builder Blogs How To Use The Firebase API To Add Read and Delete Data In A Realtime Document Oriented Database](https://i0.wp.com/blogs.embarcadero.com/wp-content/uploads/2024/01/2024-01-26-121046-8734600.jpg?resize=750%2C813&ssl=1)
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.
![20240129 140016 | Embarcadero RAD Studio Delphi C++Builder Blogs How To Use The Firebase API To Add Read and Delete Data In A Realtime Document Oriented Database](https://i0.wp.com/blogs.embarcadero.com/wp-content/uploads/2024/01/2024-01-29-140016-3502165.jpg?resize=750%2C135&ssl=1)
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.
![20240129 140115 | Embarcadero RAD Studio Delphi C++Builder Blogs 2024 01 29 14 01 15](https://i0.wp.com/blogs.embarcadero.com/wp-content/uploads/2024/01/2024-01-29-140115-9859165.jpg?resize=750%2C105&ssl=1)
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).
![20240126 121333 | Embarcadero RAD Studio Delphi C++Builder Blogs How To Use The Firebase API To Add Read and Delete Data In A Realtime Document Oriented Database](https://i0.wp.com/blogs.embarcadero.com/wp-content/uploads/2024/01/2024-01-26-121333-5476873.jpg?resize=750%2C143&ssl=1)
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.
![20240129 140209 | Embarcadero RAD Studio Delphi C++Builder Blogs How To Use The Firebase API To Add Read and Delete Data In A Realtime Document Oriented Database](https://i0.wp.com/blogs.embarcadero.com/wp-content/uploads/2024/01/2024-01-29-140209-3034442.jpg?resize=750%2C344&ssl=1)
To display the JSON response from the Firebase Realtime Database, we will use TThread.Synchronize (to update the TMemo component in the main thread).
![20240126 121515 | Embarcadero RAD Studio Delphi C++Builder Blogs How To Use The Firebase API To Add Read and Delete Data In A Realtime Document Oriented Database](https://i0.wp.com/blogs.embarcadero.com/wp-content/uploads/2024/01/2024-01-26-121515-2065677.jpg?resize=750%2C205&ssl=1)
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.
![20240126 121702 | Embarcadero RAD Studio Delphi C++Builder Blogs How To Use The Firebase API To Add Read and Delete Data In A Realtime Document Oriented Database](https://i0.wp.com/blogs.embarcadero.com/wp-content/uploads/2024/01/2024-01-26-121702-8468506.jpg?resize=750%2C736&ssl=1)
![20240126 121750 | Embarcadero RAD Studio Delphi C++Builder Blogs How To Use The Firebase API To Add Read and Delete Data In A Realtime Document Oriented Database](https://i0.wp.com/blogs.embarcadero.com/wp-content/uploads/2024/01/2024-01-26-121750-7526726.jpg?resize=750%2C728&ssl=1)
![20240126 121836 | Embarcadero RAD Studio Delphi C++Builder Blogs How To Use The Firebase API To Add Read and Delete Data In A Realtime Document Oriented Database](https://i0.wp.com/blogs.embarcadero.com/wp-content/uploads/2024/01/2024-01-26-121836-1364127.jpg?resize=750%2C386&ssl=1)
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.
![20240126 121931 | Embarcadero RAD Studio Delphi C++Builder Blogs How To Use The Firebase API To Add Read and Delete Data In A Realtime Document Oriented Database](https://i0.wp.com/blogs.embarcadero.com/wp-content/uploads/2024/01/2024-01-26-121931-3643339.jpg?resize=750%2C213&ssl=1)
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.
![20240126 122107 | Embarcadero RAD Studio Delphi C++Builder Blogs How To Use The Firebase API To Add Read and Delete Data In A Realtime Document Oriented Database](https://i0.wp.com/blogs.embarcadero.com/wp-content/uploads/2024/01/2024-01-26-122107-7795974.jpg?resize=750%2C726&ssl=1)
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).
![20240129 140501 | Embarcadero RAD Studio Delphi C++Builder Blogs How To Use The Firebase API To Add Read and Delete Data In A Realtime Document Oriented Database](https://i0.wp.com/blogs.embarcadero.com/wp-content/uploads/2024/01/2024-01-29-140501-8637014.jpg?resize=750%2C218&ssl=1)
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.
![20240126 122353 | Embarcadero RAD Studio Delphi C++Builder Blogs How To Use The Firebase API To Add Read and Delete Data In A Realtime Document Oriented Database](https://i0.wp.com/blogs.embarcadero.com/wp-content/uploads/2024/01/2024-01-26-122353-2249445.jpg?resize=750%2C729&ssl=1)
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.
![20240129 140701 | Embarcadero RAD Studio Delphi C++Builder Blogs How To Use The Firebase API To Add Read and Delete Data In A Realtime Document Oriented Database](https://i0.wp.com/blogs.embarcadero.com/wp-content/uploads/2024/01/2024-01-29-140701-8347090.jpg?resize=750%2C272&ssl=1)
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.
![20240126 122655 | Embarcadero RAD Studio Delphi C++Builder Blogs How To Use The Firebase API To Add Read and Delete Data In A Realtime Document Oriented Database](https://i0.wp.com/blogs.embarcadero.com/wp-content/uploads/2024/01/2024-01-26-122655-6634757.jpg?resize=750%2C723&ssl=1)
![20240126 122733 | Embarcadero RAD Studio Delphi C++Builder Blogs How To Use The Firebase API To Add Read and Delete Data In A Realtime Document Oriented Database](https://i0.wp.com/blogs.embarcadero.com/wp-content/uploads/2024/01/2024-01-26-122733-9189251.jpg?resize=750%2C726&ssl=1)
Let’s demonstrate the work of the application on the Android and MacOS platforms.
![20240126 122733 | Embarcadero RAD Studio Delphi C++Builder Blogs How To Use The Firebase API To Add Read and Delete Data In A Realtime Document Oriented Database](https://i0.wp.com/blogs.embarcadero.com/wp-content/uploads/2024/01/2024-01-26-122733-5664729.jpg?resize=750%2C726&ssl=1)
![20240126 122900 | Embarcadero RAD Studio Delphi C++Builder Blogs How To Use The Firebase API To Add Read and Delete Data In A Realtime Document Oriented Database](https://i0.wp.com/blogs.embarcadero.com/wp-content/uploads/2024/01/2024-01-26-122900-8456216.jpg?resize=750%2C352&ssl=1)
![20240126 122949 | Embarcadero RAD Studio Delphi C++Builder Blogs How To Use The Firebase API To Add Read and Delete Data In A Realtime Document Oriented Database](https://i0.wp.com/blogs.embarcadero.com/wp-content/uploads/2024/01/2024-01-26-122949-9449502.jpg?resize=750%2C351&ssl=1)
![20240126 123035 | Embarcadero RAD Studio Delphi C++Builder Blogs How To Use The Firebase API To Add Read and Delete Data In A Realtime Document Oriented Database](https://i0.wp.com/blogs.embarcadero.com/wp-content/uploads/2024/01/2024-01-26-123035-2450268.jpg?resize=750%2C353&ssl=1)
![20240126 123121 | Embarcadero RAD Studio Delphi C++Builder Blogs How To Use The Firebase API To Add Read and Delete Data In A Realtime Document Oriented Database](https://i0.wp.com/blogs.embarcadero.com/wp-content/uploads/2024/01/2024-01-26-123121-7820520.jpg?resize=750%2C349&ssl=1)
![20240126 123204 | Embarcadero RAD Studio Delphi C++Builder Blogs How To Use The Firebase API To Add Read and Delete Data In A Realtime Document Oriented Database](https://i0.wp.com/blogs.embarcadero.com/wp-content/uploads/2024/01/2024-01-26-123204-8532263.jpg?resize=750%2C351&ssl=1)
![| Embarcadero RAD Studio Delphi C++Builder Blogs How To Record Exact Operation Execution Time With Delphi Delphi logo](https://i0.wp.com/blogs.embarcadero.com/wp-content/uploads/2022/11/dlogonew-5582740.png?resize=254%2C242&ssl=1)
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