@ohos.application.DataShareExtensionAbility (DataShare ExtensionAbility)

The DataShareExtensionAbility module provides data share services based on the ExtensionAbility.

NOTE

  • The initial APIs of this module are supported since API version 9. Newly added APIs will be marked with a superscript to indicate their earliest API version.

  • The APIs provided by this module are system APIs.

  • The APIs of this module can be used only in the stage model.

Modules to Import

import DataShareExtensionAbility from '@ohos.application.DataShareExtensionAbility';

Attributes

System capability: SystemCapability.DistributedDataManager.DataShare.Provider

Name Type Readable Writable Description
context10+ ExtensionContext Yes No Context of the DataShare ExtensionAbility.

onCreate

onCreate?(want: Want, callback: AsyncCallback<void>): void

Called by the server to initialize service logic when the DataShare client connects to the DataShareExtensionAbility server. This API can be overridden as required.

System capability: SystemCapability.DistributedDataManager.DataShare.Provider

Parameters

Name Type Mandatory Description
want Want Yes Want information, including the ability name and bundle name.
callback AsyncCallback<void> Yes Callback that returns no value.

Example

import relationalStore from '@ohos.data.relationalStore'
import Want from '@ohos.app.ability.Want'

let DB_NAME = 'DB00.db';
let TBL_NAME = 'TBL00';
let DDL_TBL_CREATE = 'CREATE TABLE IF NOT EXISTS '
  + TBL_NAME
  + ' (id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT, age INTEGER, phoneNumber DOUBLE, isStudent BOOLEAN, Binary BINARY)';
let rdbStore: relationalStore.RdbStore;

export default class DataShareExtAbility extends DataShareExtensionAbility {
  onCreate(want: Want, callback: Function) {
    relationalStore.getRdbStore(this.context, {
      name: DB_NAME,
      securityLevel: relationalStore.SecurityLevel.S1
    }, (err, data) => {
      console.info(`getRdbStore done, data : ${data}`);
      rdbStore = data;
      rdbStore.executeSql(DDL_TBL_CREATE, [], (err) => {
        console.error(`executeSql done, error message : ${err}`);
      });
      if (callback) {
        callback();
      }
    });
  }
};

insert

insert?(uri: string, valueBucket: ValuesBucket, callback: AsyncCallback<number>): void

Inserts data into the database. This API can be overridden as required.

System capability: SystemCapability.DistributedDataManager.DataShare.Provider

Parameters

Name Type Mandatory Description
uri string Yes URI of the data to insert.
valueBucket ValuesBucket Yes Data to insert.
callback AsyncCallback<number> Yes Callback invoked to return the index of the data inserted.

Example

import relationalStore from '@ohos.data.relationalStore'
import { ValuesBucket } from '@ohos.data.ValuesBucket'

let TBL_NAME = 'TBL00';
let rdbStore: relationalStore.RdbStore;

export default class DataShareExtAbility extends DataShareExtensionAbility {
  insert(uri: string, valueBucket: ValuesBucket, callback: Function) {
    if (valueBucket === null) {
      console.error('invalid valueBuckets');
      return;
    }
    rdbStore.insert(TBL_NAME, valueBucket, (err, ret) => {
      console.info(`callback ret: ${ret}`);
      if (callback !== undefined) {
        callback(err, ret);
      }
    });
  }
};

update

update?(uri: string, predicates: dataSharePredicates.DataSharePredicates, valueBucket: ValuesBucket, callback: AsyncCallback<number>): void

Updates data in the database. This API can be overridden as required.

System capability: SystemCapability.DistributedDataManager.DataShare.Provider

Parameters

Name Type Mandatory Description
uri string Yes URI of the data to update.
predicates dataSharePredicates.DataSharePredicates Yes Filter criteria for updating data.
valueBucket ValuesBucket Yes New data.
callback AsyncCallback<number> Yes Callback invoked to return the number of updated data records.

Example

import relationalStore from '@ohos.data.relationalStore';
import dataSharePredicates from '@ohos.data.dataSharePredicates';
import { ValuesBucket } from '@ohos.data.ValuesBucket'

let TBL_NAME = 'TBL00';
let rdbStore: relationalStore.RdbStore;

export default class DataShareExtAbility extends DataShareExtensionAbility {
  update(uri: string, predicates: dataSharePredicates.DataSharePredicates, valueBucket: ValuesBucket, callback: Function) {
    if (predicates === null || predicates === undefined) {
      return;
    }
    rdbStore.update(TBL_NAME, valueBucket, predicates, (err, ret) => {
      if (callback !== undefined) {
        callback(err, ret);
      }
    });
  }
};

delete

delete?(uri: string, predicates: dataSharePredicates.DataSharePredicates, callback: AsyncCallback<number>): void

Deletes data from the database. This API can be overridden as required.

System capability: SystemCapability.DistributedDataManager.DataShare.Provider

Parameters

Name Type Mandatory Description
uri string Yes URI of the data to delete.
predicates dataSharePredicates.DataSharePredicates Yes Filter criteria for deleting data.
callback AsyncCallback<number> Yes Callback invoked to return the number of data records deleted.

