C++ Builder - Creating InterBase database on the fly at run-time using FireDAC
Sometimes you have the need to create your InterBase database on the fly in code at runtime, for example, if a user downloads your application and runs the application for the first time, and your app needs to create and use a database with the application. On Single or Multi-Device platforms, this would save you from having to deploy the database file and also setup the paths for deployment reducing your upfront work.
From Connect to InterBase (FireDAC), looks like OpenMode=Open|Create|OpenOrCreate is what we need to create an InterBase database on the fly.
In previous versions of FireDAC, the InterBase driver had a 'CreateDatabase' parameter, but the 'CreateDatabase' parameter has been deprecated, but FireDAC internally still recognizes CreateDatabase=Yes|No. With the current versions of FireDAC, instead of 'CreateDatabase', you should use OpenMode = Open | Create | OpenOrCreate, where:
Create -- creates a new database.
OpenOrCreate -- opens an existing database or creates a new database if the specified database does not exist.
For an example using C++ Builder, here is the base code you would need:
TFDConnection* FDConnection1=new TFDConnection(Form3)
FDConnection1->Params->Values["DriverID"] = "IB";
FDConnection1->Params->Values["Database"] = DBPath;
FDConnection1->Params->Values["User_Name"] = "SYSDBA";
FDConnection1->Params->Values["Password"] = "masterkey";
FDConnection1->Params->Values["OpenMode"] = "Create";
In the above example code, using OpenMode = Create, if the MyDB.ib file already exists, then FireDAC will throw this error:
EIBNativeException: [FireDAC][Phys][IB]I/O error for the file "MyDB.ib" database exists.
If you want to open the existing MyDB.ib file, then use OpenMode = OpenOrCreate.
If you need to know the names of the Parameters, you can drop a FDConnection component on the form, double-click the FDConnection component that will display the FDConnection Editor, showing all the Parameters for your FireDAC InterBase driver, like this:
A C++ Builder 10.2 Tokyo example is here, showing both deploy the database file and set the path for deployment.
Using InterBase on mobile and/or desktop devices gives many benefits, especially when you are storing local data needing full 256bit AES strength encryption. Using the properties exposed in FireDAC for InterBase, you can easily create and manage a local database on the mobile or desktop device without having to deploy one from the start. This makes deployment simpler! But, if you do need to deploy an InterBase database you can still do this as InterBase supports the same InterBase On Disk Structure (ODS) for Windows, Mac, Linux, Solaris, Android and iOS.
Please login first in order for you to submit comments