未验证 提交 9eb7b2ac 编写于 作者: O openharmony_ci 提交者: Gitee

!14413 [翻译完成】#I6BSHH

Merge pull request !14413 from Annie_wang/PR13784
...@@ -13,7 +13,7 @@ The **DataShare** module allows an application to manage its own data and share ...@@ -13,7 +13,7 @@ The **DataShare** module allows an application to manage its own data and share
|query?(uri: string, predicates: DataSharePredicates, columns: Array<string>, callback: AsyncCallback<Object>): void|Queries data from the database.| |query?(uri: string, predicates: DataSharePredicates, columns: Array<string>, callback: AsyncCallback<Object>): void|Queries data from the database.|
|delete?(uri: string, predicates: DataSharePredicates, callback: AsyncCallback<number>): void|Deletes data from the database.| |delete?(uri: string, predicates: DataSharePredicates, callback: AsyncCallback<number>): void|Deletes data from the database.|
For more information, see [DataShareExtensionAbility](../reference/apis/js-apis-application-dataShareExtensionAbility.md). For details about the data provider APIs, see [DataShareExtensionAbility](../reference/apis/js-apis-application-DataShareExtensionAbility.md).
**Table 2** APIs of the data consumer **Table 2** APIs of the data consumer
...@@ -38,16 +38,16 @@ Examples are given below. ...@@ -38,16 +38,16 @@ Examples are given below.
### Data Provider Application Development (Only for System Applications) ### Data Provider Application Development (Only for System Applications)
1. Import the dependencies. 1. Import dependencies.
```ts ```ts
import Extension from '@ohos.application.DataShareExtensionAbility'; import Extension from '@ohos.application.DataShareExtensionAbility';
import rdb from '@ohos.data.rdb'; import rdb from '@ohos.data.relationalStore';
import fileIo from '@ohos.fileio'; import fileIo from '@ohos.fileio';
import dataSharePredicates from '@ohos.data.dataSharePredicates'; import dataSharePredicates from '@ohos.data.dataSharePredicates';
``` ```
2. Override **DataShareExtensionAbility** APIs based on actual requirements. For example, if the data provider provides only data query, override only the **query()** API. 2. Override **DataShareExtensionAbility** APIs based on actual requirements. For example, if the data provider provides only data query, override only **query()**.
3. Implement the data provider services. For example, implement data storage of the data provider by using a database, reading and writing files, or accessing the network. 3. Implement the data provider services. For example, implement data storage of the data provider by using a database, reading and writing files, or accessing the network.
...@@ -64,13 +64,14 @@ Examples are given below. ...@@ -64,13 +64,14 @@ Examples are given below.
export default class DataShareExtAbility extends Extension { export default class DataShareExtAbility extends Extension {
private rdbStore_; private rdbStore_;
// Override the onCreate() API. // Override onCreate().
onCreate(want, callback) { onCreate(want, callback) {
result = this.context.cacheDir + '/datashare.txt' result = this.context.cacheDir + '/datashare.txt'
// Create an RDB. // Create an RDB store.
rdb.getRdbStore(this.context, { rdb.getRdbStore(this.context, {
name: DB_NAME name: DB_NAME,
}, 1, function (err, data) { securityLevel: rdb.SecurityLevel.S1
}, function (err, data) {
rdbStore = data; rdbStore = data;
rdbStore.executeSql(DDL_TBL_CREATE, [], function (err) { rdbStore.executeSql(DDL_TBL_CREATE, [], function (err) {
console.log('DataShareExtAbility onCreate, executeSql done err:' + JSON.stringify(err)); console.log('DataShareExtAbility onCreate, executeSql done err:' + JSON.stringify(err));
...@@ -79,7 +80,7 @@ Examples are given below. ...@@ -79,7 +80,7 @@ Examples are given below.
}); });
} }
// Override the query() API. // Override query().
query(uri, predicates, columns, callback) { query(uri, predicates, columns, callback) {
if (predicates == null || predicates == undefined) { if (predicates == null || predicates == undefined) {
console.info('invalid predicates'); console.info('invalid predicates');
...@@ -109,7 +110,7 @@ Examples are given below. ...@@ -109,7 +110,7 @@ Examples are given below.
| "name" | Ability name, corresponding to the **ExtensionAbility** class name derived from **Ability**. | | "name" | Ability name, corresponding to the **ExtensionAbility** class name derived from **Ability**. |
| "type" | Ability type. The value is **dataShare**, indicating the development is based on the **datashare** template.| | "type" | Ability type. The value is **dataShare**, indicating the development is based on the **datashare** template.|
| "uri" | URI used for communication. It is the unique identifier for the data consumer to connect to the provider. | | "uri" | URI used for communication. It is the unique identifier for the data consumer to connect to the provider. |
| "visible" | Whether it is visible to other applications. Data sharing is allowed only when the value is **true**. | | "visible" | Whether it is visible to other applications. Data sharing is allowed only when the value is **true**.|
**module.json5 example** **module.json5 example**
...@@ -132,7 +133,7 @@ Examples are given below. ...@@ -132,7 +133,7 @@ Examples are given below.
1. Import dependencies. 1. Import dependencies.
```ts ```ts
import UIAbility from '@ohos.app.ability.UIAbility'; import Ability from '@ohos.application.Ability';
import dataShare from '@ohos.data.dataShare'; import dataShare from '@ohos.data.dataShare';
import dataSharePredicates from '@ohos.data.dataSharePredicates'; import dataSharePredicates from '@ohos.data.dataSharePredicates';
``` ```
...@@ -150,7 +151,7 @@ Examples are given below. ...@@ -150,7 +151,7 @@ Examples are given below.
let dsHelper; let dsHelper;
let abilityContext; let abilityContext;
export default class EntryAbility extends UIAbility { export default class MainAbility extends Ability {
onWindowStageCreate(windowStage) { onWindowStageCreate(windowStage) {
abilityContext = this.context; abilityContext = this.context;
dataShare.createDataShareHelper(abilityContext, dseUri, (err, data)=>{ dataShare.createDataShareHelper(abilityContext, dseUri, (err, data)=>{
...@@ -180,7 +181,7 @@ Examples are given below. ...@@ -180,7 +181,7 @@ Examples are given below.
dsHelper.query(dseUri, predicates, valArray, (err, data) => { dsHelper.query(dseUri, predicates, valArray, (err, data) => {
console.log("dsHelper query result: " + data); console.log("dsHelper query result: " + data);
}); });
// Delete specified data. // Delete data.
dsHelper.delete(dseUri, predicates, (err, data) => { dsHelper.delete(dseUri, predicates, (err, data) => {
console.log("dsHelper delete result: " + data); console.log("dsHelper delete result: " + data);
}); });
......
# @ohos.application.DataShareExtensionAbility # @ohos.application.DataShareExtensionAbility (DataShare Extension Ability)
The **DataShareExtensionAbility** module provides data share services based on the Extension ability. The **DataShareExtensionAbility** module provides data share services based on the Extension ability.
...@@ -22,7 +22,6 @@ import DataShareExtensionAbility from '@ohos.application.DataShareExtensionAbili ...@@ -22,7 +22,6 @@ import DataShareExtensionAbility from '@ohos.application.DataShareExtensionAbili
The URIs are in the following format: The URIs are in the following format:
**Scheme://authority/path** **Scheme://authority/path**
- *Scheme*: scheme name, which has a fixed value of **datashare** for the **DataShare** module. - *Scheme*: scheme name, which has a fixed value of **datashare** for the **DataShare** module.
- *authority*: [userinfo@]host[:port] - *authority*: [userinfo@]host[:port]
- *userinfo*: login information, which can be left unspecified. - *userinfo*: login information, which can be left unspecified.
...@@ -76,7 +75,8 @@ let rdbStore; ...@@ -76,7 +75,8 @@ let rdbStore;
export default class DataShareExtAbility extends DataShareExtensionAbility { export default class DataShareExtAbility extends DataShareExtensionAbility {
onCreate(want, callback) { onCreate(want, callback) {
rdb.getRdbStore(this.context, { rdb.getRdbStore(this.context, {
name: DB_NAME name: DB_NAME,
securityLevel: rdb.SecurityLevel.S1
}, function (err, data) { }, function (err, data) {
console.log('getRdbStore done, data : ' + data); console.log('getRdbStore done, data : ' + data);
rdbStore = data; rdbStore = data;
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册