提交 5d5029ac 编写于 作者: L liusaimei

dataShare相关使用说明文档示例代码ArkTs规范整改

Signed-off-by: Nliusaimei <liusaimei@huawei.com>
上级 ba0e70f1
......@@ -60,68 +60,69 @@
3. 在DataShareExtAbility.ts文件中,导入
`@ohos.application.DataShareExtensionAbility`模块,开发者可根据应用需求选择性重写其业务实现。例如数据提供方只提供插入、删除和查询服务,则可只重写这些接口,并导入对应的基础依赖模块。
```js
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';
```
```ts
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';
import { BusinessError } from '@ohos.base'
```
4. 数据提供方的业务实现由开发者自定义。例如可以通过数据库、读写文件或访问网络等各方式实现数据提供方的数据存储。
```js
const DB_NAME = 'DB00.db';
const TBL_NAME = 'TBL00';
const DDL_TBL_CREATE = "CREATE TABLE IF NOT EXISTS "
+ TBL_NAME
+ ' (id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT, age INTEGER, isStudent BOOLEAN, Binary BINARY)';
let rdbStore: relationalStore.RdbStore;
let result: string;
export default class DataShareExtAbility extends Extension {
private rdbStore_: relationalStore.RdbStore;
// 重写onCreate接口
onCreate(want: Want, callback: Function) {
result = this.context.cacheDir + '/datashare.txt';
// 业务实现使用RDB
rdb.getRdbStore(this.context, {
name: DB_NAME,
securityLevel: rdb.SecurityLevel.S1
}, (err, data) => {
rdbStore = data;
rdbStore.executeSql(DDL_TBL_CREATE, [], (err) => {
console.info(`DataShareExtAbility onCreate, executeSql done err:${err}`);
});
if (callback) {
callback();
}
});
}
// 重写query接口
query(uri: string, predicates: dataSharePredicates.DataSharePredicates, columns: Array<string>, callback: Function) {
if (predicates === null || predicates === undefined) {
console.info('invalid predicates');
}
try {
rdbStore.query(TBL_NAME, predicates, columns, (err, resultSet) => {
if (resultSet !== undefined) {
console.info(`resultSet.rowCount:${resultSet.rowCount}`);
}
if (callback !== undefined) {
callback(err, resultSet);
}
});
} catch (err) {
console.error(`Failed to query. Code:${err.code},message:${err.message}`);
}
}
// 可根据应用需求,选择性重写各个接口
};
```
```ts
const DB_NAME = 'DB00.db';
const TBL_NAME = 'TBL00';
const DDL_TBL_CREATE = "CREATE TABLE IF NOT EXISTS "
+ TBL_NAME
+ ' (id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT, age INTEGER, isStudent BOOLEAN, Binary BINARY)';
let rdbStore: relationalStore.RdbStore;
let result: string;
export default class DataShareExtAbility extends Extension {
// 重写onCreate接口
onCreate(want: Want, callback: Function) {
result = this.context.cacheDir + '/datashare.txt';
// 业务实现使用RDB
rdb.getRdbStore(this.context, {
name: DB_NAME,
securityLevel: rdb.SecurityLevel.S1
}, (err, data) => {
rdbStore = data;
rdbStore.executeSql(DDL_TBL_CREATE, [], (err) => {
console.info(`DataShareExtAbility onCreate, executeSql done err:${err}`);
});
if (callback) {
callback();
}
});
}
// 重写query接口
query(uri: string, predicates: dataSharePredicates.DataSharePredicates, columns: Array<string>, callback: Function) {
if (predicates === null || predicates === undefined) {
console.info('invalid predicates');
}
try {
rdbStore.query(TBL_NAME, predicates, columns, (err, resultSet) => {
if (resultSet !== undefined) {
console.info(`resultSet.rowCount:${resultSet.rowCount}`);
}
if (callback !== undefined) {
callback(err, resultSet);
}
});
} catch (err) {
let code = (err as BusinessError).code;
let message = (err as BusinessError).message
console.error(`Failed to query. Code:${code},message:${message}`);
}
}
// 可根据应用需求,选择性重写各个接口
};
```
5. 在module.json5中定义DataShareExtensionAbility。
......@@ -186,73 +187,75 @@
1. 导入基础依赖包。
```js
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';
```
```ts
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字符串。
```js
// 作为参数传递的URI,与module.json5中定义的URI的区别是多了一个"/",是因为作为参数传递的URI中,在第二个与第三个"/"中间,存在一个DeviceID的参数
let dseUri = ('datashareproxy://com.samples.datasharetest.DataShare');
```
```ts
// 作为参数传递的URI,与module.json5中定义的URI的区别是多了一个"/",是因为作为参数传递的URI中,在第二个与第三个"/"中间,存在一个DeviceID的参数
let dseUri = ('datashareproxy://com.samples.datasharetest.DataShare');
```
3. 创建工具接口类对象。
```js
let dsHelper: dataShare.DataShareHelper;
let abilityContext: Context;
export default class EntryAbility extends UIAbility {
onWindowStageCreate(windowStage: window.WindowStage) {
abilityContext = this.context;
dataShare.createDataShareHelper(abilityContext, dseUri, (err, data) => {
dsHelper = data;
});
}
}
```
```ts
let dsHelper: dataShare.DataShareHelper | undefined = undefined;
let abilityContext: Context;
export default class EntryAbility extends UIAbility {
onWindowStageCreate(windowStage: window.WindowStage) {
abilityContext = this.context;
dataShare.createDataShareHelper(abilityContext, dseUri, (err, data) => {
dsHelper = data;
});
}
}
```
4. 获取到接口类对象后,便可利用其提供的接口访问提供方提供的服务,如进行数据的增删改查等。
```js
// 构建一条数据
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 = ['*'];
// 插入一条数据
dsHelper.insert(dseUri, valuesBucket, (err, data) => {
console.info(`dsHelper insert result:${data}`);
});
// 更新数据
dsHelper.update(dseUri, predicates, updateBucket, (err, data) => {
console.info(`dsHelper update result:${data}`);
});
// 查询数据
dsHelper.query(dseUri, predicates, valArray, (err, data) => {
console.info(`dsHelper query result:${data}`);
});
// 删除指定的数据
dsHelper.delete(dseUri, predicates, (err, data) => {
console.info(`dsHelper delete result:${data}`);
});
```
```ts
// 构建一条数据
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 = ['*'];
if (dsHelper != undefined) {
// 插入一条数据
(dsHelper as dataShare.DataShareHelper).insert(dseUri, valuesBucket, (err, data) => {
console.info(`dsHelper insert result:${data}`);
});
// 更新数据
(dsHelper as dataShare.DataShareHelper).update(dseUri, predicates, updateBucket, (err, data) => {
console.info(`dsHelper update result:${data}`);
});
// 查询数据
(dsHelper as dataShare.DataShareHelper).query(dseUri, predicates, valArray, (err, data) => {
console.info(`dsHelper query result:${data}`);
});
// 删除指定的数据
(dsHelper as dataShare.DataShareHelper).delete(dseUri, predicates, (err, data) => {
console.info(`dsHelper delete result:${data}`);
});
}
```
## 相关实例
......
......@@ -43,7 +43,9 @@ DataShare客户端连接DataShareExtensionAbility服务端时,服务端回调
**示例:**
```ts
import rdb from '@ohos.data.relationalStore';
import rdb from '@ohos.data.relationalStore'
import relationalStore from '@ohos.data.relationalStore'
import Want from '@ohos.app.ability.Want'
let DB_NAME = 'DB00.db';
let TBL_NAME = 'TBL00';
......@@ -90,7 +92,7 @@ insert?(uri: string, valueBucket: ValuesBucket, callback: AsyncCallback&lt;numbe
**示例:**
```ts
import rdb from '@ohos.data.relationalStore';
import relationalStore from '@ohos.data.relationalStore'
import { ValuesBucket } from '@ohos.data.ValuesBucket'
let DB_NAME = 'DB00.db';
......@@ -136,7 +138,7 @@ update?(uri: string, predicates: dataSharePredicates.DataSharePredicates, valueB
**示例:**
```ts
import rdb from '@ohos.data.relationalStore';
import relationalStore from '@ohos.data.relationalStore';
import dataSharePredicates from '@ohos.data.dataSharePredicates';
import { ValuesBucket } from '@ohos.data.ValuesBucket'
......@@ -180,7 +182,7 @@ delete?(uri: string, predicates: dataSharePredicates.DataSharePredicates, callba
**示例:**
```ts
import rdb from '@ohos.data.relationalStore';
import relationalStore from '@ohos.data.relationalStore';
import dataSharePredicates from '@ohos.data.dataSharePredicates';
let DB_NAME = 'DB00.db';
......@@ -224,7 +226,7 @@ query?(uri: string, predicates: dataSharePredicates.DataSharePredicates, columns
**示例:**
```ts
import rdb from '@ohos.data.relationalStore';
import relationalStore from '@ohos.data.relationalStore';
import dataSharePredicates from '@ohos.data.dataSharePredicates';
let DB_NAME = 'DB00.db';
......@@ -270,7 +272,7 @@ batchInsert?(uri: string, valueBuckets: Array&lt;ValuesBucket&gt;, callback: Asy
**示例:**
```ts
import rdb from '@ohos.data.relationalStore';
import relationalStore from '@ohos.data.relationalStore';
import { ValuesBucket } from '@ohos.data.ValuesBucket'
let DB_NAME = 'DB00.db';
......@@ -313,6 +315,8 @@ normalizeUri?(uri: string, callback: AsyncCallback&lt;string&gt;): void
**示例:**
```ts
import { BusinessError } from '@ohos.base'
export default class DataShareExtAbility extends DataShareExtensionAbility {
normalizeUri(uri: string, callback: Function) {
let key = 'code';
......@@ -346,6 +350,8 @@ denormalizeUri?(uri: string, callback: AsyncCallback&lt;string&gt;): void
**示例:**
```ts
import { BusinessError } from '@ohos.base'
export default class DataShareExtAbility extends DataShareExtensionAbility {
denormalizeUri(uri: string, callback: Function) {
let key = 'code';
......
......@@ -24,27 +24,29 @@ import dataShare from '@ohos.data.dataShare';
import dataSharePredicates from '@ohos.data.dataSharePredicates'
import { BusinessError } from '@ohos.base'
let dataShareHelper: dataShare.DataShareHelper;
let dataShareHelper: dataShare.DataShareHelper | undefined = undefined;
let uri = ("datashare:///com.samples.datasharetest.DataShare");
await dataShare.createDataShareHelper(this.context, uri, (err, data) => {
if (err != undefined) {
console.error("createDataShareHelper fail, error message : " + err);
} else {
console.info("createDataShareHelper end, data : " + data);
dataShareHelper = data;
}
if (err != undefined) {
console.error("createDataShareHelper fail, error message : " + err);
} else {
console.info("createDataShareHelper end, data : " + data);
dataShareHelper = data;
}
});
let columns = ["*"];
let da = new dataSharePredicates.DataSharePredicates();
let resultSet: DataShareResultSet;
let resultSet: DataShareResultSet | undefined = undefined;
da.equalTo("name", "ZhangSan");
dataShareHelper.query(uri, da, columns).then((data: DataShareResultSet) => {
if (dataShareHelper != undefined) {
(dataShareHelper as dataShare.DataShareHelper).query(uri, da, columns).then((data: DataShareResultSet) => {
console.info("query end, data : " + data);
resultSet = data;
}).catch((err: BusinessError) => {
console.error("query fail, error message : " + err);
});
}).catch((err: BusinessError) => {
console.error("query fail, error message : " + err);
});
}
```
## DataShareResultSet
......@@ -78,8 +80,10 @@ goToFirstRow(): boolean
**示例:**
```ts
let isGoTOFirstRow = resultSet.goToFirstRow();
console.info('resultSet.goToFirstRow: ' + isGoTOFirstRow);
if (resultSet != undefined) {
let isGoToFirstRow = (resultSet as DataShareResultSet).goToFirstRow();
console.info('resultSet.goToFirstRow: ' + isGoToFirstRow);
}
```
### goToLastRow
......@@ -99,8 +103,10 @@ goToLastRow(): boolean
**示例:**
```ts
let isGoToLastRow = resultSet.goToLastRow();
console.info('resultSet.goToLastRow: ' + isGoToLastRow);
if (resultSet != undefined) {
let isGoToLastRow = (resultSet as DataShareResultSet).goToLastRow();
console.info('resultSet.goToLastRow: ' + isGoToLastRow);
}
```
### goToNextRow
......@@ -120,8 +126,10 @@ goToNextRow(): boolean
**示例:**
```ts
let isGoToNextRow = resultSet.goToNextRow();
console.info('resultSet.goToNextRow: ' + isGoToNextRow);
if (resultSet != undefined) {
let isGoToNextRow = (resultSet as DataShareResultSet).goToNextRow();
console.info('resultSet.goToNextRow: ' + isGoToNextRow);
}
```
### goToPreviousRow
......@@ -141,8 +149,10 @@ goToPreviousRow(): boolean
**示例:**
```ts
let isGoToPreviousRow = resultSet.goToPreviousRow();
console.info('resultSet.goToPreviousRow: ' + isGoToPreviousRow);
if (resultSet != undefined) {
let isGoToPreviousRow = (resultSet as DataShareResultSet).goToPreviousRow();
console.info('resultSet.goToPreviousRow: ' + isGoToPreviousRow);
}
```
### goTo
......@@ -169,8 +179,10 @@ goTo(offset:number): boolean
```ts
let goToNum = 1;
let isGoTo = resultSet.goTo(goToNum);
console.info('resultSet.goTo: ' + isGoTo);
if (resultSet != undefined) {
let isGoTo = (resultSet as DataShareResultSet).goTo(goToNum);
console.info('resultSet.goTo: ' + isGoTo);
}
```
### goToRow
......@@ -197,8 +209,10 @@ goToRow(position: number): boolean
```ts
let goToRowNum = 2;
let isGoToRow = resultSet.goToRow(goToRowNum);
console.info('resultSet.goToRow: ' + isGoToRow);
if (resultSet != undefined) {
let isGoToRow = (resultSet as DataShareResultSet).goToRow(goToRowNum);
console.info('resultSet.goToRow: ' + isGoToRow);
}
```
### getBlob
......@@ -225,9 +239,11 @@ getBlob(columnIndex: number): Uint8Array
```ts
let columnIndex = 1;
let goToFirstRow = resultSet.goToFirstRow();
let getBlob = resultSet.getBlob(columnIndex);
console.info('resultSet.getBlob: ' + getBlob);
if (resultSet != undefined) {
let goToFirstRow = (resultSet as DataShareResultSet).goToFirstRow();
let getBlob = (resultSet as DataShareResultSet).getBlob(columnIndex);
console.info('resultSet.getBlob: ' + getBlob);
}
```
### getString
......@@ -254,9 +270,11 @@ getString(columnIndex: number): string
```ts
let columnIndex = 1;
let goToFirstRow = resultSet.goToFirstRow();
let getString = resultSet.getString(columnIndex);
console.info('resultSet.getString: ' + getString);
if (resultSet != undefined) {
let goToFirstRow = (resultSet as DataShareResultSet).goToFirstRow();
let getString = (resultSet as DataShareResultSet).getString(columnIndex);
console.info('resultSet.getString: ' + getString);
}
```
### getLong
......@@ -283,9 +301,11 @@ getLong(columnIndex: number): number
```ts
let columnIndex = 1;
let goToFirstRow = resultSet.goToFirstRow();
let getLong = resultSet.getLong(columnIndex);
console.info('resultSet.getLong: ' + getLong);
if (resultSet != undefined) {
let goToFirstRow = (resultSet as DataShareResultSet).goToFirstRow();
let getLong = (resultSet as DataShareResultSet).getLong(columnIndex);
console.info('resultSet.getLong: ' + getLong);
}
```
### getDouble
......@@ -312,9 +332,11 @@ getDouble(columnIndex: number): number
```ts
let columnIndex = 1;
let goToFirstRow = resultSet.goToFirstRow();
let getDouble = resultSet.getDouble(columnIndex);
console.info('resultSet.getDouble: ' + getDouble);
if (resultSet != undefined) {
let goToFirstRow = (resultSet as DataShareResultSet).goToFirstRow();
let getDouble = (resultSet as DataShareResultSet).getDouble(columnIndex);
console.info('resultSet.getDouble: ' + getDouble);
}
```
### close
......@@ -328,7 +350,9 @@ close(): void
**示例:**
```ts
resultSet.close();
if (resultSet != undefined) {
(resultSet as DataShareResultSet).close();
}
```
### getColumnIndex
......@@ -355,8 +379,10 @@ getColumnIndex(columnName: string): number
```ts
let ColumnName = "name";
let getColumnIndex = resultSet.getColumnIndex(ColumnName);
console.info('resultSet.getColumnIndex: ' + getColumnIndex);
if (resultSet != undefined) {
let getColumnIndex = (resultSet as DataShareResultSet).getColumnIndex(ColumnName);
console.info('resultSet.getColumnIndex: ' + getColumnIndex);
}
```
### getColumnName
......@@ -383,8 +409,10 @@ getColumnName(columnIndex: number): string
```ts
let columnIndex = 1;
let getColumnName = resultSet.getColumnName(columnIndex);
console.info('resultSet.getColumnName: ' + getColumnName);
if (resultSet != undefined) {
let getColumnName = (resultSet as DataShareResultSet).getColumnName(columnIndex);
console.info('resultSet.getColumnName: ' + getColumnName);
}
```
### getDataType
......@@ -411,8 +439,10 @@ getDataType(columnIndex: number): DataType
```ts
let columnIndex = 1;
let getDataType = resultSet.getDataType(columnIndex);
console.info('resultSet.getDataType: ' + getDataType);
if (resultSet != undefined) {
let getDataType = (resultSet as DataShareResultSet).getDataType(columnIndex);
console.info('resultSet.getDataType: ' + getDataType);
}
```
## DataType
......
......@@ -49,21 +49,23 @@ createDataShareHelper(context: Context, uri: string, callback: AsyncCallback&lt;
**示例:**
```ts
import UIAbility from '@ohos.app.ability.UIAbility';
import { BusinessError } from '@ohos.base'
let uri = ("datashare:///com.samples.datasharetest.DataShare");
let dataShareHelper: dataShare.DataShareHelper;
let dataShareHelper: dataShare.DataShareHelper | undefined = undefined;
try {
dataShare.createDataShareHelper(this.context, uri, (err, data) => {
if (err !== undefined) {
console.error(`createDataShareHelper error: code: ${err.code}, message: ${err.message} `);
return;
}
console.info("createDataShareHelper succeed, data : " + data);
dataShareHelper = data;
});
dataShare.createDataShareHelper(this.context, uri, (err, data) => {
if (err !== undefined) {
console.error(`createDataShareHelper error: code: ${err.code}, message: ${err.message} `);
return;
}
console.info("createDataShareHelper succeed, data : " + data);
dataShareHelper = data;
});
} catch (err) {
console.error(`createDataShareHelper error: code: ${err.code}, message: ${err.message} `);
let code = (err as BusinessError).code;
let message = (err as BusinessError).message;
console.error(`createDataShareHelper error: code: ${code}, message: ${message} `);
};
```
......@@ -98,21 +100,23 @@ createDataShareHelper(context: Context, uri: string, options: DataShareHelperOpt
**示例:**
```ts
import UIAbility from '@ohos.app.ability.UIAbility';
import { BusinessError } from '@ohos.base'
let uri = ("datashareproxy://com.samples.datasharetest.DataShare");
let dataShareHelper: dataShare.DataShareHelper;
let dataShareHelper: dataShare.DataShareHelper | undefined = undefined;
try {
dataShare.createDataShareHelper(this.context, uri, {isProxy : true}, (err, data) => {
if (err !== undefined) {
console.error(`createDataShareHelper error: code: ${err.code}, message: ${err.message} `);
return;
}
console.info("createDataShareHelper succeed, data : " + data);
dataShareHelper = data;
});
dataShare.createDataShareHelper(this.context, uri, {isProxy : true}, (err, data) => {
if (err !== undefined) {
console.error(`createDataShareHelper error: code: ${err.code}, message: ${err.message} `);
return;
}
console.info("createDataShareHelper succeed, data : " + data);
dataShareHelper = data;
});
} catch (err) {
console.error(`createDataShareHelper error: code: ${err.code}, message: ${err.message} `);
let code = (err as BusinessError).code;
let message = (err as BusinessError).message;
console.error(`createDataShareHelper error: code: ${code}, message: ${message} `);
};
```
## dataShare.createDataShareHelper
......@@ -153,20 +157,21 @@ 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: dataShare.DataShareHelper;
let dataShareHelper: dataShare.DataShareHelper | undefined = undefined;
try {
dataShare.createDataShareHelper(this.context, uri, {isProxy : true}).then((data: dataShare.DataShareHelper) => {
console.info("createDataShareHelper succeed, data : " + data);
dataShareHelper = data;
}). catch((err: BusinessError) => {
console.error(`createDataShareHelper error: code: ${err.code}, message: ${err.message} `);
});
} catch (err) {
dataShare.createDataShareHelper(this.context, uri, {isProxy : true}).then((data: dataShare.DataShareHelper) => {
console.info("createDataShareHelper succeed, data : " + data);
dataShareHelper = data;
}). catch((err: BusinessError) => {
console.error(`createDataShareHelper error: code: ${err.code}, message: ${err.message} `);
});
} catch (err) {
let code = (err as BusinessError).code;
let message = (err as BusinessError).message;
console.error(`createDataShareHelper error: code: ${code}, message: ${message} `);
};
```
......@@ -270,12 +275,13 @@ on(type: 'dataChange', uri: string, callback: AsyncCallback&lt;void&gt;): void
**示例:**
```ts
let dataShareHelper: dataShare.DataShareHelper;
let onCallback: () => void = (): void => {
console.info("**** Observer on callback ****");
console.info("**** Observer on callback ****");
}
let uri = ("datashare:///com.samples.datasharetest.DataShare");
dataShareHelper.on("dataChange", uri, onCallback);
if (dataShareHelper !== undefined) {
(dataShareHelper as dataShare.DataShareHelper).on("dataChange", uri, onCallback);
}
```
### off('dataChange')
......@@ -297,13 +303,14 @@ off(type: 'dataChange', uri: string, callback?: AsyncCallback&lt;void&gt;): void
**示例:**
```ts
let dataShareHelper: dataShare.DataShareHelper;
let callback: () => void = (): void => {
console.info("**** Observer on callback ****");
console.info("**** Observer on callback ****");
}
let uri = ("datashare:///com.samples.datasharetest.DataShare");
dataShareHelper.on("dataChange", uri, callback);
dataShareHelper.off("dataChange", uri, callback);
if (dataShareHelper != undefined) {
(dataShareHelper as dataShare.DataShareHelper).on("dataChange", uri, callback);
(dataShareHelper as dataShare.DataShareHelper).off("dataChange", uri, callback);
}
```
### addTemplate<sup>10+</sup>
......@@ -333,7 +340,6 @@ addTemplate(uri: string, subscriberId: string, template: Template): void
**示例:**
```ts
let dataShareHelper: dataShare.DataShareHelper;
let uri = ("datashareproxy://com.samples.datasharetest.DataShare");
let subscriberId = '11';
let key1: string = "p1";
......@@ -341,13 +347,15 @@ let value1: string = "select cityColumn as city_1, visitedCilumn as visited_1 fr
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 : {
predicates : {
key1 : value1,
key2 : value2,
},
scheduler : "select remindTimer(time) from TBL00"
},
scheduler : "select remindTimer(time) from TBL00"
}
if (dataShareHelper != undefined) {
(dataShareHelper as dataShare.DataShareHelper).addTemplate(uri, subscriberId, template);
}
dataShareHelper.addTemplate(uri, subscriberId, template);
```
### delTemplate<sup>10+</sup>
......@@ -376,7 +384,6 @@ delTemplate(uri: string, subscriberId: string): void
**示例:**
```ts
let dataShareHelper: dataShare.DataShareHelper;
let uri = ("datashareproxy://com.samples.datasharetest.DataShare");
let subscriberId = '11';
let key1: string = "p1";
......@@ -384,14 +391,16 @@ let value1: string = "select cityColumn as city_1, visitedCilumn as visited_1 fr
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 : {
key1 : value1,
key2 : value2,
},
scheduler : "select remindTimer(time) from TBL00"
predicates : {
key1 : value1,
key2 : value2,
},
scheduler : "select remindTimer(time) from TBL00"
}
if (dataShareHelper != undefined) {
(dataShareHelper as dataShare.DataShareHelper).addTemplate(uri, subscriberId, template);
(dataShareHelper as dataShare.DataShareHelper).delTemplate(uri, subscriberId);
}
dataShareHelper.addTemplate(uri, subscriberId, template);
dataShareHelper.delTemplate(uri, subscriberId);
```
### on('rdbDataChange')<sup>10+</sup>
......@@ -422,19 +431,20 @@ on(type: 'rdbDataChange', uris: Array&lt;string&gt;, templateId: TemplateId, cal
```ts
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);
for (let i = 0; i < node.data.length; i++) {
console.info("onCallback " + typeof node.data[i] + " " + node.data[i]);
}
console.info("onCallback " + JSON.stringify(node.uri));
console.info("onCallback " + JSON.stringify(node.templateId));
console.info("onCallback " + node.data.length);
for (let i = 0; i < node.data.length; i++) {
console.info("onCallback " + typeof node.data[i] + " " + node.data[i]);
}
}
let uri = ("datashareproxy://com.samples.datasharetest.DataShare");
let templateId:dataShare.TemplateId = {subscriberId:"11", bundleNameOfOwner:"com.acts.ohos.data.datasharetest"};
let result:Array<dataShare.OperationResult> = dataShareHelper.on("rdbDataChange", [uri], templateId, onCallback);
if (dataShareHelper != undefined) {
let result: Array<dataShare.OperationResult> = (dataShareHelper as dataShare.DataShareHelper).on("rdbDataChange", [uri], templateId, onCallback);
}
```
### off('rdbDataChange')<sup>10+</sup>
......@@ -463,10 +473,11 @@ off(type: 'rdbDataChange', uris: Array&lt;string&gt;, 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<dataShare.OperationResult> = dataShareHelper.off("rdbDataChange", [uri], templateId);
if (dataShareHelper != undefined) {
let result: Array<dataShare.OperationResult> = (dataShareHelper as dataShare.DataShareHelper).off("rdbDataChange", [uri], templateId);
}
```
### on('publishedDataChange')<sup>10+</sup>
......@@ -497,23 +508,24 @@ on(type: 'publishedDataChange', uris: Array&lt;string&gt;, subscriberId: string,
```ts
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++) {
console.info("onPublishCallback node " + typeof node.data[i].data);
if (typeof node.data[i].data != 'string') {
let array:ArrayBuffer = node.data[i].data as ArrayBuffer;
let data:Uint8Array = new Uint8Array(array);
console.info("onPublishCallback " + i + " " + JSON.stringify(data));
}
console.info("onPublishCallback data " + i + " " + JSON.stringify(node.data[i]));
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++) {
console.info("onPublishCallback node " + typeof node.data[i].data);
if (typeof node.data[i].data != 'string') {
let array: ArrayBuffer = node.data[i].data as ArrayBuffer;
let data: Uint8Array = new Uint8Array(array);
console.info("onPublishCallback " + i + " " + JSON.stringify(data));
}
console.info("onPublishCallback data " + i + " " + JSON.stringify(node.data[i]));
}
}
let uris:Array<string> = ['city', 'datashareproxy://com.acts.ohos.data.datasharetest/appInfo', 'key2'];
let subscriberId = '11';
let result: Array<dataShare.OperationResult> = dataShareHelper.on('publishedDataChange', uris, subscriberId, onPublishCallback);
if (dataShareHelper != undefined) {
let result: Array<dataShare.OperationResult> = (dataShareHelper as dataShare.DataShareHelper).on('publishedDataChange', uris, subscriberId, onPublishCallback);
}
```
### off('publishedDataChange')<sup>10+</sup>
......@@ -544,13 +556,14 @@ off(type: 'publishedDataChange', uris: Array&lt;string&gt;, subscriberId: string
```ts
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 ****");
console.info("**** Observer off callback ****");
}
let uris:Array<string> = ["city", "datashareproxy://com.acts.ohos.data.datasharetest/appInfo", "key2"];
let subscriberId = '11';
let result: Array<dataShare.OperationResult> = dataShareHelper.off("publishedDataChange", uris, subscriberId, offCallback);
if (dataShareHelper != undefined) {
let result: Array<dataShare.OperationResult> = dataShareHelper.off("publishedDataChange", uris, subscriberId, offCallback);
}
```
### publish<sup>10+</sup>
......@@ -583,18 +596,19 @@ publish(data: Array&lt;PublishedItem&gt;, 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<dataShare.PublishedItem> = [{key:"key2", subscriberId:"11", data:arrayBuffer}];
let publishCallback: (err: BusinessError, result: Array<dataShare.OperationResult>) => void = (err: BusinessError, result: Array<dataShare.OperationResult>): void => {
console.info("publishCallback " + JSON.stringify(result));
console.info("publishCallback " + JSON.stringify(result));
}
try {
console.info("dataArray length is:", dataArray.length);
dataShareHelper.publish(dataArray, "com.acts.ohos.data.datasharetest", version, publishCallback);
console.info("dataArray length is:", dataArray.length);
if (dataShareHelper != undefined) {
(dataShareHelper as dataShare.DataShareHelper).publish(dataArray, "com.acts.ohos.data.datasharetest", version, publishCallback);
}
} catch (e) {
console.error("publish error " + JSON.stringify(e));
console.error("publish error " + JSON.stringify(e));
}
```
......@@ -627,15 +641,16 @@ publish(data: Array&lt;PublishedItem&gt;, bundleName: string, callback: AsyncCal
```ts
import { BusinessError } from '@ohos.base'
let dataShareHelper: dataShare.DataShareHelper;
let publishCallback: (err: BusinessError, result: Array<dataShare.OperationResult>) => void = (err: BusinessError, result: Array<dataShare.OperationResult>): void => {
console.info("publishCallback " + JSON.stringify(result));
console.info("publishCallback " + JSON.stringify(result));
}
let dataArray : Array<dataShare.PublishedItem> = [
{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"}];
dataShareHelper.publish(dataArray, "com.acts.ohos.data.datasharetest", publishCallback);
{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"}];
if (dataShareHelper != undefined) {
(dataShareHelper as dataShare.DataShareHelper).publish(dataArray, "com.acts.ohos.data.datasharetest", publishCallback);
}
```
### publish<sup>10+</sup>
......@@ -671,12 +686,13 @@ publish(data: Array&lt;PublishedItem&gt;, bundleName: string, version?: number):
**示例:**
```ts
let dataShareHelper: dataShare.DataShareHelper;
let dataArray: Array<dataShare.PublishedItem> = [
{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: Promise<Array<dataShare.OperationResult>> = dataShareHelper.publish(dataArray, "com.acts.ohos.data.datasharetest");
{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"}];
if (dataShareHelper != undefined) {
let result: Promise<Array<dataShare.OperationResult>> = (dataShareHelper as dataShare.DataShareHelper).publish(dataArray, "com.acts.ohos.data.datasharetest");
}
```
### getPublishedData<sup>10+</sup>
......@@ -707,11 +723,12 @@ getPublishedData(bundleName: string, callback: AsyncCallback&lt;Array&lt;Publish
```ts
import { BusinessError } from '@ohos.base'
let dataShareHelper: dataShare.DataShareHelper;
let publishCallback: (err: BusinessError, data: Array<dataShare.PublishedItem>) => void = (err: BusinessError, result: Array<dataShare.PublishedItem>): void => {
console.info("**** Observer publish callback ****");
console.info("**** Observer publish callback ****");
}
if (dataShareHelper != undefined) {
(dataShareHelper as dataShare.DataShareHelper).getPublishedData("com.acts.ohos.data.datasharetest", publishCallback);
}
dataShareHelper.getPublishedData("com.acts.ohos.data.datasharetest", publishCallback);
```
### getPublishedData<sup>10+</sup>
......@@ -745,8 +762,9 @@ getPublishedData(bundleName: string): Promise&lt;Array&lt;PublishedItem&gt;&gt;
**示例:**
```ts
let dataShareHelper: dataShare.DataShareHelper;
let publishedData: Promise<Array<dataShare.PublishedItem>> = dataShareHelper.getPublishedData("com.acts.ohos.data.datasharetest");
if (dataShareHelper != undefined) {
let publishedData: Promise<Array<dataShare.PublishedItem>> = (dataShareHelper as dataShare.DataShareHelper).getPublishedData("com.acts.ohos.data.datasharetest");
}
```
### insert
......@@ -769,8 +787,8 @@ insert(uri: string, value: ValuesBucket, callback: AsyncCallback&lt;number&gt;):
```ts
import { ValuesBucket } from '@ohos.data.ValuesBucket'
import { BusinessError } from '@ohos.base'
let dataShareHelper: dataShare.DataShareHelper;
let uri = ("datashare:///com.samples.datasharetest.DataShare");
let key1: string = "name";
let value1: string = "rose";
......@@ -779,20 +797,24 @@ let value2: number = 22;
let key3: string = "salary";
let value3: number = 200.5;
const valueBucket: ValuesBucket = {
key1: value1,
key2: value2,
key3: value3,
key1: value1,
key2: value2,
key3: value3,
}
try {
dataShareHelper.insert(uri, valueBucket, (err, data) => {
if (err !== undefined) {
console.error(`insert error: code: ${err.code}, message: ${err.message} `);
return;
}
console.info("insert succeed, data : " + data);
if (dataShareHelper != undefined) {
(dataShareHelper as dataShare.DataShareHelper).insert(uri, valueBucket, (err: BusinessError, data: number) => {
if (err !== undefined) {
console.error(`insert error: code: ${err.code}, message: ${err.message} `);
return;
}
console.info("insert succeed, data : " + data);
});
}
} catch (err) {
console.error(`insert error: code: ${err.code}, message: ${err.message} `);
let code = (err as BusinessError).code;
let message = (err as BusinessError).message;
console.error(`insert error: code: ${code}, message: ${message} `);
};
```
......@@ -823,7 +845,6 @@ insert(uri: string, value: ValuesBucket): Promise&lt;number&gt;
import { BusinessError } from '@ohos.base'
import { ValuesBucket } from '@ohos.data.ValuesBucket'
let dataShareHelper: dataShare.DataShareHelper;
let uri = ("datashare:///com.samples.datasharetest.DataShare");
let key1: string = "name";
let value1: string = "rose1";
......@@ -832,18 +853,22 @@ let value2: number = 21;
let key3: string = "salary";
let value3: number = 20.5;
const valueBucket: ValuesBucket = {
key1: value1,
key2: value2,
key3: value3,
key1: value1,
key2: value2,
key3: value3,
}
try {
dataShareHelper.insert(uri, valueBucket).then((data: number) => {
console.info("insert succeed, data : " + data);
}). catch((err: BusinessError) => {
console.error(`insert error: code: ${err.code}, message: ${err.message} `);
if (dataShareHelper != undefined) {
(dataShareHelper as dataShare.DataShareHelper).insert(uri, valueBucket).then((data: number) => {
console.info("insert succeed, data : " + data);
}).catch((err: BusinessError) => {
console.error(`insert error: code: ${err.code}, message: ${err.message} `);
});
}
} catch (err) {
console.error(`insert error: code: ${err.code}, message: ${err.message} `);
let code = (err as BusinessError).code;
let message = (err as BusinessError).message;
console.error(`insert error: code: ${code}, message: ${message} `);
};
```
......@@ -867,21 +892,25 @@ delete(uri: string, predicates: dataSharePredicates.DataSharePredicates, callbac
```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, (err, data) => {
if (err !== undefined) {
console.error(`delete error: code: ${err.code}, message: ${err.message} `);
return;
}
console.info("delete succeed, data : " + data);
if (dataShareHelper != undefined) {
(dataShareHelper as dataShare.DataShareHelper).delete(uri, da, (err: BusinessError, data: number) => {
if (err !== undefined) {
console.error(`delete error: code: ${err.code}, message: ${err.message} `);
return;
}
console.info("delete succeed, data : " + data);
});
}
} catch (err) {
console.error(`delete error: code: ${err.code}, message: ${err.message} `);
let code = (err as BusinessError).code;
let message = (err as BusinessError).message;
console.error(`delete error: code: ${code}, message: ${message} `);
};
```
......@@ -912,18 +941,21 @@ delete(uri: string, predicates: dataSharePredicates.DataSharePredicates): Promis
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: number) => {
console.info("delete succeed, data : " + data);
}). catch((err: BusinessError) => {
console.error(`delete error: code: ${err.code}, message: ${err.message} `);
if (dataShareHelper != undefined) {
(dataShareHelper as dataShare.DataShareHelper).delete(uri, da).then((data: number) => {
console.info("delete succeed, data : " + data);
}).catch((err: BusinessError) => {
console.error(`delete error: code: ${err.code}, message: ${err.message} `);
});
}
} catch (err) {
console.error(`delete error: code: ${err.code}, message: ${err.message} `);
let code = (err as BusinessError).code;
let message = (err as BusinessError).message;
console.error(`delete error: code: ${code}, message: ${message} `);
};
```
......@@ -948,22 +980,27 @@ query(uri: string, predicates: dataSharePredicates.DataSharePredicates, columns:
```ts
import dataSharePredicates from '@ohos.data.dataSharePredicates';
import { BusinessError } from '@ohos.base'
import DataShareResultSet from '@ohos.data.DataShareResultSet'
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, (err, data) => {
if (err !== undefined) {
console.error(`query error: code: ${err.code}, message: ${err.message} `);
return;
}
console.info("query succeed, rowCount : " + data.rowCount);
if (dataShareHelper != undefined) {
(dataShareHelper as dataShare.DataShareHelper).query(uri, da, columns, (err: BusinessError, data: DataShareResultSet) => {
if (err !== undefined) {
console.error(`query error: code: ${err.code}, message: ${err.message} `);
return;
}
console.info("query succeed, rowCount : " + data.rowCount);
});
}
} catch (err) {
console.error(`query error: code: ${err.code}, message: ${err.message} `);
let code = (err as BusinessError).code;
let message = (err as BusinessError).message;
console.error(`query error: code: ${code}, message: ${message} `);
};
```
......@@ -994,20 +1031,24 @@ query(uri: string, predicates: dataSharePredicates.DataSharePredicates, columns:
```ts
import dataSharePredicates from '@ohos.data.dataSharePredicates';
import { BusinessError } from '@ohos.base'
import DataShareResultSet from '@ohos.data.DataShareResultSet'
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: DataShareResultSet) => {
console.info("query succeed, rowCount : " + data.rowCount);
}). catch((err: BusinessError) => {
console.error(`query error: code: ${err.code}, message: ${err.message} `);
if (dataShareHelper != undefined) {
(dataShareHelper as dataShare.DataShareHelper).query(uri, da, columns).then((data: DataShareResultSet) => {
console.info("query succeed, rowCount : " + data.rowCount);
}).catch((err: BusinessError) => {
console.error(`query error: code: ${err.code}, message: ${err.message} `);
});
}
} catch (err) {
console.error(`query error: code: ${err.code}, message: ${err.message} `);
let code = (err as BusinessError).code;
let message = (err as BusinessError).message;
console.error(`query error: code: ${code}, message: ${message} `);
};
```
......@@ -1032,9 +1073,9 @@ update(uri: string, predicates: dataSharePredicates.DataSharePredicates, value:
```ts
import dataSharePredicates from '@ohos.data.dataSharePredicates';
import { BusinessError } from '@ohos.base'
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");
......@@ -1045,20 +1086,24 @@ let value2: number = 21
let key3: string = "salary";
let value3: number = 20.5;
const va: ValuesBucket = {
key1: value1,
key2: value2,
key3: value3,
key1: value1,
key2: value2,
key3: value3,
}
try {
dataShareHelper.update(uri, da, va, (err, data) => {
if (err !== undefined) {
console.error(`update error: code: ${err.code}, message: ${err.message} `);
return;
}
console.info("update succeed, data : " + data);
if (dataShareHelper != undefined) {
(dataShareHelper as dataShare.DataShareHelper).update(uri, da, va, (err: BusinessError, data: number) => {
if (err !== undefined) {
console.error(`update error: code: ${err.code}, message: ${err.message} `);
return;
}
console.info("update succeed, data : " + data);
});
}
} catch (err) {
console.error(`update error: code: ${err.code}, message: ${err.message} `);
let code = (err as BusinessError).code;
let message = (err as BusinessError).message;
console.error(`update error: code: ${code}, message: ${message} `);
};
```
......@@ -1091,7 +1136,6 @@ 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");
......@@ -1102,18 +1146,22 @@ let value2: number = 21
let key3: string = "salary";
let value3: number = 20.5;
const va: ValuesBucket = {
key1: value1,
key2: value2,
key3: value3,
key1: value1,
key2: value2,
key3: value3,
}
try {
dataShareHelper.update(uri, da, va).then((data: number) => {
console.info("update succeed, data : " + data);
}). catch((err: BusinessError) => {
console.error(`update error: code: ${err.code}, message: ${err.message} `);
if (dataShareHelper != undefined) {
(dataShareHelper as dataShare.DataShareHelper).update(uri, da, va).then((data: number) => {
console.info("update succeed, data : " + data);
}).catch((err: BusinessError) => {
console.error(`update error: code: ${err.code}, message: ${err.message} `);
});
}
} catch (err) {
console.error(`update error: code: ${err.code}, message: ${err.message} `);
let code = (err as BusinessError).code;
let message = (err as BusinessError).message;
console.error(`update error: code: ${code}, message: ${message} `);
};
```
......@@ -1137,8 +1185,8 @@ batchInsert(uri: string, values: Array&lt;ValuesBucket&gt;, callback: AsyncCallb
```ts
import { ValuesBucket } from '@ohos.data.ValuesBucket'
import { BusinessError } from '@ohos.base'
let dataShareHelper: dataShare.DataShareHelper;
let uri = ("datashare:///com.samples.datasharetest.DataShare");
let key1: string = "name";
let value11: string = "roe11"
......@@ -1147,21 +1195,25 @@ let value21: number = 21;
let key3: string = "salary";
let value31: number = 20.5;
let valuesBucket1: ValuesBucket = {
key1: value11,
key2: value21,
key3: value31,
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 (dataShareHelper != undefined) {
(dataShareHelper as dataShare.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);
});
}
} catch (err) {
console.error(`batchInsert error: code: ${err.code}, message: ${err.message} `);
let code = (err as BusinessError).code;
let message = (err as BusinessError).message;
console.error(`batchInsert error: code: ${code}, message: ${message} `);
};
```
......@@ -1192,7 +1244,6 @@ batchInsert(uri: string, values: Array&lt;ValuesBucket&gt;): Promise&lt;number&g
import { ValuesBucket } from '@ohos.data.ValuesBucket'
import { BusinessError } from '@ohos.base'
let dataShareHelper: dataShare.DataShareHelper;
let uri = ("datashare:///com.samples.datasharetest.DataShare");
let key1: string = "name";
let value11: string = "roe11"
......@@ -1201,19 +1252,23 @@ let value21: number = 21;
let key3: string = "salary";
let value31: number = 20.5;
let valuesBucket1: ValuesBucket = {
key1: value11,
key2: value21,
key3: value31,
key1: value11,
key2: value21,
key3: value31,
}
let vbs = new Array(valuesBucket1);
try {
dataShareHelper.batchInsert(uri, vbs).then((data: number) => {
console.info("batchInsert succeed, data : " + data);
}). catch((err: BusinessError) => {
console.error(`batchInsert error: code: ${err.code}, message: ${err.message} `);
if (dataShareHelper != undefined) {
(dataShareHelper as dataShare.DataShareHelper).batchInsert(uri, vbs).then((data: number) => {
console.info("batchInsert succeed, data : " + data);
}).catch((err: BusinessError) => {
console.error(`batchInsert error: code: ${err.code}, message: ${err.message} `);
});
}
} catch (err) {
console.error(`batchInsert error: code: ${err.code}, message: ${err.message} `);
let code = (err as BusinessError).code;
let message = (err as BusinessError).message
console.error(`batchInsert error: code: ${code}, message: ${message} `);
};
```
......@@ -1235,15 +1290,18 @@ normalizeUri(uri: string, callback: AsyncCallback&lt;string&gt;): void
**示例:**
```ts
let dataShareHelper: dataShare.DataShareHelper;
import { BusinessError } from '@ohos.base'
let uri = ("datashare:///com.samples.datasharetest.DataShare");
dataShareHelper.normalizeUri(uri, (err, data) => {
if (dataShareHelper != undefined) {
(dataShareHelper as dataShare.DataShareHelper).normalizeUri(uri, (err: BusinessError, data: string) => {
if (err !== undefined) {
console.info("normalizeUri failed, error message : " + err);
}else{
console.info("normalizeUri = " + data);
console.info("normalizeUri failed, error message : " + err);
} else {
console.info("normalizeUri = " + data);
}
});
});
}
```
### normalizeUri
......@@ -1271,13 +1329,14 @@ normalizeUri(uri: string): Promise&lt;string&gt;
```ts
import { BusinessError } from '@ohos.base'
let dataShareHelper: dataShare.DataShareHelper;
let uri = ("datashare:///com.samples.datasharetest.DataShare");
dataShareHelper.normalizeUri(uri).then((data: string) => {
if (dataShareHelper != undefined) {
(dataShareHelper as dataShare.DataShareHelper).normalizeUri(uri).then((data: string) => {
console.info("normalizeUri = " + data);
}).catch((err: BusinessError) => {
}).catch((err: BusinessError) => {
console.info("normalizeUri failed, error message : " + err);
});
});
}
```
### denormalizeUri
......@@ -1298,15 +1357,18 @@ denormalizeUri(uri: string, callback: AsyncCallback&lt;string&gt;): void
**示例:**
```ts
let dataShareHelper: dataShare.DataShareHelper;
import { BusinessError } from '@ohos.base'
let uri = ("datashare:///com.samples.datasharetest.DataShare");
dataShareHelper.denormalizeUri(uri, (err, data) => {
if (dataShareHelper != undefined) {
(dataShareHelper as dataShare.DataShareHelper).denormalizeUri(uri, (err: BusinessError, data: string) => {
if (err !== undefined) {
console.error("denormalizeUri failed, error message : " + err);
}else{
console.info("denormalizeUri = " + data);
console.error("denormalizeUri failed, error message : " + err);
} else {
console.info("denormalizeUri = " + data);
}
});
});
}
```
### denormalizeUri
......@@ -1334,13 +1396,14 @@ denormalizeUri(uri: string): Promise&lt;string&gt;
```ts
import { BusinessError } from '@ohos.base'
let dataShareHelper: dataShare.DataShareHelper;
let uri = ("datashare:///com.samples.datasharetest.DataShare");
dataShareHelper.denormalizeUri(uri).then((data: string) => {
if (dataShareHelper != undefined) {
(dataShareHelper as dataShare.DataShareHelper).denormalizeUri(uri).then((data: string) => {
console.info("denormalizeUri = " + data);
}).catch((err: BusinessError) => {
}).catch((err: BusinessError) => {
console.error("denormalizeUri failed, error message : " + err);
});
});
}
```
### notifyChange
......@@ -1361,11 +1424,12 @@ notifyChange(uri: string, callback: AsyncCallback&lt;void&gt;): void
**示例:**
```ts
let dataShareHelper: dataShare.DataShareHelper;
let uri = ("datashare:///com.samples.datasharetest.DataShare");
dataShareHelper.notifyChange(uri, () => {
if (dataShareHelper != undefined) {
(dataShareHelper as dataShare.DataShareHelper).notifyChange(uri, () => {
console.info("***** notifyChange *****");
});
});
}
```
### notifyChange
......@@ -1391,7 +1455,8 @@ notifyChange(uri: string): Promise&lt;void&gt;
**示例:**
```ts
let dataShareHelper: dataShare.DataShareHelper;
let uri = ("datashare:///com.samples.datasharetest.DataShare");
dataShareHelper.notifyChange(uri);
if (dataShareHelper != undefined) {
(dataShareHelper as dataShare.DataShareHelper).notifyChange(uri);
}
```
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册