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

!5685 翻译完成:4319 修改dataAbility开发资料

Merge pull request !5685 from wusongqing/TR4319
...@@ -6,13 +6,13 @@ Data ability providers can customize data access-related APIs such as data inser ...@@ -6,13 +6,13 @@ Data ability providers can customize data access-related APIs such as data inser
## Available APIs ## Available APIs
**Table 1** Data ability lifecycle callbacks **Table 1** Data ability lifecycle APIs
|API|Description| |API|Description|
|:------|:------| |:------|:------|
|onInitialized|Called during ability initialization to initialize the relational database (RDB).| |onInitialized|Called during ability initialization to initialize the relational database (RDB).|
|update|Updates data in the database.| |update|Updates data in the database.|
|query|Queries data in the database.| |query|Queries data in the database.|
|delete|Deletes one or multiple data records from the database.| |delete|Deletes one or more data records from the database.|
|normalizeUri|Normalizes the URI. A normalized URI applies to cross-device use, persistence, backup, and restore. When the context changes, it ensures that the same data item can be referenced.| |normalizeUri|Normalizes the URI. A normalized URI applies to cross-device use, persistence, backup, and restore. When the context changes, it ensures that the same data item can be referenced.|
|batchInsert|Inserts multiple data records into the database.| |batchInsert|Inserts multiple data records into the database.|
|denormalizeUri|Converts a normalized URI generated by **normalizeUri** into a denormalized URI.| |denormalizeUri|Converts a normalized URI generated by **normalizeUri** into a denormalized URI.|
...@@ -21,17 +21,25 @@ Data ability providers can customize data access-related APIs such as data inser ...@@ -21,17 +21,25 @@ Data ability providers can customize data access-related APIs such as data inser
|getFileTypes|Obtains the MIME type of a file.| |getFileTypes|Obtains the MIME type of a file.|
|getType|Obtains the MIME type matching the data specified by the URI.| |getType|Obtains the MIME type matching the data specified by the URI.|
|executeBatch|Operates data in the database in batches.| |executeBatch|Operates data in the database in batches.|
|call|A customized API.| |call|Calls a custom API.|
## How to Develop ## How to Develop
### Creating a Data Ability ### Creating a Data Ability
1. To meet the basic requirements of the database storage service, implement the **Insert**, **Query**, **Update**, and **Delete** APIs in the **Data** class to provide batch data processing. The traversal logic has been implemented by the **BatchInsert** and **ExecuteBatch** APIs. 1. To meet the basic requirements of the database storage service, implement the **Insert**, **Query**, **Update**, and **Delete** APIs in the **Data** class. The **BatchInsert** and **ExecuteBatch** APIs have already implemented the traversal logic, but not batch data processing.
The following code snippet shows how to create a Data ability: The following code snippet shows how to create a Data ability:
```javascript ```javascript
import dataAbility from '@ohos.data.dataAbility'
import dataRdb from '@ohos.data.rdb'
const TABLE_NAME = 'book'
const STORE_CONFIG = { name: 'book.db' }
const SQL_CREATE_TABLE = 'CREATE TABLE IF NOT EXISTS book(id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT NOT NULL, introduction TEXT NOT NULL)'
let rdbStore: dataRdb.RdbStore = undefined
export default { export default {
onInitialized(abilityInfo) { onInitialized(abilityInfo) {
console.info('DataAbility onInitialized, abilityInfo:' + abilityInfo.bundleName) console.info('DataAbility onInitialized, abilityInfo:' + abilityInfo.bundleName)
...@@ -50,7 +58,7 @@ Data ability providers can customize data access-related APIs such as data inser ...@@ -50,7 +58,7 @@ Data ability providers can customize data access-related APIs such as data inser
for (let i = 0;i < valueBuckets.length; i++) { for (let i = 0;i < valueBuckets.length; i++) {
console.info('DataAbility batch insert i=' + i) console.info('DataAbility batch insert i=' + i)
if (i < valueBuckets.length - 1) { if (i < valueBuckets.length - 1) {
rdbStore.insert(TABLE_NAME, valueBuckets[i], (num: number) => { rdbStore.insert(TABLE_NAME, valueBuckets[i], (err: any, num: number) => {
console.info('DataAbility batch insert ret=' + num) console.info('DataAbility batch insert ret=' + num)
}) })
} else { } else {
...@@ -76,7 +84,7 @@ Data ability providers can customize data access-related APIs such as data inser ...@@ -76,7 +84,7 @@ Data ability providers can customize data access-related APIs such as data inser
}; };
``` ```
2. Submodule Configuration 2. Configure the submodule.
| JSON Field| Description | | JSON Field| Description |
| ------------ | ------------------------------------------------------------ | | ------------ | ------------------------------------------------------------ |
...@@ -118,6 +126,10 @@ The basic dependency packages include: ...@@ -118,6 +126,10 @@ The basic dependency packages include:
For details about the APIs provided by **DataAbilityHelper**, see [DataAbilityHelper Module](../reference/apis/js-apis-dataAbilityHelper.md). For details about the APIs provided by **DataAbilityHelper**, see [DataAbilityHelper Module](../reference/apis/js-apis-dataAbilityHelper.md).
```js ```js
// Different from the URI defined in the config.json file, the URI passed in the parameter has an extra slash (/), because there is a DeviceID parameter between the second and the third slash (/). // Different from the URI defined in the config.json file, the URI passed in the parameter has an extra slash (/), because there is a DeviceID parameter between the second and the third slash (/).
import featureAbility from '@ohos.ability.featureAbility'
import ohos_data_ability from '@ohos.data.dataAbility'
import ohos_data_rdb from '@ohos.data.rdb'
var urivar = "dataability:///com.ix.DataAbility" var urivar = "dataability:///com.ix.DataAbility"
var DAHelper = featureAbility.acquireDataAbilityHelper( var DAHelper = featureAbility.acquireDataAbilityHelper(
urivar urivar
...@@ -137,7 +149,7 @@ The basic dependency packages include: ...@@ -137,7 +149,7 @@ The basic dependency packages include:
urivar, urivar,
valuesBucket, valuesBucket,
(error, data) => { (error, data) => {
expect(typeof(data)).assertEqual("number") console.log("DAHelper insert result: " + data)
} }
); );
``` ```
...@@ -156,7 +168,7 @@ The basic dependency packages include: ...@@ -156,7 +168,7 @@ The basic dependency packages include:
urivar, urivar,
da, da,
(error, data) => { (error, data) => {
expect(typeof(data)).assertEqual("number") console.log("DAHelper delete result: " + data)
} }
); );
``` ```
...@@ -176,7 +188,7 @@ The basic dependency packages include: ...@@ -176,7 +188,7 @@ The basic dependency packages include:
valuesBucket, valuesBucket,
da, da,
(error, data) => { (error, data) => {
expect(typeof(data)).assertEqual("number") console.log("DAHelper update result: " + data)
} }
); );
``` ```
...@@ -197,7 +209,7 @@ The basic dependency packages include: ...@@ -197,7 +209,7 @@ The basic dependency packages include:
valArray, valArray,
da, da,
(error, data) => { (error, data) => {
expect(typeof(data)).assertEqual("object") console.log("DAHelper query result: " + data)
} }
); );
``` ```
...@@ -217,7 +229,7 @@ The basic dependency packages include: ...@@ -217,7 +229,7 @@ The basic dependency packages include:
urivar, urivar,
cars, cars,
(error, data) => { (error, data) => {
expect(typeof(data)).assertEqual("number") console.log("DAHelper batchInsert result: " + data)
} }
); );
``` ```
...@@ -241,12 +253,12 @@ The basic dependency packages include: ...@@ -241,12 +253,12 @@ The basic dependency packages include:
valuesBucket: {"executeBatch" : "value1",}, valuesBucket: {"executeBatch" : "value1",},
predicates: da, predicates: da,
expectedCount:0, expectedCount:0,
PredicatesBackReferences: {}, predicatesBackReferences: null,
interrupted:true, interrupted:true,
} }
], ],
(error, data) => { (error, data) => {
expect(typeof(data)).assertEqual("object") console.log("DAHelper executeBatch result: " + data)
} }
); );
``` ```
...@@ -265,21 +277,15 @@ The basic dependency packages include: ...@@ -265,21 +277,15 @@ The basic dependency packages include:
}, },
predicates: da, predicates: da,
expectedCount:0, expectedCount:0,
PredicatesBackReferences: {}, predicatesBackReferences: null,
interrupted:true, interrupted:true,
} }
] ]
); );
``` ```
## Development Example ## Samples
The following sample is provided to help you better understand how to develop a Data ability:
- [DataAbility](https://gitee.com/openharmony/app_samples/tree/master/ability/DataAbility)
This sample shows how to:
Create a Data ability in the **data.ts** file in the **DataAbility** directory. The following sample is provided to help you better understand how to develop Data abilities:
Encapsulate the process of accessing the Data ability in the **MainAbility** directory. - [`DataAbility`: Creation and Access of Data Abilities (eTS, API version 8)](https://gitee.com/openharmony/app_samples/tree/master/ability/DataAbility)
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册