提交 c7a8c61c 编写于 作者: L liusaimei

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

Signed-off-by: Nliusaimei <liusaimei@huawei.com>
上级 5d5029ac
...@@ -60,69 +60,69 @@ ...@@ -60,69 +60,69 @@
3. 在DataShareExtAbility.ts文件中,导入 3. 在DataShareExtAbility.ts文件中,导入
`@ohos.application.DataShareExtensionAbility`模块,开发者可根据应用需求选择性重写其业务实现。例如数据提供方只提供插入、删除和查询服务,则可只重写这些接口,并导入对应的基础依赖模块。 `@ohos.application.DataShareExtensionAbility`模块,开发者可根据应用需求选择性重写其业务实现。例如数据提供方只提供插入、删除和查询服务,则可只重写这些接口,并导入对应的基础依赖模块。
```ts ```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' import { BusinessError } from '@ohos.base'
``` ```
4. 数据提供方的业务实现由开发者自定义。例如可以通过数据库、读写文件或访问网络等各方式实现数据提供方的数据存储。 4. 数据提供方的业务实现由开发者自定义。例如可以通过数据库、读写文件或访问网络等各方式实现数据提供方的数据存储。
```ts ```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 { 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';
// 业务实现使用RDB // 业务实现使用RDB
rdb.getRdbStore(this.context, { rdb.getRdbStore(this.context, {
name: DB_NAME, name: DB_NAME,
securityLevel: rdb.SecurityLevel.S1 securityLevel: rdb.SecurityLevel.S1
}, (err, data) => { }, (err, data) => {
rdbStore = data; rdbStore = data;
rdbStore.executeSql(DDL_TBL_CREATE, [], (err) => { rdbStore.executeSql(DDL_TBL_CREATE, [], (err) => {
console.info(`DataShareExtAbility onCreate, executeSql done err:${err}`); console.info(`DataShareExtAbility onCreate, executeSql done err:${err}`);
}); });
if (callback) { if (callback) {
callback(); callback();
} }
}); });
} }
// 重写query接口 // 重写query接口
query(uri: string, predicates: dataSharePredicates.DataSharePredicates, columns: Array<string>, callback: Function) { query(uri: string, predicates: dataSharePredicates.DataSharePredicates, columns: Array<string>, callback: Function) {
if (predicates === null || predicates === undefined) { if (predicates === null || predicates === undefined) {
console.info('invalid predicates'); console.info('invalid predicates');
} }
try { try {
rdbStore.query(TBL_NAME, predicates, columns, (err, resultSet) => { rdbStore.query(TBL_NAME, predicates, columns, (err, resultSet) => {
if (resultSet !== undefined) { if (resultSet !== undefined) {
console.info(`resultSet.rowCount:${resultSet.rowCount}`); console.info(`resultSet.rowCount:${resultSet.rowCount}`);
} }
if (callback !== undefined) { if (callback !== undefined) {
callback(err, resultSet); callback(err, resultSet);
} }
}); });
} catch (err) { } catch (err) {
let code = (err as BusinessError).code; let code = (err as BusinessError).code;
let message = (err as BusinessError).message let message = (err as BusinessError).message
console.error(`Failed to query. Code:${code},message:${message}`); console.error(`Failed to query. Code:${code},message:${message}`);
} }
} }
// 可根据应用需求,选择性重写各个接口 // 可根据应用需求,选择性重写各个接口
}; };
``` ```
5. 在module.json5中定义DataShareExtensionAbility。 5. 在module.json5中定义DataShareExtensionAbility。
...@@ -187,75 +187,75 @@ export default class DataShareExtAbility extends Extension { ...@@ -187,75 +187,75 @@ export default class DataShareExtAbility extends Extension {
1. 导入基础依赖包。 1. 导入基础依赖包。
```ts ```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字符串。
```ts ```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. 创建工具接口类对象。
```ts ```ts
let dsHelper: dataShare.DataShareHelper | undefined = undefined; 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. 获取到接口类对象后,便可利用其提供的接口访问提供方提供的服务,如进行数据的增删改查等。
```ts ```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) { if (dsHelper != undefined) {
// 插入一条数据 // 插入一条数据
(dsHelper as dataShare.DataShareHelper).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 as dataShare.DataShareHelper).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 as dataShare.DataShareHelper).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 as dataShare.DataShareHelper).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,33 +43,32 @@ DataShare客户端连接DataShareExtensionAbility服务端时,服务端回调 ...@@ -43,33 +43,32 @@ DataShare客户端连接DataShareExtensionAbility服务端时,服务端回调
**示例:** **示例:**
```ts ```ts
import rdb from '@ohos.data.relationalStore'
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'
let DB_NAME = 'DB00.db'; let DB_NAME = 'DB00.db';
let TBL_NAME = 'TBL00'; let TBL_NAME = 'TBL00';
let DDL_TBL_CREATE = 'CREATE TABLE IF NOT EXISTS ' let DDL_TBL_CREATE = 'CREATE TABLE IF NOT EXISTS '
+ TBL_NAME + TBL_NAME
+ ' (id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT, age INTEGER, phoneNumber DOUBLE, isStudent BOOLEAN, Binary BINARY)'; + ' (id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT, age INTEGER, phoneNumber DOUBLE, isStudent BOOLEAN, Binary BINARY)';
let rdbStore: relationalStore.RdbStore; let rdbStore: relationalStore.RdbStore;
export default class DataShareExtAbility extends DataShareExtensionAbility { export default class DataShareExtAbility extends DataShareExtensionAbility {
onCreate(want: Want, callback: Function) { onCreate(want: Want, callback: Function) {
rdb.getRdbStore(this.context, { relationalStore.getRdbStore(globalThis.getContext(), {
name: DB_NAME, name: DB_NAME,
securityLevel: rdb.SecurityLevel.S1 securityLevel: relationalStore.SecurityLevel.S1
}, (err, data) => { }, (err, data) => {
console.info(`getRdbStore done, data : ${data}`); console.info(`getRdbStore done, data : ${data}`);
rdbStore = data; rdbStore = data;
rdbStore.executeSql(DDL_TBL_CREATE, [], (err) => { rdbStore.executeSql(DDL_TBL_CREATE, [], (err) => {
console.error(`executeSql done, error message : ${err}`); console.error(`executeSql done, error message : ${err}`);
}); });
if (callback) { if (callback) {
callback(); callback();
} }
}); });
} }
}; };
``` ```
...@@ -95,26 +94,22 @@ insert?(uri: string, valueBucket: ValuesBucket, callback: AsyncCallback&lt;numbe ...@@ -95,26 +94,22 @@ insert?(uri: string, valueBucket: ValuesBucket, callback: AsyncCallback&lt;numbe
import relationalStore 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 TBL_NAME = 'TBL00'; 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; let rdbStore: relationalStore.RdbStore;
export default class DataShareExtAbility extends DataShareExtensionAbility { export default class DataShareExtAbility extends DataShareExtensionAbility {
insert(uri: string, valueBucket: ValuesBucket, callback: Function) { insert(uri: string, valueBucket: ValuesBucket, callback: Function) {
if (valueBucket === null) { if (valueBucket === null) {
console.error('invalid valueBuckets'); console.error('invalid valueBuckets');
return; return;
}
rdbStore.insert(TBL_NAME, valueBucket, (err, ret) => {
console.info(`callback ret: ${ret}`);
if (callback !== undefined) {
callback(err, ret);
}
});
} }
rdbStore.insert(TBL_NAME, valueBucket, (err, ret) => {
console.info(`callback ret: ${ret}`);
if (callback !== undefined) {
callback(err, ret);
}
});
}
}; };
``` ```
...@@ -142,24 +137,20 @@ import relationalStore from '@ohos.data.relationalStore'; ...@@ -142,24 +137,20 @@ 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'
let DB_NAME = 'DB00.db';
let TBL_NAME = 'TBL00'; 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; let rdbStore: relationalStore.RdbStore;
export default class DataShareExtAbility extends DataShareExtensionAbility { export default class DataShareExtAbility extends DataShareExtensionAbility {
update(uri: string, predicates: dataSharePredicates.DataSharePredicates, valueBucket: ValuesBucket, callback: Function) { update(uri: string, predicates: dataSharePredicates.DataSharePredicates, valueBucket: ValuesBucket, callback: Function) {
if (predicates === null || predicates === undefined) { if (predicates === null || predicates === undefined) {
return; return;
}
rdbStore.update(TBL_NAME, valueBucket, predicates, (err, ret) => {
if (callback !== undefined) {
callback(err, ret);
}
});
} }
rdbStore.update(TBL_NAME, valueBucket, predicates, (err, ret) => {
if (callback !== undefined) {
callback(err, ret);
}
});
}
}; };
``` ```
...@@ -185,24 +176,20 @@ delete?(uri: string, predicates: dataSharePredicates.DataSharePredicates, callba ...@@ -185,24 +176,20 @@ delete?(uri: string, predicates: dataSharePredicates.DataSharePredicates, callba
import relationalStore 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 TBL_NAME = 'TBL00'; 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; let rdbStore: relationalStore.RdbStore;
export default class DataShareExtAbility extends DataShareExtensionAbility { export default class DataShareExtAbility extends DataShareExtensionAbility {
delete(uri: string, predicates: dataSharePredicates.DataSharePredicates, callback: Function) { delete(uri: string, predicates: dataSharePredicates.DataSharePredicates, callback: Function) {
if (predicates === null || predicates === undefined) { if (predicates === null || predicates === undefined) {
return; return;
}
rdbStore.delete(TBL_NAME, predicates, (err, ret) => {
if (callback !== undefined) {
callback(err, ret);
}
});
} }
rdbStore.delete(TBL_NAME, predicates, (err, ret) => {
if (callback !== undefined) {
callback(err, ret);
}
});
}
}; };
``` ```
...@@ -229,27 +216,23 @@ query?(uri: string, predicates: dataSharePredicates.DataSharePredicates, columns ...@@ -229,27 +216,23 @@ query?(uri: string, predicates: dataSharePredicates.DataSharePredicates, columns
import relationalStore 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 TBL_NAME = 'TBL00'; 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; let rdbStore: relationalStore.RdbStore;
export default class DataShareExtAbility extends DataShareExtensionAbility { export default class DataShareExtAbility extends DataShareExtensionAbility {
query(uri: string, predicates: dataSharePredicates.DataSharePredicates, columns: Array<string>, callback: Function) { query(uri: string, predicates: dataSharePredicates.DataSharePredicates, columns: Array<string>, callback: Function) {
if (predicates === null || predicates === undefined) { if (predicates === null || predicates === undefined) {
return; return;
}
rdbStore.query(TBL_NAME, predicates, columns, (err, resultSet) => {
if (resultSet !== undefined) {
console.info(`resultSet.rowCount: ${resultSet.rowCount}`);
}
if (callback !== undefined) {
callback(err, resultSet);
}
});
} }
rdbStore.query(TBL_NAME, predicates, columns, (err, resultSet) => {
if (resultSet !== undefined) {
console.info(`resultSet.rowCount: ${resultSet.rowCount}`);
}
if (callback !== undefined) {
callback(err, resultSet);
}
});
}
}; };
``` ```
...@@ -275,11 +258,7 @@ batchInsert?(uri: string, valueBuckets: Array&lt;ValuesBucket&gt;, callback: Asy ...@@ -275,11 +258,7 @@ batchInsert?(uri: string, valueBuckets: Array&lt;ValuesBucket&gt;, callback: Asy
import relationalStore 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 TBL_NAME = 'TBL00'; 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; let rdbStore: relationalStore.RdbStore;
export default class DataShareExtAbility extends DataShareExtensionAbility { export default class DataShareExtAbility extends DataShareExtensionAbility {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册