未验证 提交 2661d914 编写于 作者: O openharmony_ci 提交者: Gitee

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

Merge pull request !24063 from 刘赛美/monthly_20230815
......@@ -60,36 +60,34 @@
3. 在DataShareExtAbility.ts文件中,导入
`@ohos.application.DataShareExtensionAbility`模块,开发者可根据应用需求选择性重写其业务实现。例如数据提供方只提供插入、删除和查询服务,则可只重写这些接口,并导入对应的基础依赖模块。
```js
```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
```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)';
+ 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, {
relationalStore.getRdbStore(this.context, {
name: DB_NAME,
securityLevel: rdb.SecurityLevel.S1
securityLevel: relationalStore.SecurityLevel.S1
}, (err, data) => {
rdbStore = data;
rdbStore.executeSql(DDL_TBL_CREATE, [], (err) => {
......@@ -100,7 +98,7 @@
}
});
}
// 重写query接口
query(uri: string, predicates: dataSharePredicates.DataSharePredicates, columns: Array<string>, callback: Function) {
if (predicates === null || predicates === undefined) {
......@@ -116,7 +114,9 @@
}
});
} 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}`);
}
}
// 可根据应用需求,选择性重写各个接口
......@@ -186,7 +186,7 @@
1. 导入基础依赖包。
```js
```ts
import UIAbility from '@ohos.app.ability.UIAbility';
import dataShare from '@ohos.data.dataShare';
import dataSharePredicates from '@ohos.data.dataSharePredicates';
......@@ -196,17 +196,17 @@
2. 定义与数据提供方通信的URI字符串。
```js
```ts
// 作为参数传递的URI,与module.json5中定义的URI的区别是多了一个"/",是因为作为参数传递的URI中,在第二个与第三个"/"中间,存在一个DeviceID的参数
let dseUri = ('datashareproxy://com.samples.datasharetest.DataShare');
```
3. 创建工具接口类对象。
```js
let dsHelper: dataShare.DataShareHelper;
```ts
let dsHelper: dataShare.DataShareHelper | undefined = undefined;
let abilityContext: Context;
export default class EntryAbility extends UIAbility {
onWindowStageCreate(windowStage: window.WindowStage) {
abilityContext = this.context;
......@@ -219,7 +219,7 @@
4. 获取到接口类对象后,便可利用其提供的接口访问提供方提供的服务,如进行数据的增删改查等。
```js
```ts
// 构建一条数据
let key1 = 'name';
let key2 = 'age';
......@@ -236,22 +236,24 @@
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}`);
});
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,31 +43,32 @@ DataShare客户端连接DataShareExtensionAbility服务端时,服务端回调
**示例:**
```ts
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';
let DDL_TBL_CREATE = 'CREATE TABLE IF NOT EXISTS '
+ TBL_NAME
+ ' (id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT, age INTEGER, phoneNumber DOUBLE, isStudent BOOLEAN, Binary BINARY)';
+ TBL_NAME
+ ' (id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT, age INTEGER, phoneNumber DOUBLE, isStudent BOOLEAN, Binary BINARY)';
let rdbStore: relationalStore.RdbStore;
export default class DataShareExtAbility extends DataShareExtensionAbility {
onCreate(want: Want, callback: Function) {
rdb.getRdbStore(this.context, {
name: DB_NAME,
securityLevel: rdb.SecurityLevel.S1
}, (err, data) => {
console.info(`getRdbStore done, data : ${data}`);
rdbStore = data;
rdbStore.executeSql(DDL_TBL_CREATE, [], (err) => {
console.error(`executeSql done, error message : ${err}`);
});
if (callback) {
callback();
}
});
}
onCreate(want: Want, callback: Function) {
relationalStore.getRdbStore(this.context, {
name: DB_NAME,
securityLevel: relationalStore.SecurityLevel.S1
}, (err, data) => {
console.info(`getRdbStore done, data : ${data}`);
rdbStore = data;
rdbStore.executeSql(DDL_TBL_CREATE, [], (err) => {
console.error(`executeSql done, error message : ${err}`);
});
if (callback) {
callback();
}
});
}
};
```
......@@ -90,29 +91,25 @@ 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';
let TBL_NAME = 'TBL00';
let DDL_TBL_CREATE = 'CREATE TABLE IF NOT EXISTS '
+ TBL_NAME
+ ' (id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT, age INTEGER, phoneNumber DOUBLE, isStudent BOOLEAN, Binary BINARY)';
let rdbStore: relationalStore.RdbStore;
export default class DataShareExtAbility extends DataShareExtensionAbility {
insert(uri: string, valueBucket: ValuesBucket, callback: Function) {
if (valueBucket === null) {
console.error('invalid valueBuckets');
return;
}
rdbStore.insert(TBL_NAME, valueBucket, (err, ret) => {
console.info(`callback ret: ${ret}`);
if (callback !== undefined) {
callback(err, ret);
}
});
insert(uri: string, valueBucket: ValuesBucket, callback: Function) {
if (valueBucket === null) {
console.error('invalid valueBuckets');
return;
}
rdbStore.insert(TBL_NAME, valueBucket, (err, ret) => {
console.info(`callback ret: ${ret}`);
if (callback !== undefined) {
callback(err, ret);
}
});
}
};
```
......@@ -136,28 +133,24 @@ 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'
let DB_NAME = 'DB00.db';
let TBL_NAME = 'TBL00';
let DDL_TBL_CREATE = 'CREATE TABLE IF NOT EXISTS '
+ TBL_NAME
+ ' (id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT, age INTEGER, phoneNumber DOUBLE, isStudent BOOLEAN, Binary BINARY)';
let rdbStore: relationalStore.RdbStore;
export default class DataShareExtAbility extends DataShareExtensionAbility {
update(uri: string, predicates: dataSharePredicates.DataSharePredicates, valueBucket: ValuesBucket, callback: Function) {
if (predicates === null || predicates === undefined) {
return;
}
rdbStore.update(TBL_NAME, valueBucket, predicates, (err, ret) => {
if (callback !== undefined) {
callback(err, ret);
}
});
update(uri: string, predicates: dataSharePredicates.DataSharePredicates, valueBucket: ValuesBucket, callback: Function) {
if (predicates === null || predicates === undefined) {
return;
}
rdbStore.update(TBL_NAME, valueBucket, predicates, (err, ret) => {
if (callback !== undefined) {
callback(err, ret);
}
});
}
};
```
......@@ -180,27 +173,23 @@ 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';
let TBL_NAME = 'TBL00';
let DDL_TBL_CREATE = 'CREATE TABLE IF NOT EXISTS '
+ TBL_NAME
+ ' (id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT, age INTEGER, phoneNumber DOUBLE, isStudent BOOLEAN, Binary BINARY)';
let rdbStore: relationalStore.RdbStore;
export default class DataShareExtAbility extends DataShareExtensionAbility {
delete(uri: string, predicates: dataSharePredicates.DataSharePredicates, callback: Function) {
if (predicates === null || predicates === undefined) {
return;
}
rdbStore.delete(TBL_NAME, predicates, (err, ret) => {
if (callback !== undefined) {
callback(err, ret);
}
});
delete(uri: string, predicates: dataSharePredicates.DataSharePredicates, callback: Function) {
if (predicates === null || predicates === undefined) {
return;
}
rdbStore.delete(TBL_NAME, predicates, (err, ret) => {
if (callback !== undefined) {
callback(err, ret);
}
});
}
};
```
......@@ -224,30 +213,26 @@ 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';
let TBL_NAME = 'TBL00';
let DDL_TBL_CREATE = 'CREATE TABLE IF NOT EXISTS '
+ TBL_NAME
+ ' (id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT, age INTEGER, phoneNumber DOUBLE, isStudent BOOLEAN, Binary BINARY)';
let rdbStore: relationalStore.RdbStore;
export default class DataShareExtAbility extends DataShareExtensionAbility {
query(uri: string, predicates: dataSharePredicates.DataSharePredicates, columns: Array<string>, callback: Function) {
if (predicates === null || predicates === undefined) {
return;
}
rdbStore.query(TBL_NAME, predicates, columns, (err, resultSet) => {
if (resultSet !== undefined) {
console.info(`resultSet.rowCount: ${resultSet.rowCount}`);
}
if (callback !== undefined) {
callback(err, resultSet);
}
});
query(uri: string, predicates: dataSharePredicates.DataSharePredicates, columns: Array<string>, callback: Function) {
if (predicates === null || predicates === undefined) {
return;
}
rdbStore.query(TBL_NAME, predicates, columns, (err, resultSet) => {
if (resultSet !== undefined) {
console.info(`resultSet.rowCount: ${resultSet.rowCount}`);
}
if (callback !== undefined) {
callback(err, resultSet);
}
});
}
};
```
......@@ -270,14 +255,10 @@ 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';
let TBL_NAME = 'TBL00';
let DDL_TBL_CREATE = 'CREATE TABLE IF NOT EXISTS '
+ TBL_NAME
+ ' (id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT, age INTEGER, phoneNumber DOUBLE, isStudent BOOLEAN, Binary BINARY)';
let rdbStore: relationalStore.RdbStore;
export default class DataShareExtAbility extends DataShareExtensionAbility {
......@@ -313,6 +294,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 +329,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.
先完成此消息的编辑!
想要评论请 注册