harmony 鸿蒙Accessing a DataAbility

  • 2023-02-03
  • 浏览 (712)

Accessing a DataAbility

To access a DataAbility, import the basic dependency packages and obtain the URI string for communicating with the DataAbility.

The basic dependency packages include:

  • @ohos.ability.featureAbility

  • @ohos.data.dataAbility

  • @ohos.data.relationalStore

The sample code for accessing a DataAbility is as follows:

  1. Create a DataAbilityHelper instance.
   // Different from the URI defined in the config.json file, the URI passed in the parameter has an extra slash (/), three slashes in total.
   import featureAbility from '@ohos.ability.featureAbility'
   import ohos_data_ability from '@ohos.data.dataAbility'
   import relationalStore from '@ohos.data.relationalStore'
   
   let urivar = "dataability:///com.ix.DataAbility"
   let DAHelper = featureAbility.acquireDataAbilityHelper(urivar);
  1. Construct RDB data.
   import ohos_data_ability from '@ohos.data.dataAbility'
   import rdb from '@ohos.data.rdb'

   let valuesBucket: rdb.ValuesBucket = {"name": "gaolu"}
   let da = new ohos_data_ability.DataAbilityPredicates()
   let valArray = new Array("value1");
   let cars = new Array({"batchInsert1" : "value1",} as rdb.ValuesBucket);

For details about DataAbilityPredicates, see DataAbility Predicates.

  1. Use insert to insert data to the DataAbility.
   import { BusinessError } from '@ohos.base';

   // Callback mode:
   DAHelper.insert(
     urivar,
     valuesBucket,
     (error: BusinessError, data: number) => {
       console.info("DAHelper insert result: " + data)
     }
   );
   import featureAbility from '@ohos.ability.featureAbility'
   import { BusinessError } from '@ohos.base';

   let urivar = "dataability:///com.ix.DataAbility"
   let DAHelper = featureAbility.acquireDataAbilityHelper(urivar);
   // Promise mode (await needs to be used in the asynchronous method):
   let datainsert = await DAHelper.insert(urivar, valuesBucket).then((data) => {
     console.info("insert success.");
   }).catch((error: BusinessError) => {
     console.error("insert failed.");
   });
  1. Use delete to delete data from the DataAbility.
   import featureAbility from '@ohos.ability.featureAbility'

   let urivar = "dataability:///com.ix.DataAbility"
   let DAHelper = featureAbility.acquireDataAbilityHelper(urivar);
   // Callback mode:
   DAHelper.delete(
     urivar,
     da,
     (error, data) => {
       console.info("DAHelper delete result: " + data)
     }
   );
   import featureAbility from '@ohos.ability.featureAbility'

   let urivar = "dataability:///com.ix.DataAbility"
   let DAHelper = featureAbility.acquireDataAbilityHelper(urivar);
   // Promise mode (await needs to be used in the asynchronous method):
   let datadelete = await DAHelper.delete(
     urivar,
     da,
   );
  1. Use update to update data in the DataAbility.
   import featureAbility from '@ohos.ability.featureAbility'

   let urivar = "dataability:///com.ix.DataAbility"
   let DAHelper = featureAbility.acquireDataAbilityHelper(urivar);
   // Callback mode:
   DAHelper.update(
     urivar,
     valuesBucket,
     da,
     (error, data) => {
       console.info("DAHelper update result: " + data)
     }
   );
   import featureAbility from '@ohos.ability.featureAbility'

   let urivar = "dataability:///com.ix.DataAbility"
   let DAHelper = featureAbility.acquireDataAbilityHelper(urivar);
   // Promise mode (await needs to be used in the asynchronous method):
   let dataupdate = await DAHelper.update(
     urivar,
     valuesBucket,
     da,
   );
  1. Use query to query data in the DataAbility.
   import featureAbility from '@ohos.ability.featureAbility'

   let urivar = "dataability:///com.ix.DataAbility"
   let DAHelper = featureAbility.acquireDataAbilityHelper(urivar);
   // Callback mode:
   DAHelper.query(
     urivar,
     valArray,
     da,
     (error, data) => {
       console.info("DAHelper query result: " + data)
     }
   );
   import featureAbility from '@ohos.ability.featureAbility'

   let urivar = "dataability:///com.ix.DataAbility"
   let DAHelper = featureAbility.acquireDataAbilityHelper(urivar);
   // Promise mode (await needs to be used in the asynchronous method):
   let dataquery = await DAHelper.query(
     urivar,
     valArray,
     da
   );
  1. Use batchInsert to insert data in batches to the DataAbility.
   import featureAbility from '@ohos.ability.featureAbility'

   let urivar = "dataability:///com.ix.DataAbility"
   let DAHelper = featureAbility.acquireDataAbilityHelper(urivar);
   // Callback mode:
   DAHelper.batchInsert(
     urivar,
     cars,
     (error, data) => {
       console.info("DAHelper batchInsert result: " + data)
     }
   );
   import featureAbility from '@ohos.ability.featureAbility'

   let urivar = "dataability:///com.ix.DataAbility"
   let DAHelper = featureAbility.acquireDataAbilityHelper(urivar);
   // Promise mode (await needs to be used in the asynchronous method):
   let databatchInsert = await DAHelper.batchInsert(
     urivar,
     cars
   );
  1. Use executeBatch to process data in batches in the DataAbility.
   import featureAbility from '@ohos.ability.featureAbility'

   let urivar = "dataability:///com.ix.DataAbility"
   let DAHelper = featureAbility.acquireDataAbilityHelper(urivar);
   // Callback mode:
   DAHelper.executeBatch(
     urivar,
     [
       {
         uri: urivar,
         type: featureAbility.DataAbilityOperationType.TYPE_INSERT,
         valuesBucket: {"executeBatch" : "value1",},
         predicates: da,
         expectedCount:0,
         predicatesBackReferences: undefined,
         interrupted:true,
       }
     ],
     (error, data) => {
       console.info("DAHelper executeBatch result: " + data)
     }
   );
   import featureAbility from '@ohos.ability.featureAbility'

   let urivar = "dataability:///com.ix.DataAbility"
   let DAHelper = featureAbility.acquireDataAbilityHelper(urivar);
   // Promise mode (await needs to be used in the asynchronous method):
   let dataexecuteBatch = await DAHelper.executeBatch(
     urivar,
     [
       {
         uri: urivar,
         type: featureAbility.DataAbilityOperationType.TYPE_INSERT,
         valuesBucket:
         {
           "executeBatch" : "value1",
         },
         predicates: da,
         expectedCount:0,
         predicatesBackReferences: undefined,
         interrupted:true,
       }
     ]
   );

The APIs for operating a DataAbility are provided by DataAbilityHelper. For details about the APIs, see DataAbilityHelper.

你可能感兴趣的鸿蒙文章

harmony 鸿蒙Application Models

harmony 鸿蒙Using Explicit Want to Start an Application Component

harmony 鸿蒙Using Implicit Want to Open a Website

harmony 鸿蒙AbilityStage Component Container

harmony 鸿蒙Accessing a DataShareExtensionAbility from the FA Model

harmony 鸿蒙AccessibilityExtensionAbility

harmony 鸿蒙Common action and entities Values

harmony 鸿蒙API Switching Overview

harmony 鸿蒙Switching of app and deviceConfig

harmony 鸿蒙Application- or Component-Level Configuration (FA Model)

0  赞