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..ebcac27ba5d871a7b5d062a9432652b713af9861 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,17 +1136,29 @@ 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) { - console.error(`batchInsert error: code: ${err.code}, message: ${err.message} `); - return; - } - console.info("batchInsert succeed, data : " + data); + if (err !== undefined) { + console.error(`batchInsert error: code: ${err.code}, message: ${err.message} `); + return; + } + console.info("batchInsert succeed, data : " + data); }); } catch (err) { console.error(`batchInsert error: code: ${err.code}, message: ${err.message} `); @@ -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