Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
OpenHarmony
Docs
提交
c7a8c61c
D
Docs
项目概览
OpenHarmony
/
Docs
1 年多 前同步成功
通知
159
Star
292
Fork
28
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
D
Docs
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
提交
c7a8c61c
编写于
9月 06, 2023
作者:
L
liusaimei
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
dataShare相关使用说明文档示例代码ArkTs规范整改
Signed-off-by:
N
liusaimei
<
liusaimei@huawei.com
>
上级
5d5029ac
变更
2
显示空白变更内容
内联
并排
Showing
2 changed file
with
178 addition
and
199 deletion
+178
-199
zh-cn/application-dev/database/share-data-by-datashareextensionability.md
...n-dev/database/share-data-by-datashareextensionability.md
+120
-120
zh-cn/application-dev/reference/apis/js-apis-application-dataShareExtensionAbility.md
...nce/apis/js-apis-application-dataShareExtensionAbility.md
+58
-79
未找到文件。
zh-cn/application-dev/database/share-data-by-datashareextensionability.md
浏览文件 @
c7a8c61c
...
...
@@ -60,28 +60,28 @@
3.
在DataShareExtAbility.ts文件中,导入
`@ohos.application.DataShareExtensionAbility`
模块,开发者可根据应用需求选择性重写其业务实现。例如数据提供方只提供插入、删除和查询服务,则可只重写这些接口,并导入对应的基础依赖模块。
```
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
'
```
```
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.
数据提供方的业务实现由开发者自定义。例如可以通过数据库、读写文件或访问网络等各方式实现数据提供方的数据存储。
```
ts
const
DB_NAME
=
'
DB00.db
'
;
const
TBL_NAME
=
'
TBL00
'
;
const
DDL_TBL_CREATE
=
"
CREATE TABLE IF NOT EXISTS
"
```
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
;
let
rdbStore
:
relationalStore
.
RdbStore
;
let
result
:
string
;
export
default
class
DataShareExtAbility
extends
Extension
{
export
default
class
DataShareExtAbility
extends
Extension
{
// 重写onCreate接口
onCreate
(
want
:
Want
,
callback
:
Function
)
{
result
=
this
.
context
.
cacheDir
+
'
/datashare.txt
'
;
...
...
@@ -121,8 +121,8 @@ export default class DataShareExtAbility extends Extension {
}
}
// 可根据应用需求,选择性重写各个接口
};
```
};
```
5.
在module.json5中定义DataShareExtensionAbility。
...
...
@@ -187,57 +187,57 @@ export default class DataShareExtAbility extends Extension {
1.
导入基础依赖包。
```
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
'
;
```
```
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字符串。
```
ts
// 作为参数传递的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.
创建工具接口类对象。
```
ts
let
dsHelper
:
dataShare
.
DataShareHelper
|
undefined
=
undefined
;
let
abilityContext
:
Context
;
```
ts
let
dsHelper
:
dataShare
.
DataShareHelper
|
undefined
=
undefined
;
let
abilityContext
:
Context
;
export
default
class
EntryAbility
extends
UIAbility
{
export
default
class
EntryAbility
extends
UIAbility
{
onWindowStageCreate
(
windowStage
:
window
.
WindowStage
)
{
abilityContext
=
this
.
context
;
dataShare
.
createDataShareHelper
(
abilityContext
,
dseUri
,
(
err
,
data
)
=>
{
dsHelper
=
data
;
});
}
}
```
}
```
4.
获取到接口类对象后,便可利用其提供的接口访问提供方提供的服务,如进行数据的增删改查等。
```
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
)
{
```
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
}
`
);
...
...
@@ -254,8 +254,8 @@ if (dsHelper != undefined) {
(
dsHelper
as
dataShare
.
DataShareHelper
).
delete
(
dseUri
,
predicates
,
(
err
,
data
)
=>
{
console
.
info
(
`dsHelper delete result:
${
data
}
`
);
});
}
```
}
```
## 相关实例
...
...
zh-cn/application-dev/reference/apis/js-apis-application-dataShareExtensionAbility.md
浏览文件 @
c7a8c61c
...
...
@@ -43,7 +43,6 @@ DataShare客户端连接DataShareExtensionAbility服务端时,服务端回调
**示例:**
```
ts
import
rdb
from
'
@ohos.data.relationalStore
'
import
relationalStore
from
'
@ohos.data.relationalStore
'
import
Want
from
'
@ohos.app.ability.Want
'
...
...
@@ -56,9 +55,9 @@ let rdbStore: relationalStore.RdbStore;
export
default
class
DataShareExtAbility
extends
DataShareExtensionAbility
{
onCreate
(
want
:
Want
,
callback
:
Function
)
{
rdb
.
getRdbStore
(
this
.
context
,
{
relationalStore
.
getRdbStore
(
globalThis
.
getContext
()
,
{
name
:
DB_NAME
,
securityLevel
:
rdb
.
SecurityLevel
.
S1
securityLevel
:
relationalStore
.
SecurityLevel
.
S1
},
(
err
,
data
)
=>
{
console
.
info
(
`getRdbStore done, data :
${
data
}
`
);
rdbStore
=
data
;
...
...
@@ -95,11 +94,7 @@ insert?(uri: string, valueBucket: ValuesBucket, callback: AsyncCallback<numbe
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
{
...
...
@@ -142,11 +137,7 @@ 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
{
...
...
@@ -185,11 +176,7 @@ delete?(uri: string, predicates: dataSharePredicates.DataSharePredicates, callba
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
{
...
...
@@ -229,11 +216,7 @@ query?(uri: string, predicates: dataSharePredicates.DataSharePredicates, columns
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
{
...
...
@@ -275,11 +258,7 @@ batchInsert?(uri: string, valueBuckets: Array<ValuesBucket>, callback: Asy
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
{
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录