目录

Initializing Database Access

Constraints

This feature supports phones and tablets by default. Starting from version 5.1.0(18), support for wearables is added. From version 5.1.1(19), TVs are also supported. From version 6.1.0(23), PCs/2-in-1 devices are supported as well.

Prerequisites

You have imported an object type file.

Procedure

  1. Configure the cloud database.

    Add the network permission to the entry/src/main/module.json5 file.

    "requestPermissions": [
      {
        "name": "ohos.permission.INTERNET"
      }
    ]
  2. (Optional) If database operations (such as adding or deleting data) require users to sign in to the app, you need to perform the following operations:

    1. Obtain user credentials via AuthProvider.
    2. Pass the obtained credentials when calling the init() method for initialization.
  3. Import required modules.

    import { cloudDatabase } from '@kit.CloudFoundationKit';
    import { BookInfo } from '../model/BookInfo';
  4. In the service code, use QuickStartDemo, the Cloud DB zone created in AppGallery Connect, to initialize DatabaseZone.

    let databaseZone = cloudDatabase.zone('QuickStartDemo');
    • You can create the databaseZone object, which will be used in each query, by referring to this section. The following code will not describe the creation process again.

    • The input parameter received by the cloudDatabase.zone method is cloudDBZoneName, which indicates the Cloud DB zone name. For details, please refer to Adding a Cloud DB Zone.

    • You can create a maximum of four Cloud DB zones. If more than four Cloud DB zones are created, Cloud DB access will fail.

  5. To use database query methods, you can use classes (BookInfo as an example) to initialize DatabaseQuery.

    let condition = new cloudDatabase.DatabaseQuery(BookInfo);