Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
OpenHarmony
Docs
提交
fa420e00
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看板
提交
fa420e00
编写于
8月 25, 2023
作者:
L
liusaimei
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Share相关使用说明文档示例代码ArxTs规范整改
Signed-off-by:
N
liusaimei
<
liusaimei@huawei.com
>
上级
32fada0b
变更
4
展开全部
显示空白变更内容
内联
并排
Showing
4 changed file
with
248 addition
and
105 deletion
+248
-105
zh-cn/application-dev/database/share-data-by-datashareextensionability.md
...n-dev/database/share-data-by-datashareextensionability.md
+26
-11
zh-cn/application-dev/reference/apis/js-apis-application-dataShareExtensionAbility.md
...nce/apis/js-apis-application-dataShareExtensionAbility.md
+47
-29
zh-cn/application-dev/reference/apis/js-apis-data-DataShareResultSet.md
...ion-dev/reference/apis/js-apis-data-DataShareResultSet.md
+5
-4
zh-cn/application-dev/reference/apis/js-apis-data-dataShare.md
.../application-dev/reference/apis/js-apis-data-dataShare.md
+170
-61
未找到文件。
zh-cn/application-dev/database/share-data-by-datashareextensionability.md
浏览文件 @
fa420e00
...
...
@@ -64,6 +64,8 @@
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
'
;
```
4.
数据提供方的业务实现由开发者自定义。例如可以通过数据库、读写文件或访问网络等各方式实现数据提供方的数据存储。
...
...
@@ -75,20 +77,20 @@
+
TBL_NAME
+
'
(id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT, age INTEGER, isStudent BOOLEAN, Binary BINARY)
'
;
let
rdbStore
;
let
result
;
let
rdbStore
:
relationalStore
.
RdbStore
;
let
result
:
string
;
export
default
class
DataShareExtAbility
extends
Extension
{
private
rdbStore_
;
private
rdbStore_
:
relationalStore
.
RdbStore
;
// 重写onCreate接口
onCreate
(
want
,
callback
)
{
onCreate
(
want
:
Want
,
callback
:
Function
)
{
result
=
this
.
context
.
cacheDir
+
'
/datashare.txt
'
;
// 业务实现使用RDB
rdb
.
getRdbStore
(
this
.
context
,
{
name
:
DB_NAME
,
securityLevel
:
rdb
.
SecurityLevel
.
S1
},
function
(
err
,
data
)
{
},
(
err
,
data
)
=>
{
rdbStore
=
data
;
rdbStore
.
executeSql
(
DDL_TBL_CREATE
,
[],
(
err
)
=>
{
console
.
info
(
`DataShareExtAbility onCreate, executeSql done err:
${
err
}
`
);
...
...
@@ -100,7 +102,7 @@
}
// 重写query接口
query
(
uri
,
predicates
,
columns
,
callback
)
{
query
(
uri
:
string
,
predicates
:
dataSharePredicates
.
DataSharePredicates
,
columns
:
Array
<
string
>
,
callback
:
Function
)
{
if
(
predicates
===
null
||
predicates
===
undefined
)
{
console
.
info
(
'
invalid predicates
'
);
}
...
...
@@ -188,6 +190,8 @@
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字符串。
...
...
@@ -200,11 +204,11 @@
3.
创建工具接口类对象。
```
js
let
dsHelper
;
let
abilityContext
;
let
dsHelper
:
dataShare
.
DataShareHelper
;
let
abilityContext
:
Context
;
export
default
class
EntryAbility
extends
UIAbility
{
onWindowStageCreate
(
windowStage
)
{
onWindowStageCreate
(
windowStage
:
window
.
WindowStage
)
{
abilityContext
=
this
.
context
;
dataShare
.
createDataShareHelper
(
abilityContext
,
dseUri
,
(
err
,
data
)
=>
{
dsHelper
=
data
;
...
...
@@ -217,8 +221,19 @@
```
js
// 构建一条数据
let
valuesBucket
=
{
'
name
'
:
'
ZhangSan
'
,
'
age
'
:
21
,
'
isStudent
'
:
false
,
'
Binary
'
:
new
Uint8Array
([
1
,
2
,
3
])
};
let
updateBucket
=
{
'
name
'
:
'
LiSi
'
,
'
age
'
:
18
,
'
isStudent
'
:
true
,
'
Binary
'
:
new
Uint8Array
([
1
,
2
,
3
])
};
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
=
[
'
*
'
];
// 插入一条数据
...
...
zh-cn/application-dev/reference/apis/js-apis-application-dataShareExtensionAbility.md
浏览文件 @
fa420e00
...
...
@@ -50,17 +50,17 @@ 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
;
let
rdbStore
:
relationalStore
.
RdbStore
;
export
default
class
DataShareExtAbility
extends
DataShareExtensionAbility
{
onCreate
(
want
,
callback
)
{
onCreate
(
want
:
Want
,
callback
:
Function
)
{
rdb
.
getRdbStore
(
this
.
context
,
{
name
:
DB_NAME
,
securityLevel
:
rdb
.
SecurityLevel
.
S1
},
function
(
err
,
data
)
{
},
(
err
,
data
)
=>
{
console
.
info
(
`getRdbStore done, data :
${
data
}
`
);
rdbStore
=
data
;
rdbStore
.
executeSql
(
DDL_TBL_CREATE
,
[],
function
(
err
)
{
rdbStore
.
executeSql
(
DDL_TBL_CREATE
,
[],
(
err
)
=>
{
console
.
error
(
`executeSql done, error message :
${
err
}
`
);
});
if
(
callback
)
{
...
...
@@ -91,21 +91,22 @@ insert?(uri: string, valueBucket: ValuesBucket, callback: AsyncCallback<numbe
```
ts
import
rdb
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
;
let
rdbStore
:
relationalStore
.
RdbStore
;
export
default
class
DataShareExtAbility
extends
DataShareExtensionAbility
{
insert
(
uri
,
valueBucket
,
callback
)
{
insert
(
uri
:
string
,
valueBucket
:
ValuesBucket
,
callback
:
Function
)
{
if
(
valueBucket
===
null
)
{
console
.
error
(
'
invalid valueBuckets
'
);
return
;
}
rdbStore
.
insert
(
TBL_NAME
,
valueBucket
,
function
(
err
,
ret
)
{
rdbStore
.
insert
(
TBL_NAME
,
valueBucket
,
(
err
,
ret
)
=>
{
console
.
info
(
`callback ret:
${
ret
}
`
);
if
(
callback
!==
undefined
)
{
callback
(
err
,
ret
);
...
...
@@ -136,20 +137,22 @@ update?(uri: string, predicates: dataSharePredicates.DataSharePredicates, valueB
```
ts
import
rdb
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
;
let
rdbStore
:
relationalStore
.
RdbStore
;
export
default
class
DataShareExtAbility
extends
DataShareExtensionAbility
{
update
(
uri
,
predicates
,
valueBucket
,
callback
)
{
update
(
uri
:
string
,
predicates
:
dataSharePredicates
.
DataSharePredicates
,
valueBucket
:
ValuesBucket
,
callback
:
Function
)
{
if
(
predicates
===
null
||
predicates
===
undefined
)
{
return
;
}
rdbStore
.
update
(
TBL_NAME
,
valueBucket
,
predicates
,
function
(
err
,
ret
)
{
rdbStore
.
update
(
TBL_NAME
,
valueBucket
,
predicates
,
(
err
,
ret
)
=>
{
if
(
callback
!==
undefined
)
{
callback
(
err
,
ret
);
}
...
...
@@ -178,20 +181,21 @@ delete?(uri: string, predicates: dataSharePredicates.DataSharePredicates, callba
```
ts
import
rdb
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
;
let
rdbStore
:
relationalStore
.
RdbStore
;
export
default
class
DataShareExtAbility
extends
DataShareExtensionAbility
{
delete
(
uri
,
predicates
,
callback
)
{
delete
(
uri
:
string
,
predicates
:
dataSharePredicates
.
DataSharePredicates
,
callback
:
Function
)
{
if
(
predicates
===
null
||
predicates
===
undefined
)
{
return
;
}
rdbStore
.
delete
(
TBL_NAME
,
predicates
,
function
(
err
,
ret
)
{
rdbStore
.
delete
(
TBL_NAME
,
predicates
,
(
err
,
ret
)
=>
{
if
(
callback
!==
undefined
)
{
callback
(
err
,
ret
);
}
...
...
@@ -221,20 +225,21 @@ query?(uri: string, predicates: dataSharePredicates.DataSharePredicates, columns
```
ts
import
rdb
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
;
let
rdbStore
:
relationalStore
.
RdbStore
;
export
default
class
DataShareExtAbility
extends
DataShareExtensionAbility
{
query
(
uri
,
predicates
,
columns
,
callback
)
{
query
(
uri
:
string
,
predicates
:
dataSharePredicates
.
DataSharePredicates
,
columns
:
Array
<
string
>
,
callback
:
Function
)
{
if
(
predicates
===
null
||
predicates
===
undefined
)
{
return
;
}
rdbStore
.
query
(
TBL_NAME
,
predicates
,
columns
,
function
(
err
,
resultSet
)
{
rdbStore
.
query
(
TBL_NAME
,
predicates
,
columns
,
(
err
,
resultSet
)
=>
{
if
(
resultSet
!==
undefined
)
{
console
.
info
(
`resultSet.rowCount:
${
resultSet
.
rowCount
}
`
);
}
...
...
@@ -266,21 +271,22 @@ batchInsert?(uri: string, valueBuckets: Array<ValuesBucket>, callback: Asy
```
ts
import
rdb
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
;
let
rdbStore
:
relationalStore
.
RdbStore
;
export
default
class
DataShareExtAbility
extends
DataShareExtensionAbility
{
batchInsert
(
uri
,
valueBuckets
,
callback
)
{
batchInsert
(
uri
:
string
,
valueBuckets
:
Array
<
ValuesBucket
>
,
callback
:
Function
)
{
if
(
valueBuckets
===
null
||
valueBuckets
.
length
===
undefined
)
{
console
.
error
(
'
invalid valueBuckets
'
);
return
;
}
rdbStore
.
batchInsert
(
TBL_NAME
,
valueBuckets
,
function
(
err
,
ret
)
{
rdbStore
.
batchInsert
(
TBL_NAME
,
valueBuckets
,
(
err
,
ret
)
=>
{
if
(
callback
!==
undefined
)
{
callback
(
err
,
ret
);
}
...
...
@@ -308,9 +314,15 @@ normalizeUri?(uri: string, callback: AsyncCallback<string>): void
```
ts
export
default
class
DataShareExtAbility
extends
DataShareExtensionAbility
{
normalizeUri
(
uri
,
callback
)
{
let
err
=
{
'
code
'
:
0
};
let
ret
=
`normalize:
${
uri
}
`
;
normalizeUri
(
uri
:
string
,
callback
:
Function
)
{
let
key
=
'
code
'
;
let
value
=
0
;
let
err
:
BusinessError
=
{
code
:
value
,
name
:
key
,
message
:
key
};
let
ret
:
string
=
`normalize:
${
uri
}
`
;
callback
(
err
,
ret
);
}
};
...
...
@@ -335,8 +347,14 @@ denormalizeUri?(uri: string, callback: AsyncCallback<string>): void
```
ts
export
default
class
DataShareExtAbility
extends
DataShareExtensionAbility
{
denormalizeUri
(
uri
,
callback
)
{
let
err
=
{
'
code
'
:
0
};
denormalizeUri
(
uri
:
string
,
callback
:
Function
)
{
let
key
=
'
code
'
;
let
value
=
0
;
let
err
:
BusinessError
=
{
code
:
value
,
name
:
key
,
message
:
key
};
let
ret
=
`denormalize
${
uri
}
`
;
callback
(
err
,
ret
);
}
...
...
zh-cn/application-dev/reference/apis/js-apis-data-DataShareResultSet.md
浏览文件 @
fa420e00
...
...
@@ -22,8 +22,9 @@ import DataShareResultSet from '@ohos.data.DataShareResultSet';
```
ts
import
dataShare
from
'
@ohos.data.dataShare
'
;
import
dataSharePredicates
from
'
@ohos.data.dataSharePredicates
'
import
{
BusinessError
}
from
'
@ohos.base
'
let
dataShareHelper
;
let
dataShareHelper
:
dataShare
.
DataShareHelper
;
let
uri
=
(
"
datashare:///com.samples.datasharetest.DataShare
"
);
await
dataShare
.
createDataShareHelper
(
this
.
context
,
uri
,
(
err
,
data
)
=>
{
if
(
err
!=
undefined
)
{
...
...
@@ -36,12 +37,12 @@ await dataShare.createDataShareHelper(this.context, uri, (err, data) => {
let
columns
=
[
"
*
"
];
let
da
=
new
dataSharePredicates
.
DataSharePredicates
();
let
resultSet
;
let
resultSet
:
DataShareResultSet
;
da
.
equalTo
(
"
name
"
,
"
ZhangSan
"
);
dataShareHelper
.
query
(
uri
,
da
,
columns
).
then
((
data
)
=>
{
dataShareHelper
.
query
(
uri
,
da
,
columns
).
then
((
data
:
DataShareResultSet
)
=>
{
console
.
info
(
"
query end, data :
"
+
data
);
resultSet
=
data
;
}).
catch
((
err
)
=>
{
}).
catch
((
err
:
BusinessError
)
=>
{
console
.
error
(
"
query fail, error message :
"
+
err
);
});
```
...
...
zh-cn/application-dev/reference/apis/js-apis-data-dataShare.md
浏览文件 @
fa420e00
此差异已折叠。
点击以展开。
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录