Example

import relationalStore from '@ohos.data.relationalStore';
import dataSharePredicates from '@ohos.data.dataSharePredicates';

let TBL_NAME = 'TBL00';
let rdbStore: relationalStore.RdbStore;

export default class DataShareExtAbility extends DataShareExtensionAbility {
  delete(uri: string, predicates: dataSharePredicates.DataSharePredicates, callback: Function) {
    if (predicates === null || predicates === undefined) {
      return;
    }
    rdbStore.delete(TBL_NAME, predicates, (err, ret) => {
      if (callback !== undefined) {
        callback(err, ret);
      }
    });
  }
};

query

query?(uri: string, predicates: dataSharePredicates.DataSharePredicates, columns: Array<string>, callback: AsyncCallback<Object>): void

Queries data from the database. This API can be overridden as required.

System capability: SystemCapability.DistributedDataManager.DataShare.Provider

Parameters

Name Type Mandatory Description
uri string Yes URI of the data to query.
predicates dataSharePredicates.DataSharePredicates Yes Filter criteria for querying data.
columns Array<string> Yes Columns to query. If this parameter is empty, all columns will be queried.
callback AsyncCallback<Object> Yes Callback invoked to return the result set obtained.

Example

import relationalStore from '@ohos.data.relationalStore';
import dataSharePredicates from '@ohos.data.dataSharePredicates';

let TBL_NAME = 'TBL00';
let rdbStore: relationalStore.RdbStore;

export default class DataShareExtAbility extends DataShareExtensionAbility {
  query(uri: string, predicates: dataSharePredicates.DataSharePredicates, columns: Array<string>, callback: Function) {
    if (predicates === null || predicates === undefined) {
      return;
    }
    rdbStore.query(TBL_NAME, predicates, columns, (err, resultSet) => {
      if (resultSet !== undefined) {
        console.info(`resultSet.rowCount: ${resultSet.rowCount}`);
      }
      if (callback !== undefined) {
        callback(err, resultSet);
      }
    });
  }
};

batchInsert

batchInsert?(uri: string, valueBuckets: Array<ValuesBucket>, callback: AsyncCallback<number>): void

Batch inserts data into the database. This API is called by the server and can be overridden as required.

System capability: SystemCapability.DistributedDataManager.DataShare.Provider

Parameters

Name Type Mandatory Description
uri string Yes URI of the data to insert.
valueBuckets Array<ValuesBucket> Yes Data to insert.
callback AsyncCallback<number> Yes Callback invoked to return the number of inserted data records.

Example

import relationalStore from '@ohos.data.relationalStore';
import { ValuesBucket } from '@ohos.data.ValuesBucket'

let TBL_NAME = 'TBL00';
let rdbStore: relationalStore.RdbStore;

export default class DataShareExtAbility extends DataShareExtensionAbility {
  batchInsert(uri: string, valueBuckets: Array<ValuesBucket>, callback: Function) {
    if (valueBuckets === null || valueBuckets.length === undefined) {
      console.error('invalid valueBuckets');
      return;
    }
    rdbStore.batchInsert(TBL_NAME, valueBuckets, (err, ret) => {
      if (callback !== undefined) {
        callback(err, ret);
      }
    });
  };
};

normalizeUri

normalizeUri?(uri: string, callback: AsyncCallback<string>): void

Normalizes a URI. This API can be overridden as required.

System capability: SystemCapability.DistributedDataManager.DataShare.Provider

Parameters

Name Type Mandatory Description
uri string Yes URI to normalize.
callback AsyncCallback<string> Yes Callback used to return the result. If the operation is successful, the normalized URI is returned. Otherwise, null is returned.

Example

import { BusinessError } from '@ohos.base'

export default class DataShareExtAbility extends DataShareExtensionAbility {
  normalizeUri(uri: string, callback: Function) {
    let key = 'code';
    let value = 0;
    let err: BusinessError = {
      code: value,
      name: key,
      message: key
    };
    let ret: string = `normalize: ${uri}`;
    callback(err, ret);
  }
};

denormalizeUri

denormalizeUri?(uri: string, callback: AsyncCallback<string>): void

Denormalizes a URI. This API can be overridden as required.

System capability: SystemCapability.DistributedDataManager.DataShare.Provider

Parameters

Name Type Mandatory Description
uri string Yes URI to denormalize.
callback AsyncCallback<string> Yes Callback used to return the result. If the operation is successful, the denormalized URI is returned. If the URI passed in is returned, denormalization is not required. If denormalization is not supported, null is returned.

Example

import { BusinessError } from '@ohos.base'

export default class DataShareExtAbility extends DataShareExtensionAbility {
  denormalizeUri(uri: string, callback: Function) {
    let key = 'code';
    let value = 0;
    let err: BusinessError = {
      code: value,
      name: key,
      message: key
    };
      let ret = `denormalize ${uri}`;
      callback(err, ret);
  }
};