Welcome

Welcome, thanks to look my blog

Saturday 19 April 2014

Mobile Tutorial: Using FireDAC in Mobile Applications (iOS and Android)

Mobile Tutorial: Using FireDAC in Mobile Applications (iOS and Android)



sumber : http://docwiki.embarcadero.com/RADStudio/XE5/en/Mobile_Tutorial:_Using_FireDAC_in_Mobile_Applications_%28iOS_and_Android%29

This tutorial describes the basic steps to use SQLite as a local data storage on your mobile device through the FireDAC framework.
iOS Android
IPodRunTime.png

AndroidTRunTime.png

Using FireDAC to Connect to the Database

FireDAC is a unique set of Universal Data Access Components for developing cross-platform database applications for Delphi and C++Builder. With its powerful common architecture, FireDAC enables native high-speed direct access from Delphi to InterBase, SQLite, MySQL, SQL Server, Oracle, PostgreSQL, IBM DB2, SQL Anywhere, Access, Firebird, Informix, and more.
The FireDAC native driver supports SQLite database version 3.0 and later. For a detailed discussion on SQLite usage in FireDAC for a Delphi application, read the "Using SQLite with FireDAC" article.
To use SQLite in your application, the sqlite3.dll file must be present on your development system. If this file is not present, download sqlite3.dll from http://www.sqlite.org/download.html to your system path (such as C:\Windows\SysWOW64 for 64-bit Windows).

Design and Set Up the User Interface

Create a new project. Choose a FireMonkey Mobile Application.
  1. Drop a TFDConnection component on the form.
  2. Right click the TFDConnection component and choose Connection Editor.
  3. In the FireDAC Connection Editor, set the following parameters of the TFDConnection:
    1. Set the Driver ID property to SQLite.
    2. Set the Database parameter to:
      C:\Users\Public\Documents\RAD Studio\<n.n>\Samples\Data\Employees.s3db (location of the database)
      and click Open in the File Open dialog box.
      (To display Employees.s3db in the Open dialog, set the All Files (*.*) option.)
      ConnectionEdtFD.png

    3. Click the Test button to test the connection.
      TestConnectionSuccess.png

    4. Click OK to close the Connection Editor.
  4. In the Object Inspector, set the following properties of TFDConnection:
    1. Set the LoginPrompt property to False, so that the user is not prompted for a login.
    2. Set the Connected property to True.

Using the LiveBindings Wizard

Use the LiveBindings Wizard to add the LiveBindings components (TBindSourceDB, TBindingsList), TFDQuery, and the TListView component.

Add the LiveBinding components

  1. Select View > LiveBindings Designer and the LiveBindings Designer opens.
  2. Select LiveBindings Wizard.
    SelectLiveBindWizard.png

  3. Select Create a data source binding task.
  4. Click the Next button.
  5. Select FireDAC class name.
  6. Click the Next button.
  7. Change the Command Type to Query.
  8. Set the Command Text property to select Name, Department, Seniority from Employee order by Department.
    LiveBindWizardFD.png

  9. Click the Test Command button.
  10. Click the Next button.
  11. Click the Finish button.
At this point, TBindSourceDB and TFDQuery components were added to your form.

Add the ListView component

  1. Reopen the LiveBindings Wizard.
  2. Select Link a control with a field binding task.
    ControlFieldWizard.png

  3. Click the Next button.
  4. Select TListView.
  5. Click the Next button.
  6. Select BindSourceDB1.
  7. Click the Next button.
  8. Select Name Field Name.
    FieldNameWizard.png

  9. Click the Next button.
  10. Click the Finish button to close the wizard.

Using the LiveBindings Wizard


LiveBindings Designer
Use the LiveBindings Designer to add new connections between the TBindSourceDB and TListView components.
  1. Select the ItemHeader.Text member from TListView.
  2. Bind to the Department member of the BindSourceDB1 component by dragging (a connection line appears).
  3. Select the Item.Text member from TListView.
  4. Bind to the Name member of the BindSourceDB1 component by dragging (a connection line appears).
  5. In the Object Inspector, set the following properties of TListView:
    1. Set the ItemAppearance to ImageListItemRightButton.
      In the LiveBindings Designer, the TListView should have a new member, Item.ButtonText.
    2. Set the Align property to alClient.
  6. In the LiveBindings Designer, select the Item.ButtonText member from TListView.
  7. Bind to the Seniority member of the BindSourceDB1 component by dragging (a connection line appears).

