diff --git a/zh-cn/application-dev/database/share-data-by-datashareextensionability.md b/zh-cn/application-dev/database/share-data-by-datashareextensionability.md index 831902663ef160655c5bb85b804fde2b62e0e77e..bda623ca8a2e402873f7727414d82128817b22e8 100644 --- a/zh-cn/application-dev/database/share-data-by-datashareextensionability.md +++ b/zh-cn/application-dev/database/share-data-by-datashareextensionability.md @@ -64,6 +64,8 @@ import Extension from '@ohos.application.DataShareExtensionAbility'; import rdb from '@ohos.data.relationalStore'; import dataSharePredicates from '@ohos.data.dataSharePredicates'; + import relationalStore from '@ohos.data.relationalStore'; + import Want from '@ohos.app.ability.Want'; ``` 4. 数据提供方的业务实现由开发者自定义。例如可以通过数据库、读写文件或访问网络等各方式实现数据提供方的数据存储。 @@ -75,20 +77,20 @@ + TBL_NAME + ' (id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT, age INTEGER, isStudent BOOLEAN, Binary BINARY)'; - let rdbStore; - let result; + let rdbStore: relationalStore.RdbStore; + let result: string; export default class DataShareExtAbility extends Extension { - private rdbStore_; + private rdbStore_: relationalStore.RdbStore; // 重写onCreate接口 - onCreate(want, callback) { + onCreate(want: Want, callback: Function) { result = this.context.cacheDir + '/datashare.txt'; // 业务实现使用RDB rdb.getRdbStore(this.context, { name: DB_NAME, securityLevel: rdb.SecurityLevel.S1 - }, function (err, data) { + }, (err, data) => { rdbStore = data; rdbStore.executeSql(DDL_TBL_CREATE, [], (err) => { console.info(`DataShareExtAbility onCreate, executeSql done err:${err}`); @@ -100,7 +102,7 @@ } // 重写query接口 - query(uri, predicates, columns, callback) { + query(uri: string, predicates: dataSharePredicates.DataSharePredicates, columns: Array, callback: Function) { if (predicates === null || predicates === undefined) { console.info('invalid predicates'); } @@ -188,6 +190,8 @@ import UIAbility from '@ohos.app.ability.UIAbility'; import dataShare from '@ohos.data.dataShare'; import dataSharePredicates from '@ohos.data.dataSharePredicates'; + import { ValuesBucket } from '@ohos.data.ValuesBucket' + import window from '@ohos.window'; ``` 2. 定义与数据提供方通信的URI字符串。 @@ -200,11 +204,11 @@ 3. 创建工具接口类对象。 ```js - let dsHelper; - let abilityContext; + let dsHelper: dataShare.DataShareHelper; + let abilityContext: Context; export default class EntryAbility extends UIAbility { - onWindowStageCreate(windowStage) { + onWindowStageCreate(windowStage: window.WindowStage) { abilityContext = this.context; dataShare.createDataShareHelper(abilityContext, dseUri, (err, data) => { dsHelper = data; @@ -217,8 +221,19 @@ ```js // 构建一条数据 - let valuesBucket = { 'name': 'ZhangSan', 'age': 21, 'isStudent': false, 'Binary': new Uint8Array([1, 2, 3]) }; - let updateBucket = { 'name': 'LiSi', 'age': 18, 'isStudent': true, 'Binary': new Uint8Array([1, 2, 3]) }; + let key1 = 'name'; + let key2 = 'age'; + let key3 = 'isStudent'; + let key4 = 'Binary'; + let valueName1 = 'ZhangSan'; + let valueName2 = 'LiSi'; + let valueAge1 = 21; + let valueAge2 = 18; + let valueIsStudent1 = false; + let valueIsStudent2 = true; + let valueBinary = new Uint8Array([1, 2, 3]); + let valuesBucket: ValuesBucket = { key1: valueName1, key2: valueAge1, key3: valueIsStudent1, key4: valueBinary }; + let updateBucket: ValuesBucket = { key1: valueName2, key2: valueAge2, key3: valueIsStudent2, key4: valueBinary }; let predicates = new dataSharePredicates.DataSharePredicates(); let valArray = ['*']; // 插入一条数据 diff --git a/zh-cn/application-dev/reference/apis/js-apis-application-dataShareExtensionAbility.md b/zh-cn/application-dev/reference/apis/js-apis-application-dataShareExtensionAbility.md index 15bc8eed451e45318577a284976516647700288e..d988832ce62ba906765916f4141f5fa46bfe5f51 100644 --- a/zh-cn/application-dev/reference/apis/js-apis-application-dataShareExtensionAbility.md +++ b/zh-cn/application-dev/reference/apis/js-apis-application-dataShareExtensionAbility.md @@ -50,17 +50,17 @@ 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; +let rdbStore: relationalStore.RdbStore; export default class DataShareExtAbility extends DataShareExtensionAbility { - onCreate(want, callback) { + onCreate(want: Want, callback: Function) { rdb.getRdbStore(this.context, { name: DB_NAME, securityLevel: rdb.SecurityLevel.S1 - }, function (err, data) { + }, (err, data) => { console.info(`getRdbStore done, data : ${data}`); rdbStore = data; - rdbStore.executeSql(DDL_TBL_CREATE, [], function (err) { + rdbStore.executeSql(DDL_TBL_CREATE, [], (err) => { console.error(`executeSql done, error message : ${err}`); }); if (callback) { @@ -91,21 +91,22 @@ insert?(uri: string, valueBucket: ValuesBucket, callback: AsyncCallback<numbe ```ts import rdb from '@ohos.data.relationalStore'; +import { ValuesBucket } from '@ohos.data.ValuesBucket' 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; +let rdbStore: relationalStore.RdbStore; export default class DataShareExtAbility extends DataShareExtensionAbility { - insert(uri, valueBucket, callback) { + insert(uri: string, valueBucket: ValuesBucket, callback: Function) { if (valueBucket === null) { console.error('invalid valueBuckets'); return; } - rdbStore.insert(TBL_NAME, valueBucket, function (err, ret) { + rdbStore.insert(TBL_NAME, valueBucket, (err, ret) => { console.info(`callback ret: ${ret}`); if (callback !== undefined) { callback(err, ret); @@ -136,20 +137,22 @@ update?(uri: string, predicates: dataSharePredicates.DataSharePredicates, valueB ```ts import rdb from '@ohos.data.relationalStore'; +import dataSharePredicates from '@ohos.data.dataSharePredicates'; +import { ValuesBucket } from '@ohos.data.ValuesBucket' 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; +let rdbStore: relationalStore.RdbStore; export default class DataShareExtAbility extends DataShareExtensionAbility { - update(uri, predicates, valueBucket, callback) { + update(uri: string, predicates: dataSharePredicates.DataSharePredicates, valueBucket: ValuesBucket, callback: Function) { if (predicates === null || predicates === undefined) { return; } - rdbStore.update(TBL_NAME, valueBucket, predicates, function (err, ret) { + rdbStore.update(TBL_NAME, valueBucket, predicates, (err, ret) => { if (callback !== undefined) { callback(err, ret); } @@ -178,20 +181,21 @@ delete?(uri: string, predicates: dataSharePredicates.DataSharePredicates, callba ```ts import rdb from '@ohos.data.relationalStore'; +import dataSharePredicates from '@ohos.data.dataSharePredicates'; 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; +let rdbStore: relationalStore.RdbStore; export default class DataShareExtAbility extends DataShareExtensionAbility { - delete(uri, predicates, callback) { + delete(uri: string, predicates: dataSharePredicates.DataSharePredicates, callback: Function) { if (predicates === null || predicates === undefined) { return; } - rdbStore.delete(TBL_NAME, predicates, function (err, ret) { + rdbStore.delete(TBL_NAME, predicates, (err, ret) => { if (callback !== undefined) { callback(err, ret); } @@ -221,20 +225,21 @@ query?(uri: string, predicates: dataSharePredicates.DataSharePredicates, columns ```ts import rdb from '@ohos.data.relationalStore'; +import dataSharePredicates from '@ohos.data.dataSharePredicates'; 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; +let rdbStore: relationalStore.RdbStore; export default class DataShareExtAbility extends DataShareExtensionAbility { - query(uri, predicates, columns, callback) { + query(uri: string, predicates: dataSharePredicates.DataSharePredicates, columns: Array, callback: Function) { if (predicates === null || predicates === undefined) { return; } - rdbStore.query(TBL_NAME, predicates, columns, function (err, resultSet) { + rdbStore.query(TBL_NAME, predicates, columns, (err, resultSet) => { if (resultSet !== undefined) { console.info(`resultSet.rowCount: ${resultSet.rowCount}`); } @@ -266,21 +271,22 @@ batchInsert?(uri: string, valueBuckets: Array<ValuesBucket>, callback: Asy ```ts import rdb from '@ohos.data.relationalStore'; +import { ValuesBucket } from '@ohos.data.ValuesBucket' 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; +let rdbStore: relationalStore.RdbStore; export default class DataShareExtAbility extends DataShareExtensionAbility { - batchInsert(uri, valueBuckets, callback) { + batchInsert(uri: string, valueBuckets: Array, callback: Function) { if (valueBuckets === null || valueBuckets.length === undefined) { console.error('invalid valueBuckets'); return; } - rdbStore.batchInsert(TBL_NAME, valueBuckets, function (err, ret) { + rdbStore.batchInsert(TBL_NAME, valueBuckets, (err, ret) => { if (callback !== undefined) { callback(err, ret); } @@ -308,11 +314,17 @@ normalizeUri?(uri: string, callback: AsyncCallback<string>): void ```ts export default class DataShareExtAbility extends DataShareExtensionAbility { - normalizeUri(uri, callback) { - let err = {'code':0}; - let ret = `normalize: ${uri}`; - callback(err, ret); - } + 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); + } }; ``` @@ -335,10 +347,16 @@ denormalizeUri?(uri: string, callback: AsyncCallback<string>): void ```ts export default class DataShareExtAbility extends DataShareExtensionAbility { - denormalizeUri(uri, callback) { - let err = {'code':0}; - let ret = `denormalize ${uri}`; - callback(err, ret); - } + 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); + } }; ``` diff --git a/zh-cn/application-dev/reference/apis/js-apis-data-DataShareResultSet.md b/zh-cn/application-dev/reference/apis/js-apis-data-DataShareResultSet.md index bbf4408ef4aa8b7484da6a233886af3b7253a3fb..19c48240cb78a981159e00814e80e34567f6c92f 100644 --- a/zh-cn/application-dev/reference/apis/js-apis-data-DataShareResultSet.md +++ b/zh-cn/application-dev/reference/apis/js-apis-data-DataShareResultSet.md @@ -22,8 +22,9 @@ import DataShareResultSet from '@ohos.data.DataShareResultSet'; ```ts import dataShare from '@ohos.data.dataShare'; import dataSharePredicates from '@ohos.data.dataSharePredicates' +import { BusinessError } from '@ohos.base' -let dataShareHelper; +let dataShareHelper: dataShare.DataShareHelper; let uri = ("datashare:///com.samples.datasharetest.DataShare"); await dataShare.createDataShareHelper(this.context, uri, (err, data) => { if (err != undefined) { @@ -36,12 +37,12 @@ await dataShare.createDataShareHelper(this.context, uri, (err, data) => { let columns = ["*"]; let da = new dataSharePredicates.DataSharePredicates(); -let resultSet; +let resultSet: DataShareResultSet; da.equalTo("name", "ZhangSan"); -dataShareHelper.query(uri, da, columns).then((data) => { +dataShareHelper.query(uri, da, columns).then((data: DataShareResultSet) => { console.info("query end, data : " + data); resultSet = data; -}).catch((err) => { +}).catch((err: BusinessError) => { console.error("query fail, error message : " + err); }); ``` diff --git a/zh-cn/application-dev/reference/apis/js-apis-data-dataShare.md b/zh-cn/application-dev/reference/apis/js-apis-data-dataShare.md index 5b0f680eb5ee5e7c5bba9269c48c31769d67cd10..638af4c034f3dea509dacd760e97e39d3f8c7102 100644 --- a/zh-cn/application-dev/reference/apis/js-apis-data-dataShare.md +++ b/zh-cn/application-dev/reference/apis/js-apis-data-dataShare.md @@ -52,7 +52,7 @@ createDataShareHelper(context: Context, uri: string, callback: AsyncCallback< import UIAbility from '@ohos.app.ability.UIAbility'; let uri = ("datashare:///com.samples.datasharetest.DataShare"); -let dataShareHelper; +let dataShareHelper: dataShare.DataShareHelper; try { dataShare.createDataShareHelper(this.context, uri, (err, data) => { if (err !== undefined) { @@ -101,7 +101,7 @@ createDataShareHelper(context: Context, uri: string, options: DataShareHelperOpt import UIAbility from '@ohos.app.ability.UIAbility'; let uri = ("datashareproxy://com.samples.datasharetest.DataShare"); -let dataShareHelper; +let dataShareHelper: dataShare.DataShareHelper; try { dataShare.createDataShareHelper(this.context, uri, {isProxy : true}, (err, data) => { if (err !== undefined) { @@ -154,14 +154,15 @@ createDataShareHelper(context: Context, uri: string, options?: DataShareHelperOp ```ts import UIAbility from '@ohos.app.ability.UIAbility'; +import { BusinessError } from '@ohos.base' let uri = ("datashareproxy://com.samples.datasharetest.DataShare"); -let dataShareHelper; +let dataShareHelper: dataShare.DataShareHelper; try { - dataShare.createDataShareHelper(this.context, uri, {isProxy : true}).then((data) => { + dataShare.createDataShareHelper(this.context, uri, {isProxy : true}).then((data: dataShare.DataShareHelper) => { console.info("createDataShareHelper succeed, data : " + data); dataShareHelper = data; - }). catch((err) => { + }). catch((err: BusinessError) => { console.error(`createDataShareHelper error: code: ${err.code}, message: ${err.message} `); }); } catch (err) { @@ -269,7 +270,8 @@ on(type: 'dataChange', uri: string, callback: AsyncCallback<void>): void **示例:** ```ts -function onCallback() { +let dataShareHelper: dataShare.DataShareHelper; +let onCallback: () => void = (): void => { console.info("**** Observer on callback ****"); } let uri = ("datashare:///com.samples.datasharetest.DataShare"); @@ -295,8 +297,9 @@ off(type: 'dataChange', uri: string, callback?: AsyncCallback<void>): void **示例:** ```ts -function callback() { - console.info("**** Observer callback ****"); +let dataShareHelper: dataShare.DataShareHelper; +let callback: () => void = (): void => { + console.info("**** Observer on callback ****"); } let uri = ("datashare:///com.samples.datasharetest.DataShare"); dataShareHelper.on("dataChange", uri, callback); @@ -330,12 +333,17 @@ addTemplate(uri: string, subscriberId: string, template: Template): void **示例:** ```ts +let dataShareHelper: dataShare.DataShareHelper; let uri = ("datashareproxy://com.samples.datasharetest.DataShare"); let subscriberId = '11'; -let template = { +let key1: string = "p1"; +let value1: string = "select cityColumn as city_1, visitedCilumn as visited_1 from citys where like = true"; +let key2: string = "p2"; +let value2: string = "select cityColumn as city_2, visitedCilumn as visited_2 from citys where like = false"; +let template: dataShare.Template = { predicates : { - "p1" : "select cityColumn as city_1, visitedCilumn as visited_1 from citys where like = true", - "p2" : "select cityColumn as city_2, visitedCilumn as visited_2 from citys where like = false", + key1 : value1, + key2 : value2, }, scheduler : "select remindTimer(time) from TBL00" } @@ -368,12 +376,17 @@ delTemplate(uri: string, subscriberId: string): void **示例:** ```ts +let dataShareHelper: dataShare.DataShareHelper; let uri = ("datashareproxy://com.samples.datasharetest.DataShare"); let subscriberId = '11'; -let template = { +let key1: string = "p1"; +let value1: string = "select cityColumn as city_1, visitedCilumn as visited_1 from citys where like = true"; +let key2: string = "p2"; +let value2: string = "select cityColumn as city_2, visitedCilumn as visited_2 from citys where like = false"; +let template: dataShare.Template = { predicates : { - "p1" : "select cityColumn as city_1, visitedCilumn as visited_1 from citys where like = true", - "p2" : "select cityColumn as city_2, visitedCilumn as visited_2 from citys where like = false", + key1 : value1, + key2 : value2, }, scheduler : "select remindTimer(time) from TBL00" } @@ -407,7 +420,10 @@ on(type: 'rdbDataChange', uris: Array<string>, templateId: TemplateId, cal **示例:** ```ts -function onCallback(err, node:dataShare.RdbDataChangeNode) { +import { BusinessError } from '@ohos.base' + +let dataShareHelper: dataShare.DataShareHelper; +let onCallback: (err: BusinessError, node: dataShare.RdbDataChangeNode) => void = (err: BusinessError, node:dataShare.RdbDataChangeNode): void => { console.info("onCallback " + JSON.stringify(node.uri)); console.info("onCallback " + JSON.stringify(node.templateId)); console.info("onCallback " + node.data.length); @@ -447,6 +463,7 @@ off(type: 'rdbDataChange', uris: Array<string>, templateId: TemplateId, ca **示例:** ```ts +let dataShareHelper: dataShare.DataShareHelper; let uri = ("datashareproxy://com.samples.datasharetest.DataShare"); let templateId:dataShare.TemplateId = {subscriberId:"11", bundleNameOfOwner:"com.acts.ohos.data.datasharetest"}; let result:Array = dataShareHelper.off("rdbDataChange", [uri], templateId); @@ -478,7 +495,10 @@ on(type: 'publishedDataChange', uris: Array<string>, subscriberId: string, **示例:** ```ts -function onPublishCallback(err, node:dataShare.PublishedDataChangeNode) { +import { BusinessError } from '@ohos.base' + +let dataShareHelper: dataShare.DataShareHelper; +let onPublishCallback: (err: BusinessError, node: dataShare.PublishedDataChangeNode) => void = (err: BusinessError, node:dataShare.PublishedDataChangeNode): void => { console.info("onPublishCallback node bundleName " + JSON.stringify(node.bundleName)); console.info("onPublishCallback node data size" + node.data.length); for (let i = 0; i < node.data.length; i++) { @@ -522,7 +542,10 @@ off(type: 'publishedDataChange', uris: Array<string>, subscriberId: string **示例:** ```ts -function offCallback(err, node:dataShare.PublishedDataChangeNode) { +import { BusinessError } from '@ohos.base' + +let dataShareHelper: dataShare.DataShareHelper; +let offCallback: (err: BusinessError, node: dataShare.PublishedDataChangeNode) => void = (err: BusinessError, node:dataShare.PublishedDataChangeNode): void => { console.info("**** Observer off callback ****"); } let uris:Array = ["city", "datashareproxy://com.acts.ohos.data.datasharetest/appInfo", "key2"]; @@ -558,10 +581,13 @@ publish(data: Array<PublishedItem>, bundleName: string, version: number, c **示例:** ```ts +import { BusinessError } from '@ohos.base' + +let dataShareHelper: dataShare.DataShareHelper; let arrayBuffer = new ArrayBuffer(1); let version = 1; let dataArray : Array = [{key:"key2", subscriberId:"11", data:arrayBuffer}]; -function publishCallback(err, result: Array) { +let publishCallback: (err: BusinessError, result: Array) => void = (err: BusinessError, result: Array): void => { console.info("publishCallback " + JSON.stringify(result)); } try { @@ -599,7 +625,10 @@ publish(data: Array<PublishedItem>, bundleName: string, callback: AsyncCal | 15700012 | The data area is not exist.| ```ts -function publishCallback(err, result: Array) { +import { BusinessError } from '@ohos.base' + +let dataShareHelper: dataShare.DataShareHelper; +let publishCallback: (err: BusinessError, result: Array) => void = (err: BusinessError, result: Array): void => { console.info("publishCallback " + JSON.stringify(result)); } let dataArray : Array = [ @@ -642,11 +671,12 @@ publish(data: Array<PublishedItem>, bundleName: string, version?: number): **示例:** ```ts -let dataArray : Array = [ +let dataShareHelper: dataShare.DataShareHelper; +let dataArray: Array = [ {key:"city", subscriberId:"11", data:"xian"}, {key:"datashareproxy://com.acts.ohos.data.datasharetest/appInfo", subscriberId:"11", data:"appinfo is just a test app"}, {key:"empty", subscriberId:"11", data:"nobody sub"}]; -let result: Array = dataShareHelper.publish(dataArray, "com.acts.ohos.data.datasharetest"); +let result: Promise> = dataShareHelper.publish(dataArray, "com.acts.ohos.data.datasharetest"); ``` ### getPublishedData10+ @@ -675,7 +705,10 @@ getPublishedData(bundleName: string, callback: AsyncCallback<Array<Publish **示例:** ```ts -function publishCallback(err, data: Array) { +import { BusinessError } from '@ohos.base' + +let dataShareHelper: dataShare.DataShareHelper; +let publishCallback: (err: BusinessError, data: Array) => void = (err: BusinessError, result: Array): void => { console.info("**** Observer publish callback ****"); } dataShareHelper.getPublishedData("com.acts.ohos.data.datasharetest", publishCallback); @@ -712,7 +745,8 @@ getPublishedData(bundleName: string): Promise<Array<PublishedItem>> **示例:** ```ts -let publishedData:Array = dataShareHelper.getPublishedData("com.acts.ohos.data.datasharetest"); +let dataShareHelper: dataShare.DataShareHelper; +let publishedData: Promise> = dataShareHelper.getPublishedData("com.acts.ohos.data.datasharetest"); ``` ### insert @@ -734,11 +768,20 @@ insert(uri: string, value: ValuesBucket, callback: AsyncCallback<number>): **示例:** ```ts +import { ValuesBucket } from '@ohos.data.ValuesBucket' + +let dataShareHelper: dataShare.DataShareHelper; let uri = ("datashare:///com.samples.datasharetest.DataShare"); -const valueBucket = { - "name": "rose", - "age": 22, - "salary": 200.5, +let key1: string = "name"; +let value1: string = "rose"; +let key2: string = "age"; +let value2: number = 22; +let key3: string = "salary"; +let value3: number = 200.5; +const valueBucket: ValuesBucket = { + key1: value1, + key2: value2, + key3: value3, } try { dataShareHelper.insert(uri, valueBucket, (err, data) => { @@ -777,16 +820,26 @@ insert(uri: string, value: ValuesBucket): Promise<number> **示例:** ```ts +import { BusinessError } from '@ohos.base' +import { ValuesBucket } from '@ohos.data.ValuesBucket' + +let dataShareHelper: dataShare.DataShareHelper; let uri = ("datashare:///com.samples.datasharetest.DataShare"); -const valueBucket = { - "name": "rose1", - "age": 221, - "salary": 20.5, +let key1: string = "name"; +let value1: string = "rose1"; +let key2: string = "age"; +let value2: number = 21; +let key3: string = "salary"; +let value3: number = 20.5; +const valueBucket: ValuesBucket = { + key1: value1, + key2: value2, + key3: value3, } try { - dataShareHelper.insert(uri, valueBucket).then((data) => { + dataShareHelper.insert(uri, valueBucket).then((data: number) => { console.info("insert succeed, data : " + data); - }). catch((err) => { + }). catch((err: BusinessError) => { console.error(`insert error: code: ${err.code}, message: ${err.message} `); }); } catch (err) { @@ -815,6 +868,7 @@ delete(uri: string, predicates: dataSharePredicates.DataSharePredicates, callbac ```ts import dataSharePredicates from '@ohos.data.dataSharePredicates'; +let dataShareHelper: dataShare.DataShareHelper; let uri = ("datashare:///com.samples.datasharetest.DataShare"); let da = new dataSharePredicates.DataSharePredicates(); da.equalTo("name", "ZhangSan"); @@ -856,14 +910,16 @@ delete(uri: string, predicates: dataSharePredicates.DataSharePredicates): Promis ```ts import dataSharePredicates from '@ohos.data.dataSharePredicates'; +import { BusinessError } from '@ohos.base' +let dataShareHelper: dataShare.DataShareHelper; let uri = ("datashare:///com.samples.datasharetest.DataShare"); let da = new dataSharePredicates.DataSharePredicates(); da.equalTo("name", "ZhangSan"); try { - dataShareHelper.delete(uri, da).then((data) => { + dataShareHelper.delete(uri, da).then((data: number) => { console.info("delete succeed, data : " + data); - }). catch((err) => { + }). catch((err: BusinessError) => { console.error(`delete error: code: ${err.code}, message: ${err.message} `); }); } catch (err) { @@ -893,6 +949,7 @@ query(uri: string, predicates: dataSharePredicates.DataSharePredicates, columns: ```ts import dataSharePredicates from '@ohos.data.dataSharePredicates'; +let dataShareHelper: dataShare.DataShareHelper; let uri = ("datashare:///com.samples.datasharetest.DataShare"); let columns = ["*"]; let da = new dataSharePredicates.DataSharePredicates(); @@ -936,15 +993,17 @@ query(uri: string, predicates: dataSharePredicates.DataSharePredicates, columns: ```ts import dataSharePredicates from '@ohos.data.dataSharePredicates'; +import { BusinessError } from '@ohos.base' +let dataShareHelper: dataShare.DataShareHelper; let uri = ("datashare:///com.samples.datasharetest.DataShare"); let columns = ["*"]; let da = new dataSharePredicates.DataSharePredicates(); da.equalTo("name", "ZhangSan"); try { - dataShareHelper.query(uri, da, columns).then((data) => { + dataShareHelper.query(uri, da, columns).then((data: DataShareResultSet) => { console.info("query succeed, rowCount : " + data.rowCount); - }). catch((err) => { + }). catch((err: BusinessError) => { console.error(`query error: code: ${err.code}, message: ${err.message} `); }); } catch (err) { @@ -973,15 +1032,22 @@ update(uri: string, predicates: dataSharePredicates.DataSharePredicates, value: ```ts import dataSharePredicates from '@ohos.data.dataSharePredicates'; +import { ValuesBucket } from '@ohos.data.ValuesBucket' +let dataShareHelper: dataShare.DataShareHelper; let uri = ("datashare:///com.samples.datasharetest.DataShare"); let da = new dataSharePredicates.DataSharePredicates(); da.equalTo("name", "ZhangSan"); -const va = { - "name": "roe1", - "age": 21, - "salary": 20.5, - +let key1: string = "name"; +let value1: string = "roe1" +let key2: string = "age"; +let value2: number = 21 +let key3: string = "salary"; +let value3: number = 20.5; +const va: ValuesBucket = { + key1: value1, + key2: value2, + key3: value3, } try { dataShareHelper.update(uri, da, va, (err, data) => { @@ -1022,20 +1088,28 @@ update(uri: string, predicates: dataSharePredicates.DataSharePredicates, value: ```ts import dataSharePredicates from '@ohos.data.dataSharePredicates'; +import { ValuesBucket } from '@ohos.data.ValuesBucket' +import { BusinessError } from '@ohos.base' +let dataShareHelper: dataShare.DataShareHelper; let uri = ("datashare:///com.samples.datasharetest.DataShare"); let da = new dataSharePredicates.DataSharePredicates(); da.equalTo("name", "ZhangSan"); -const va = { - "name": "roe1", - "age": 21, - "salary": 20.5, - +let key1: string = "name"; +let value1: string = "roe1" +let key2: string = "age"; +let value2: number = 21 +let key3: string = "salary"; +let value3: number = 20.5; +const va: ValuesBucket = { + key1: value1, + key2: value2, + key3: value3, } try { - dataShareHelper.update(uri, da, va).then((data) => { + dataShareHelper.update(uri, da, va).then((data: number) => { console.info("update succeed, data : " + data); - }). catch((err) => { + }). catch((err: BusinessError) => { console.error(`update error: code: ${err.code}, message: ${err.message} `); }); } catch (err) { @@ -1062,10 +1136,22 @@ batchInsert(uri: string, values: Array<ValuesBucket>, callback: AsyncCallb **示例:** ```ts +import { ValuesBucket } from '@ohos.data.ValuesBucket' + +let dataShareHelper: dataShare.DataShareHelper; let uri = ("datashare:///com.samples.datasharetest.DataShare"); -let vbs = new Array({"name": "roe11", "age": 21, "salary": 20.5,}, - {"name": "roe12", "age": 21, "salary": 20.5,}, - {"name": "roe13", "age": 21, "salary": 20.5,}) +let key1: string = "name"; +let value11: string = "roe11" +let key2: string = "age"; +let value21: number = 21; +let key3: string = "salary"; +let value31: number = 20.5; +let valuesBucket1: ValuesBucket = { + key1: value11, + key2: value21, + key3: value31, +} +let vbs = new Array(valuesBucket1); try { dataShareHelper.batchInsert(uri, vbs, (err, data) => { if (err !== undefined) { @@ -1103,14 +1189,27 @@ batchInsert(uri: string, values: Array<ValuesBucket>): Promise<number&g **示例:** ```ts +import { ValuesBucket } from '@ohos.data.ValuesBucket' +import { BusinessError } from '@ohos.base' + +let dataShareHelper: dataShare.DataShareHelper; let uri = ("datashare:///com.samples.datasharetest.DataShare"); -let vbs = new Array({"name": "roe11", "age": 21, "salary": 20.5,}, - {"name": "roe12", "age": 21, "salary": 20.5,}, - {"name": "roe13", "age": 21, "salary": 20.5,}) +let key1: string = "name"; +let value11: string = "roe11" +let key2: string = "age"; +let value21: number = 21; +let key3: string = "salary"; +let value31: number = 20.5; +let valuesBucket1: ValuesBucket = { + key1: value11, + key2: value21, + key3: value31, +} +let vbs = new Array(valuesBucket1); try { - dataShareHelper.batchInsert(uri, vbs).then((data) => { + dataShareHelper.batchInsert(uri, vbs).then((data: number) => { console.info("batchInsert succeed, data : " + data); - }). catch((err) => { + }). catch((err: BusinessError) => { console.error(`batchInsert error: code: ${err.code}, message: ${err.message} `); }); } catch (err) { @@ -1136,6 +1235,7 @@ normalizeUri(uri: string, callback: AsyncCallback<string>): void **示例:** ```ts +let dataShareHelper: dataShare.DataShareHelper; let uri = ("datashare:///com.samples.datasharetest.DataShare"); dataShareHelper.normalizeUri(uri, (err, data) => { if (err !== undefined) { @@ -1169,10 +1269,13 @@ normalizeUri(uri: string): Promise<string> **示例:** ```ts +import { BusinessError } from '@ohos.base' + +let dataShareHelper: dataShare.DataShareHelper; let uri = ("datashare:///com.samples.datasharetest.DataShare"); -dataShareHelper.normalizeUri(uri).then((data) => { +dataShareHelper.normalizeUri(uri).then((data: string) => { console.info("normalizeUri = " + data); -}).catch((err) => { +}).catch((err: BusinessError) => { console.info("normalizeUri failed, error message : " + err); }); ``` @@ -1195,6 +1298,7 @@ denormalizeUri(uri: string, callback: AsyncCallback<string>): void **示例:** ```ts +let dataShareHelper: dataShare.DataShareHelper; let uri = ("datashare:///com.samples.datasharetest.DataShare"); dataShareHelper.denormalizeUri(uri, (err, data) => { if (err !== undefined) { @@ -1228,10 +1332,13 @@ denormalizeUri(uri: string): Promise<string> **示例:** ```ts +import { BusinessError } from '@ohos.base' + +let dataShareHelper: dataShare.DataShareHelper; let uri = ("datashare:///com.samples.datasharetest.DataShare"); -dataShareHelper.denormalizeUri(uri).then((data) => { +dataShareHelper.denormalizeUri(uri).then((data: string) => { console.info("denormalizeUri = " + data); -}).catch((err) => { +}).catch((err: BusinessError) => { console.error("denormalizeUri failed, error message : " + err); }); ``` @@ -1254,6 +1361,7 @@ notifyChange(uri: string, callback: AsyncCallback<void>): void **示例:** ```ts +let dataShareHelper: dataShare.DataShareHelper; let uri = ("datashare:///com.samples.datasharetest.DataShare"); dataShareHelper.notifyChange(uri, () => { console.info("***** notifyChange *****"); @@ -1283,6 +1391,7 @@ notifyChange(uri: string): Promise<void> **示例:** ```ts +let dataShareHelper: dataShare.DataShareHelper; let uri = ("datashare:///com.samples.datasharetest.DataShare"); dataShareHelper.notifyChange(uri); ``` \ No newline at end of file