diff --git a/en/application-dev/ability/fa-dataability.md b/en/application-dev/ability/fa-dataability.md index 4b098d095df7ca0bec73c485f1a83d70ae43fb3d..b9488903ccc6369a0caa56fcb7493c08e611a66b 100644 --- a/en/application-dev/ability/fa-dataability.md +++ b/en/application-dev/ability/fa-dataability.md @@ -6,13 +6,13 @@ Data ability providers can customize data access-related APIs such as data inser ## Available APIs -**Table 1** Data ability lifecycle callbacks +**Table 1** Data ability lifecycle APIs |API|Description| |:------|:------| |onInitialized|Called during ability initialization to initialize the relational database (RDB).| |update|Updates 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.| |batchInsert|Inserts multiple data records into the database.| |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 |getFileTypes|Obtains the MIME type of a file.| |getType|Obtains the MIME type matching the data specified by the URI.| |executeBatch|Operates data in the database in batches.| -|call|A customized API.| +|call|Calls a custom API.| ## How to Develop ### 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: ```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 { onInitialized(abilityInfo) { console.info('DataAbility onInitialized, abilityInfo:' + abilityInfo.bundleName) @@ -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++) { console.info('DataAbility batch insert i=' + i) 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) }) } else { @@ -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 | | ------------ | ------------------------------------------------------------ | @@ -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). ```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 (/). + 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 DAHelper = featureAbility.acquireDataAbilityHelper( urivar @@ -137,7 +149,7 @@ The basic dependency packages include: urivar, valuesBucket, (error, data) => { - expect(typeof(data)).assertEqual("number") + console.log("DAHelper insert result: " + data) } ); ``` @@ -156,7 +168,7 @@ The basic dependency packages include: urivar, da, (error, data) => { - expect(typeof(data)).assertEqual("number") + console.log("DAHelper delete result: " + data) } ); ``` @@ -176,7 +188,7 @@ The basic dependency packages include: valuesBucket, da, (error, data) => { - expect(typeof(data)).assertEqual("number") + console.log("DAHelper update result: " + data) } ); ``` @@ -197,7 +209,7 @@ The basic dependency packages include: valArray, da, (error, data) => { - expect(typeof(data)).assertEqual("object") + console.log("DAHelper query result: " + data) } ); ``` @@ -217,7 +229,7 @@ The basic dependency packages include: urivar, cars, (error, data) => { - expect(typeof(data)).assertEqual("number") + console.log("DAHelper batchInsert result: " + data) } ); ``` @@ -241,12 +253,12 @@ The basic dependency packages include: valuesBucket: {"executeBatch" : "value1",}, predicates: da, expectedCount:0, - PredicatesBackReferences: {}, + predicatesBackReferences: null, interrupted:true, } ], (error, data) => { - expect(typeof(data)).assertEqual("object") + console.log("DAHelper executeBatch result: " + data) } ); ``` @@ -265,21 +277,15 @@ The basic dependency packages include: }, predicates: da, expectedCount:0, - PredicatesBackReferences: {}, + predicatesBackReferences: null, interrupted:true, } ] ); ``` -## Development Example - -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: +## Samples -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)