Preparing Your Application for Run Time

FireDAC has a loosely-coupled multilayered architecture, where layers provide services. A service API is defined as a COM interface that other layers can request using the interface factory.
To properly operate FireDAC, you must link the implementation of the IFDGUIxWaitCursor and IFDPhysDriver interfaces to your application.
For this, drop the TFDGUIxWaitCursor and TFDPhysSQLiteDriverLink components on the form.

Setting Up Your Database Deployment for mobile

Up to this point, you have used SQLite on your desktop. This means that the actual database is located on your local hard disk drive (for example, C:\Users\Public\Documents\RAD Studio\12.0\Samples\Data\Employees.s3db). On the mobile Device, applications are sand-boxed, and typically you can only read and write data that is located in the Documents folder (for iOS device) and internal storage (for Android device) under your application folder.
To connect to a local database on mobile, you need to perform the following actions:
  • Deploy the database to the mobile Device.
  • Change the configuration (to connect to the database file) to a local file under the Documents folder (for iOS device) or internal storage (for Android device).

Add and Configure Your Database File in the Deployment Manager

Before you can run your application on mobile, you need to set up the deployment for your database file (Employees.s3db).
  1. Open the Deployment Manager by selecting Project > Deployment.
  2. Select Add Files and select the database file (for example, C:\Users\Public\Documents\RAD Studio\12.0\Samples\Data\Employees.s3db).
    AddingDatabaseFile.png

  3. Select Employees.s3db, and change Remote Path to StartUp\Documents\ (for iOS platform) or assets\internal\ (for Android platform).
    RemotePath on iOS device platform
    RemotePathEmployees.png

    RemotePath on Android platform
    AndroidRemotePathAssets.png

  4. Select the Platforms column (double-click the ellipsis [...] in the row for Employees.s3db):
    1. Ensure that iOS Simulator and iOS Device or Android are present for Employees.s3db.
    2. Remove Win32 from the list if it is present (you do not have to copy database files to the Win32 platform).
  5. Select All-Configurations - iOS Device platform or All-Configurations - Android platform and make sure Employees.s3db is set to be deployed to StartUp\Documents\ or assets\internal\.
As you just configured, when you run the app on the mobile device, the database file (Employees.s3db) is set to be deployed to the Documents folder (for iOS platform) or internal storage (for Android platform) in the sandbox area of your mobile app.

Modifying Your Code to Connect to a Local Database File on Mobile

The basic features of this application are now implemented. The database file used in this application was created on Windows and the file is not available on your mobile device unless you copy it to the mobile device or create it on the fly.

Specifying the Location of the SQLite Database on the Mobile Device

  1. In the Form Designer, select the FDConnection1 component.
  2. In the Object Inspector, double-click the BeforeConnect event.
  3. Add the following code to this event handler:
Delphi:
procedure TForm1.FDConnection1BeforeConnect(Sender: TObject);
begin
  FDConnection1.Params.Values['Database'] := TPath.Combine(TPath.GetDocumentsPath, 'Employees.s3db');
end;
The TPath record is declared in System.IOUtils unit, so you need to add System.IOUtils in the uses clause of your unit.
C++ (only for iOS):
void __fastcall TForm1::FDConnection1BeforeConnect(TObject *Sender)
{
   FDConnection1->Params->Values["ColumnMetadataSupported"] = "False";
   FDConnection1->Params->Values["Database"] = System::Ioutils::TPath::Combine(System::Ioutils::TPath::GetDocumentsPath(), "Employees.s3db");
}
The TPath record is declared in System.IOUtils library, so you need to add #include <System.IOUtils.hpp> in your header unit.

Running Your Application on the Simulator or on the Mobile Device

Now your application is ready to run (select Run > Run).
iOS Android
IPodRunTime.png

AndroidTRunTime.png

No comments:

Post a Comment