提交 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
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册