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

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

Signed-off-by: Nliusaimei <liusaimei@huawei.com>
上级 ba0e70f1
...@@ -60,29 +60,28 @@ ...@@ -60,29 +60,28 @@
3. 在DataShareExtAbility.ts文件中,导入 3. 在DataShareExtAbility.ts文件中,导入
`@ohos.application.DataShareExtensionAbility`模块,开发者可根据应用需求选择性重写其业务实现。例如数据提供方只提供插入、删除和查询服务,则可只重写这些接口,并导入对应的基础依赖模块。 `@ohos.application.DataShareExtensionAbility`模块,开发者可根据应用需求选择性重写其业务实现。例如数据提供方只提供插入、删除和查询服务,则可只重写这些接口,并导入对应的基础依赖模块。
```js ```ts
import Extension from '@ohos.application.DataShareExtensionAbility'; import Extension from '@ohos.application.DataShareExtensionAbility';
import rdb from '@ohos.data.relationalStore'; import rdb from '@ohos.data.relationalStore';
import dataSharePredicates from '@ohos.data.dataSharePredicates'; import dataSharePredicates from '@ohos.data.dataSharePredicates';
import relationalStore from '@ohos.data.relationalStore'; import relationalStore from '@ohos.data.relationalStore';
import Want from '@ohos.app.ability.Want'; import Want from '@ohos.app.ability.Want';
``` import { BusinessError } from '@ohos.base'
```
4. 数据提供方的业务实现由开发者自定义。例如可以通过数据库、读写文件或访问网络等各方式实现数据提供方的数据存储。 4. 数据提供方的业务实现由开发者自定义。例如可以通过数据库、读写文件或访问网络等各方式实现数据提供方的数据存储。
```js ```ts
const DB_NAME = 'DB00.db'; const DB_NAME = 'DB00.db';
const TBL_NAME = 'TBL00'; const TBL_NAME = 'TBL00';
const DDL_TBL_CREATE = "CREATE TABLE IF NOT EXISTS " const DDL_TBL_CREATE = "CREATE TABLE IF NOT EXISTS "
+ TBL_NAME + TBL_NAME
+ ' (id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT, age INTEGER, isStudent BOOLEAN, Binary BINARY)'; + ' (id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT, age INTEGER, isStudent BOOLEAN, Binary BINARY)';
let rdbStore: relationalStore.RdbStore; let rdbStore: relationalStore.RdbStore;
let result: string; let result: string;
export default class DataShareExtAbility extends Extension {
private rdbStore_: relationalStore.RdbStore;
export default class DataShareExtAbility extends Extension {
// 重写onCreate接口 // 重写onCreate接口
onCreate(want: Want, callback: Function) { onCreate(want: Want, callback: Function) {
result = this.context.cacheDir + '/datashare.txt'; result = this.context.cacheDir + '/datashare.txt';
...@@ -116,12 +115,14 @@ ...@@ -116,12 +115,14 @@
} }
}); });
} catch (err) { } catch (err) {
console.error(`Failed to query. Code:${err.code},message:${err.message}`); 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。 5. 在module.json5中定义DataShareExtensionAbility。
...@@ -186,73 +187,75 @@ ...@@ -186,73 +187,75 @@
1. 导入基础依赖包。 1. 导入基础依赖包。
```js ```ts
import UIAbility from '@ohos.app.ability.UIAbility'; import UIAbility from '@ohos.app.ability.UIAbility';
import dataShare from '@ohos.data.dataShare'; import dataShare from '@ohos.data.dataShare';
import dataSharePredicates from '@ohos.data.dataSharePredicates'; import dataSharePredicates from '@ohos.data.dataSharePredicates';
import { ValuesBucket } from '@ohos.data.ValuesBucket' import { ValuesBucket } from '@ohos.data.ValuesBucket'
import window from '@ohos.window'; import window from '@ohos.window';
``` ```
2. 定义与数据提供方通信的URI字符串。 2. 定义与数据提供方通信的URI字符串。
```js ```ts
// 作为参数传递的URI,与module.json5中定义的URI的区别是多了一个"/",是因为作为参数传递的URI中,在第二个与第三个"/"中间,存在一个DeviceID的参数 // 作为参数传递的URI,与module.json5中定义的URI的区别是多了一个"/",是因为作为参数传递的URI中,在第二个与第三个"/"中间,存在一个DeviceID的参数
let dseUri = ('datashareproxy://com.samples.datasharetest.DataShare'); let dseUri = ('datashareproxy://com.samples.datasharetest.DataShare');
``` ```
3. 创建工具接口类对象。 3. 创建工具接口类对象。
```js ```ts
let dsHelper: dataShare.DataShareHelper; let dsHelper: dataShare.DataShareHelper | undefined = undefined;
let abilityContext: Context; let abilityContext: Context;
export default class EntryAbility extends UIAbility { export default class EntryAbility extends UIAbility {
onWindowStageCreate(windowStage: window.WindowStage) { onWindowStageCreate(windowStage: window.WindowStage) {
abilityContext = this.context; abilityContext = this.context;
dataShare.createDataShareHelper(abilityContext, dseUri, (err, data) => { dataShare.createDataShareHelper(abilityContext, dseUri, (err, data) => {
dsHelper = data; dsHelper = data;
}); });
} }
} }
``` ```
4. 获取到接口类对象后,便可利用其提供的接口访问提供方提供的服务,如进行数据的增删改查等。 4. 获取到接口类对象后,便可利用其提供的接口访问提供方提供的服务,如进行数据的增删改查等。
```js ```ts
// 构建一条数据 // 构建一条数据
let key1 = 'name'; let key1 = 'name';
let key2 = 'age'; let key2 = 'age';
let key3 = 'isStudent'; let key3 = 'isStudent';
let key4 = 'Binary'; let key4 = 'Binary';
let valueName1 = 'ZhangSan'; let valueName1 = 'ZhangSan';
let valueName2 = 'LiSi'; let valueName2 = 'LiSi';
let valueAge1 = 21; let valueAge1 = 21;
let valueAge2 = 18; let valueAge2 = 18;
let valueIsStudent1 = false; let valueIsStudent1 = false;
let valueIsStudent2 = true; let valueIsStudent2 = true;
let valueBinary = new Uint8Array([1, 2, 3]); let valueBinary = new Uint8Array([1, 2, 3]);
let valuesBucket: ValuesBucket = { key1: valueName1, key2: valueAge1, key3: valueIsStudent1, key4: valueBinary }; let valuesBucket: ValuesBucket = { key1: valueName1, key2: valueAge1, key3: valueIsStudent1, key4: valueBinary };
let updateBucket: ValuesBucket = { key1: valueName2, key2: valueAge2, key3: valueIsStudent2, key4: valueBinary }; let updateBucket: ValuesBucket = { key1: valueName2, key2: valueAge2, key3: valueIsStudent2, key4: valueBinary };
let predicates = new dataSharePredicates.DataSharePredicates(); let predicates = new dataSharePredicates.DataSharePredicates();
let valArray = ['*']; let valArray = ['*'];
if (dsHelper != undefined) {
// 插入一条数据 // 插入一条数据
dsHelper.insert(dseUri, valuesBucket, (err, data) => { (dsHelper as dataShare.DataShareHelper).insert(dseUri, valuesBucket, (err, data) => {
console.info(`dsHelper insert result:${data}`); console.info(`dsHelper insert result:${data}`);
}); });
// 更新数据 // 更新数据
dsHelper.update(dseUri, predicates, updateBucket, (err, data) => { (dsHelper as dataShare.DataShareHelper).update(dseUri, predicates, updateBucket, (err, data) => {
console.info(`dsHelper update result:${data}`); console.info(`dsHelper update result:${data}`);
}); });
// 查询数据 // 查询数据
dsHelper.query(dseUri, predicates, valArray, (err, data) => { (dsHelper as dataShare.DataShareHelper).query(dseUri, predicates, valArray, (err, data) => {
console.info(`dsHelper query result:${data}`); console.info(`dsHelper query result:${data}`);
}); });
// 删除指定的数据 // 删除指定的数据
dsHelper.delete(dseUri, predicates, (err, data) => { (dsHelper as dataShare.DataShareHelper).delete(dseUri, predicates, (err, data) => {
console.info(`dsHelper delete result:${data}`); console.info(`dsHelper delete result:${data}`);
}); });
``` }
```
## 相关实例 ## 相关实例
......
...@@ -43,7 +43,9 @@ DataShare客户端连接DataShareExtensionAbility服务端时,服务端回调 ...@@ -43,7 +43,9 @@ DataShare客户端连接DataShareExtensionAbility服务端时,服务端回调
**示例:** **示例:**
```ts ```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 DB_NAME = 'DB00.db';
let TBL_NAME = 'TBL00'; let TBL_NAME = 'TBL00';
...@@ -90,7 +92,7 @@ insert?(uri: string, valueBucket: ValuesBucket, callback: AsyncCallback&lt;numbe ...@@ -90,7 +92,7 @@ insert?(uri: string, valueBucket: ValuesBucket, callback: AsyncCallback&lt;numbe
**示例:** **示例:**
```ts ```ts
import rdb from '@ohos.data.relationalStore'; import relationalStore from '@ohos.data.relationalStore'
import { ValuesBucket } from '@ohos.data.ValuesBucket' import { ValuesBucket } from '@ohos.data.ValuesBucket'
let DB_NAME = 'DB00.db'; let DB_NAME = 'DB00.db';
...@@ -136,7 +138,7 @@ update?(uri: string, predicates: dataSharePredicates.DataSharePredicates, valueB ...@@ -136,7 +138,7 @@ update?(uri: string, predicates: dataSharePredicates.DataSharePredicates, valueB
**示例:** **示例:**
```ts ```ts
import rdb from '@ohos.data.relationalStore'; import relationalStore from '@ohos.data.relationalStore';
import dataSharePredicates from '@ohos.data.dataSharePredicates'; import dataSharePredicates from '@ohos.data.dataSharePredicates';
import { ValuesBucket } from '@ohos.data.ValuesBucket' import { ValuesBucket } from '@ohos.data.ValuesBucket'
...@@ -180,7 +182,7 @@ delete?(uri: string, predicates: dataSharePredicates.DataSharePredicates, callba ...@@ -180,7 +182,7 @@ delete?(uri: string, predicates: dataSharePredicates.DataSharePredicates, callba
**示例:** **示例:**
```ts ```ts
import rdb from '@ohos.data.relationalStore'; import relationalStore from '@ohos.data.relationalStore';
import dataSharePredicates from '@ohos.data.dataSharePredicates'; import dataSharePredicates from '@ohos.data.dataSharePredicates';
let DB_NAME = 'DB00.db'; let DB_NAME = 'DB00.db';
...@@ -224,7 +226,7 @@ query?(uri: string, predicates: dataSharePredicates.DataSharePredicates, columns ...@@ -224,7 +226,7 @@ query?(uri: string, predicates: dataSharePredicates.DataSharePredicates, columns
**示例:** **示例:**
```ts ```ts
import rdb from '@ohos.data.relationalStore'; import relationalStore from '@ohos.data.relationalStore';
import dataSharePredicates from '@ohos.data.dataSharePredicates'; import dataSharePredicates from '@ohos.data.dataSharePredicates';
let DB_NAME = 'DB00.db'; let DB_NAME = 'DB00.db';
...@@ -270,7 +272,7 @@ batchInsert?(uri: string, valueBuckets: Array&lt;ValuesBucket&gt;, callback: Asy ...@@ -270,7 +272,7 @@ batchInsert?(uri: string, valueBuckets: Array&lt;ValuesBucket&gt;, callback: Asy
**示例:** **示例:**
```ts ```ts
import rdb from '@ohos.data.relationalStore'; import relationalStore from '@ohos.data.relationalStore';
import { ValuesBucket } from '@ohos.data.ValuesBucket' import { ValuesBucket } from '@ohos.data.ValuesBucket'
let DB_NAME = 'DB00.db'; let DB_NAME = 'DB00.db';
...@@ -313,6 +315,8 @@ normalizeUri?(uri: string, callback: AsyncCallback&lt;string&gt;): void ...@@ -313,6 +315,8 @@ normalizeUri?(uri: string, callback: AsyncCallback&lt;string&gt;): void
**示例:** **示例:**
```ts ```ts
import { BusinessError } from '@ohos.base'
export default class DataShareExtAbility extends DataShareExtensionAbility { export default class DataShareExtAbility extends DataShareExtensionAbility {
normalizeUri(uri: string, callback: Function) { normalizeUri(uri: string, callback: Function) {
let key = 'code'; let key = 'code';
...@@ -346,6 +350,8 @@ denormalizeUri?(uri: string, callback: AsyncCallback&lt;string&gt;): void ...@@ -346,6 +350,8 @@ denormalizeUri?(uri: string, callback: AsyncCallback&lt;string&gt;): void
**示例:** **示例:**
```ts ```ts
import { BusinessError } from '@ohos.base'
export default class DataShareExtAbility extends DataShareExtensionAbility { export default class DataShareExtAbility extends DataShareExtensionAbility {
denormalizeUri(uri: string, callback: Function) { denormalizeUri(uri: string, callback: Function) {
let key = 'code'; let key = 'code';
......
...@@ -24,7 +24,7 @@ import dataShare from '@ohos.data.dataShare'; ...@@ -24,7 +24,7 @@ import dataShare from '@ohos.data.dataShare';
import dataSharePredicates from '@ohos.data.dataSharePredicates' import dataSharePredicates from '@ohos.data.dataSharePredicates'
import { BusinessError } from '@ohos.base' import { BusinessError } from '@ohos.base'
let dataShareHelper: dataShare.DataShareHelper; let dataShareHelper: dataShare.DataShareHelper | undefined = undefined;
let uri = ("datashare:///com.samples.datasharetest.DataShare"); let uri = ("datashare:///com.samples.datasharetest.DataShare");
await dataShare.createDataShareHelper(this.context, uri, (err, data) => { await dataShare.createDataShareHelper(this.context, uri, (err, data) => {
if (err != undefined) { if (err != undefined) {
...@@ -37,14 +37,16 @@ await dataShare.createDataShareHelper(this.context, uri, (err, data) => { ...@@ -37,14 +37,16 @@ await dataShare.createDataShareHelper(this.context, uri, (err, data) => {
let columns = ["*"]; let columns = ["*"];
let da = new dataSharePredicates.DataSharePredicates(); let da = new dataSharePredicates.DataSharePredicates();
let resultSet: DataShareResultSet; let resultSet: DataShareResultSet | undefined = undefined;
da.equalTo("name", "ZhangSan"); 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); console.info("query end, data : " + data);
resultSet = data; resultSet = data;
}).catch((err: BusinessError) => { }).catch((err: BusinessError) => {
console.error("query fail, error message : " + err); console.error("query fail, error message : " + err);
}); });
}
``` ```
## DataShareResultSet ## DataShareResultSet
...@@ -78,8 +80,10 @@ goToFirstRow(): boolean ...@@ -78,8 +80,10 @@ goToFirstRow(): boolean
**示例:** **示例:**
```ts ```ts
let isGoTOFirstRow = resultSet.goToFirstRow(); if (resultSet != undefined) {
console.info('resultSet.goToFirstRow: ' + isGoTOFirstRow); let isGoToFirstRow = (resultSet as DataShareResultSet).goToFirstRow();
console.info('resultSet.goToFirstRow: ' + isGoToFirstRow);
}
``` ```
### goToLastRow ### goToLastRow
...@@ -99,8 +103,10 @@ goToLastRow(): boolean ...@@ -99,8 +103,10 @@ goToLastRow(): boolean
**示例:** **示例:**
```ts ```ts
let isGoToLastRow = resultSet.goToLastRow(); if (resultSet != undefined) {
console.info('resultSet.goToLastRow: ' + isGoToLastRow); let isGoToLastRow = (resultSet as DataShareResultSet).goToLastRow();
console.info('resultSet.goToLastRow: ' + isGoToLastRow);
}
``` ```
### goToNextRow ### goToNextRow
...@@ -120,8 +126,10 @@ goToNextRow(): boolean ...@@ -120,8 +126,10 @@ goToNextRow(): boolean
**示例:** **示例:**
```ts ```ts
let isGoToNextRow = resultSet.goToNextRow(); if (resultSet != undefined) {
console.info('resultSet.goToNextRow: ' + isGoToNextRow); let isGoToNextRow = (resultSet as DataShareResultSet).goToNextRow();
console.info('resultSet.goToNextRow: ' + isGoToNextRow);
}
``` ```
### goToPreviousRow ### goToPreviousRow
...@@ -141,8 +149,10 @@ goToPreviousRow(): boolean ...@@ -141,8 +149,10 @@ goToPreviousRow(): boolean
**示例:** **示例:**
```ts ```ts
let isGoToPreviousRow = resultSet.goToPreviousRow(); if (resultSet != undefined) {
console.info('resultSet.goToPreviousRow: ' + isGoToPreviousRow); let isGoToPreviousRow = (resultSet as DataShareResultSet).goToPreviousRow();
console.info('resultSet.goToPreviousRow: ' + isGoToPreviousRow);
}
``` ```
### goTo ### goTo
...@@ -169,8 +179,10 @@ goTo(offset:number): boolean ...@@ -169,8 +179,10 @@ goTo(offset:number): boolean
```ts ```ts
let goToNum = 1; let goToNum = 1;
let isGoTo = resultSet.goTo(goToNum); if (resultSet != undefined) {
console.info('resultSet.goTo: ' + isGoTo); let isGoTo = (resultSet as DataShareResultSet).goTo(goToNum);
console.info('resultSet.goTo: ' + isGoTo);
}
``` ```
### goToRow ### goToRow
...@@ -197,8 +209,10 @@ goToRow(position: number): boolean ...@@ -197,8 +209,10 @@ goToRow(position: number): boolean
```ts ```ts
let goToRowNum = 2; let goToRowNum = 2;
let isGoToRow = resultSet.goToRow(goToRowNum); if (resultSet != undefined) {
console.info('resultSet.goToRow: ' + isGoToRow); let isGoToRow = (resultSet as DataShareResultSet).goToRow(goToRowNum);
console.info('resultSet.goToRow: ' + isGoToRow);
}
``` ```
### getBlob ### getBlob
...@@ -225,9 +239,11 @@ getBlob(columnIndex: number): Uint8Array ...@@ -225,9 +239,11 @@ getBlob(columnIndex: number): Uint8Array
```ts ```ts
let columnIndex = 1; let columnIndex = 1;
let goToFirstRow = resultSet.goToFirstRow(); if (resultSet != undefined) {
let getBlob = resultSet.getBlob(columnIndex); let goToFirstRow = (resultSet as DataShareResultSet).goToFirstRow();
console.info('resultSet.getBlob: ' + getBlob); let getBlob = (resultSet as DataShareResultSet).getBlob(columnIndex);
console.info('resultSet.getBlob: ' + getBlob);
}
``` ```
### getString ### getString
...@@ -254,9 +270,11 @@ getString(columnIndex: number): string ...@@ -254,9 +270,11 @@ getString(columnIndex: number): string
```ts ```ts
let columnIndex = 1; let columnIndex = 1;
let goToFirstRow = resultSet.goToFirstRow(); if (resultSet != undefined) {
let getString = resultSet.getString(columnIndex); let goToFirstRow = (resultSet as DataShareResultSet).goToFirstRow();
console.info('resultSet.getString: ' + getString); let getString = (resultSet as DataShareResultSet).getString(columnIndex);
console.info('resultSet.getString: ' + getString);
}
``` ```
### getLong ### getLong
...@@ -283,9 +301,11 @@ getLong(columnIndex: number): number ...@@ -283,9 +301,11 @@ getLong(columnIndex: number): number
```ts ```ts
let columnIndex = 1; let columnIndex = 1;
let goToFirstRow = resultSet.goToFirstRow(); if (resultSet != undefined) {
let getLong = resultSet.getLong(columnIndex); let goToFirstRow = (resultSet as DataShareResultSet).goToFirstRow();
console.info('resultSet.getLong: ' + getLong); let getLong = (resultSet as DataShareResultSet).getLong(columnIndex);
console.info('resultSet.getLong: ' + getLong);
}
``` ```
### getDouble ### getDouble
...@@ -312,9 +332,11 @@ getDouble(columnIndex: number): number ...@@ -312,9 +332,11 @@ getDouble(columnIndex: number): number
```ts ```ts
let columnIndex = 1; let columnIndex = 1;
let goToFirstRow = resultSet.goToFirstRow(); if (resultSet != undefined) {
let getDouble = resultSet.getDouble(columnIndex); let goToFirstRow = (resultSet as DataShareResultSet).goToFirstRow();
console.info('resultSet.getDouble: ' + getDouble); let getDouble = (resultSet as DataShareResultSet).getDouble(columnIndex);
console.info('resultSet.getDouble: ' + getDouble);
}
``` ```
### close ### close
...@@ -328,7 +350,9 @@ close(): void ...@@ -328,7 +350,9 @@ close(): void
**示例:** **示例:**
```ts ```ts
resultSet.close(); if (resultSet != undefined) {
(resultSet as DataShareResultSet).close();
}
``` ```
### getColumnIndex ### getColumnIndex
...@@ -355,8 +379,10 @@ getColumnIndex(columnName: string): number ...@@ -355,8 +379,10 @@ getColumnIndex(columnName: string): number
```ts ```ts
let ColumnName = "name"; let ColumnName = "name";
let getColumnIndex = resultSet.getColumnIndex(ColumnName); if (resultSet != undefined) {
console.info('resultSet.getColumnIndex: ' + getColumnIndex); let getColumnIndex = (resultSet as DataShareResultSet).getColumnIndex(ColumnName);
console.info('resultSet.getColumnIndex: ' + getColumnIndex);
}
``` ```
### getColumnName ### getColumnName
...@@ -383,8 +409,10 @@ getColumnName(columnIndex: number): string ...@@ -383,8 +409,10 @@ getColumnName(columnIndex: number): string
```ts ```ts
let columnIndex = 1; let columnIndex = 1;
let getColumnName = resultSet.getColumnName(columnIndex); if (resultSet != undefined) {
console.info('resultSet.getColumnName: ' + getColumnName); let getColumnName = (resultSet as DataShareResultSet).getColumnName(columnIndex);
console.info('resultSet.getColumnName: ' + getColumnName);
}
``` ```
### getDataType ### getDataType
...@@ -411,8 +439,10 @@ getDataType(columnIndex: number): DataType ...@@ -411,8 +439,10 @@ getDataType(columnIndex: number): DataType
```ts ```ts
let columnIndex = 1; let columnIndex = 1;
let getDataType = resultSet.getDataType(columnIndex); if (resultSet != undefined) {
console.info('resultSet.getDataType: ' + getDataType); let getDataType = (resultSet as DataShareResultSet).getDataType(columnIndex);
console.info('resultSet.getDataType: ' + getDataType);
}
``` ```
## DataType ## DataType
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册