The Firebird getting started sample shows how to use FireDAC with Firebird databases. This sample allows the user to do:
- Temporary connection definition at run time.
- Master-details relationship between datasets using TFDQuery.
- Transactions handling with TFDTransaction.
- Management of Firebird database such as: managing users, creating database backup/restoring database from backup and validating a database.
Table of Contents
Location
You can find the Firebird sample project at:
- Start | Programs | Embarcadero RAD Studio Sydney | Samples and then navigate to:
Object Pascal\Database\FireDAC\Samples\Getting Started\Firebird
- Subversion Repository:
- You can find Delphi code samples in GitHub Repositories. Search by name into the samples repositories according to your RAD Studio version.
How to Use the Sample
- Navigate to the location given above and open
GettingStarted.dproj
. - Press F9 or choose Run > Run.
Files
File in Delphi | Contains |
---|---|
GettingStarted.dproj GettingStarted.dpr | The project itself. |
fGettingStarted.pas fGettingStarted.fmx | The main form. |
Implementation
The sample implements the following features.
Creating temporary connection definition
The simplest way to configure connection to Firebird database at run time is to build a temporary connection definition:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
with dbMain do begin Close; <em>// create temporary connection definition</em> with Params do begin Clear; Add('DriverID=FB'); Add('Server=localhost'); Add('Database=x:\path_to_db\db.fdb'); Add('User_Name=sysdba'); Add('Password=masterkey'); end; Open; end; |
In the sample the temporary definition is created when the Connect button is clicked.
Master Details
In this sample, the Categories and Products tables have one-to-many relation by CategoryID
field. The qryCategories.SQL property is set up as follows:
1 |
select * from Categories |
In addition, the qryProducts.SQL property is set up as follows:
1 2 |
select * from Products where CategoryID = :CategoryID |
Finally, the qryProducts.MasterSource property is set to dsCategories
, while the MasterFields property is set to CategoryID
. This creates a master-details relationship between the datasets.
Transaction handling
Handling of InterBase transactions is demonstrated using TFDTransaction + TFDConnection components. Both are configured as follows: trnMain.Connection property is set to dbMain connection
and dbMain.Transaction property is set to trnMain
.
Starting a transaction:
1 |
trnMain.StartTransaction; |
Committing the transaction:
1 |
trnMain.Commit; |
Rolling back the transaction:
1 |
trnMain.Rollback; |
Executing queries
Simple queries execution is demonstrated via ExecSQL method of TFDConnection.
Updating records:
1 2 |
dbMain.ExecSQL('update {id Products} set UnitPrice = UnitPrice * :P1 + :P2 ' + 'where ProductID < 3', [Random(5), Random(3)]); |
Getting a scalar value from the database:
1 |
dMaxPrice := dbMain.ExecSQLScalar('select MAX(UnitPrice) from {id Products}'); |
Service work with Firebird database
The sample performs the following management of databases:
- Security management.
- Database maintenance.
- Tracing server activity
All the management of databases is done using the following components TFDIBBackup, TFDIBRestore, TFDIBRestore, TFDIBSecurity, TFDIBTrace and TFDIBValidate.
This demo demonstrates work with TFDIBBackup, TFDIBRestore, TFDIBSecurity and TFDIBValidate components:
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 |
with ibSecurity do begin Host := 'localhost'; UserName := 'sysdba'; Password := 'masterkey'; AUserName := 'John_Doe'; case rgSecActions.ItemIndex of C_ADD_USER: begin APassword := 'p@ssword'; AddUser; end; C_DELETE_USER: begin DeleteUser; end; C_DISPLAY_USER: begin DisplayUser; with Memo1.Lines do begin Add('First name: ' + AFirstName); Add('Middle name: ' + AMiddleName); Add('Last name: ' + ALastName); end; end; C_MODIFY_USER: begin APassword := 'p@ssword_changed'; ModifyUser; end; end; end; … with ibBackup do begin Host := 'localhost'; UserName := 'sysdba'; Password := 'masterkey'; AUserName := 'John_Doe'; BackupFiles.Clear; BackupFiles.Add('x:\path_to_backup\db.backup'); Backup; end; … with ibRestore do begin Host := 'localhost'; UserName := 'sysdba'; Password := 'masterkey'; AUserName := 'John_Doe'; BackupFiles.Clear; BackupFiles.Add('x:\path_to_backup\db.backup'); Restore; end; … with ibValidate do begin Host := 'localhost'; UserName := 'sysdba'; Password := 'masterkey'; AUserName := 'John_Doe'; case rgValActions.ItemIndex of C_ANALYZE: Analyze; C_CHECKONLY: CheckOnly; C_REPAIR: Repair; C_SWEEP: Sweep; end; end; |
In the link below you can find more information about this sample and links to other samples:
http://docwiki.embarcadero.com/CodeExamples/Sydney/en/FireDAC.Firebird_Sample
Design. Code. Compile. Deploy.
Start Free Trial Upgrade Today
Free Delphi Community Edition Free C++Builder Community Edition