From 9805e77220a6b7debfb8ada7f8cd235df413a668 Mon Sep 17 00:00:00 2001 From: marui Date: Wed, 23 Aug 2023 15:40:57 +0800 Subject: [PATCH] 8.23 modify deviceID Signed-off-by: marui --- .../database/data-sync-of-rdb-store.md | 49 +++--- .../apis/js-apis-data-relationalStore.md | 154 ++++++++---------- 2 files changed, 93 insertions(+), 110 deletions(-) diff --git a/zh-cn/application-dev/database/data-sync-of-rdb-store.md b/zh-cn/application-dev/database/data-sync-of-rdb-store.md index 9f8030ff40..6a9a677141 100644 --- a/zh-cn/application-dev/database/data-sync-of-rdb-store.md +++ b/zh-cn/application-dev/database/data-sync-of-rdb-store.md @@ -44,8 +44,6 @@ - 单个数据库最多支持注册8个订阅数据变化的回调。 -- 不支持非系统应用调用需要指定设备的分布式能力接口。 - ## 接口说明 @@ -142,34 +140,35 @@ > **说明:** > - > deviceIds通过调用[devManager.getTrustedDeviceListSync](../reference/apis/js-apis-device-manager.md#gettrusteddevicelistsync)方法得到,deviceManager模块的接口均为系统接口,仅系统应用可用。 + > deviceIds通过调用[deviceManager.getAvailableDeviceListSync](../reference/apis/js-apis-distributedDeviceManager.md#getavailabledevicelistsync)方法得到。 ```js // 获取deviceIds - import deviceManager from '@ohos.distributedHardware.deviceManager'; - - deviceManager.createDeviceManager("com.example.appdatamgrverify", (err, manager) => { - if (err) { - console.info(`Failed to create device manager. Code:${err.code},message:${err.message}`); - return; - } - let devices = manager.getTrustedDeviceListSync(); - let deviceId = devices[0].deviceId; - - // 构造用于查询分布式表的谓词对象 - let predicates = new relationalStore.RdbPredicates('EMPLOYEE'); - // 调用跨设备查询接口,并返回查询结果 - store.remoteQuery(deviceId, 'EMPLOYEE', predicates, ['ID', 'NAME', 'AGE', 'SALARY', 'CODES'], - function (err, resultSet) { - if (err) { - console.error(`Failed to remoteQuery data. Code:${err.code},message:${err.message}`); - return; - } - console.info(`ResultSet column names: ${resultSet.columnNames}, column count: ${resultSet.columnCount}`); + import deviceManager from '@ohos.distributedDeviceManager'; + let dmInstance = null; + let deviceId = null; + + try { + dmInstance = deviceManager.createDeviceManager("com.example.appdatamgrverify"); + let devices = dmInstance.getAvailableDeviceListSync(); + deviceId = devices[0].networkId; + + // 构造用于查询分布式表的谓词对象 + let predicates = new relationalStore.RdbPredicates('EMPLOYEE'); + // 调用跨设备查询接口,并返回查询结果 + store.remoteQuery(deviceId, 'EMPLOYEE', predicates, ['ID', 'NAME', 'AGE', 'SALARY', 'CODES'], + function (err, resultSet) { + if (err) { + console.error(`Failed to remoteQuery data. Code:${err.code},message:${err.message}`); + return; } - ) - }) + console.info(`ResultSet column names: ${resultSet.columnNames}, column count: ${resultSet.columnCount}`); + } + ) + } catch (err) { + console.error("createDeviceManager errCode:" + err.code + ",errMessage:" + err.message); + } ``` ## 相关实例 diff --git a/zh-cn/application-dev/reference/apis/js-apis-data-relationalStore.md b/zh-cn/application-dev/reference/apis/js-apis-data-relationalStore.md index d17dffc75e..6e3d6cde67 100644 --- a/zh-cn/application-dev/reference/apis/js-apis-data-relationalStore.md +++ b/zh-cn/application-dev/reference/apis/js-apis-data-relationalStore.md @@ -798,7 +798,8 @@ inDevices(devices: Array<string>): RdbPredicates > **说明:** > -> 其中devices通过调用[deviceManager.getAvailableDeviceListSync](js-apis-distributedDeviceManager.md#getAvailableDeviceListSync)方法得到。数据库同步时调用Sync接口,需要在入参谓词中调用inDevices接口选择设备。如果不调用inDevices接口即默认连接组网内所有的设备。 +> 其中devices通过调用[deviceManager.getAvailableDeviceListSync](js-apis-distributedDeviceManager.md#getavailabledevicelistsync)方法得到。 +数据库同步时调用Sync接口,需要在入参谓词中调用inDevices接口选择设备。如果不调用inDevices接口即默认连接组网内所有的设备。 **系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core @@ -817,22 +818,20 @@ inDevices(devices: Array<string>): RdbPredicates **示例:** ```js -import deviceManager from '@ohos.distributedHardware.deviceManager'; +import deviceManager from '@ohos.distributedDeviceManager'; let dmInstance = null; let deviceIds = []; -deviceManager.createDeviceManager("com.example.appdatamgrverify", (err, manager) => { - if (err) { - console.log("create device manager failed, err=" + err); - return; - } - dmInstance = manager; - let devices = dmInstance.getAvailableDeviceListSync(); - for (var i = 0; i < devices.length; i++) { - deviceIds[i] = devices[i].networkId; - } -}) - +try { + dmInstance = deviceManager.createDeviceManager("com.example.appdatamgrverify"); + let devices = dmInstance.getAvailableDeviceListSync(); + for (var i = 0; i < devices.length; i++) { + deviceIds[i] = devices[i].networkId; + } +} catch (err) { + console.error("createDeviceManager errCode:" + err.code + ",errMessage:" + err.message); +} + let predicates = new relationalStore.RdbPredicates("EMPLOYEE"); predicates.inDevices(deviceIds); ``` @@ -844,9 +843,6 @@ inAllDevices(): RdbPredicates 同步分布式数据库时连接到组网内所有的远程设备。 -> **说明:** -> ->如果不调用inAllDevices接口即默认连接组网内所有的远程设备。 **系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core @@ -2737,7 +2733,7 @@ remoteQuery(device: string, table: string, predicates: RdbPredicates, columns: A > **说明:** > -> 其中device通过调用[deviceManager.getAvailableDeviceListSync](js-apis-distributedDeviceManager.md#getAvailableDeviceListSync)方法得到。 +> 其中device通过调用[deviceManager.getAvailableDeviceListSync](js-apis-distributedDeviceManager.md#getavailabledevicelistsync)方法得到。 **系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core @@ -2762,19 +2758,17 @@ remoteQuery(device: string, table: string, predicates: RdbPredicates, columns: A **示例:** ```js -import deviceManager from '@ohos.distributedHardware.deviceManager'; +import deviceManager from '@ohos.distributedDeviceManager'; let dmInstance = null; let deviceId = null; -deviceManager.createDeviceManager("com.example.appdatamgrverify", (err, manager) => { - if (err) { - console.log("create device manager failed, err=" + err); - return; - } - dmInstance = manager; - let devices = dmInstance.getAvailableDeviceListSync(); - deviceId = devices[0].networkId; -}) +try { + dmInstance = deviceManager.createDeviceManager("com.example.appdatamgrverify"); + let devices = dmInstance.getAvailableDeviceListSync(); + deviceId = devices[0].networkId; +} catch (err) { + console.error("createDeviceManager errCode:" + err.code + ",errMessage:" + err.message); +} let predicates = new relationalStore.RdbPredicates('EMPLOYEE'); predicates.greaterThan("id", 0); @@ -2807,7 +2801,7 @@ remoteQuery(device: string, table: string, predicates: RdbPredicates, columns: A > **说明:** > -> 其中device通过调用[deviceManager.getAvailableDeviceListSync](js-apis-distributedDeviceManager.md#getAvailableDeviceListSync)方法得到。 +> 其中device通过调用[deviceManager.getAvailableDeviceListSync](js-apis-distributedDeviceManager.md#getavailabledevicelistsync)方法得到。 **系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core @@ -2837,19 +2831,17 @@ remoteQuery(device: string, table: string, predicates: RdbPredicates, columns: A **示例:** ```js -import deviceManager from '@ohos.distributedHardware.deviceManager'; +import deviceManager from '@ohos.distributedDeviceManager'; let dmInstance = null; let deviceId = null; -deviceManager.createDeviceManager("com.example.appdatamgrverify", (err, manager) => { - if (err) { - console.log("create device manager failed, err=" + err); - return; - } - dmInstance = manager; - let devices = dmInstance.getAvailableDeviceListSync(); - deviceId = devices[0].networkId; -}) +try { + dmInstance = deviceManager.createDeviceManager("com.example.appdatamgrverify"); + let devices = dmInstance.getAvailableDeviceListSync(); + deviceId = devices[0].networkId; +} catch (err) { + console.error("createDeviceManager errCode:" + err.code + ",errMessage:" + err.message); +} let predicates = new relationalStore.RdbPredicates('EMPLOYEE'); predicates.greaterThan("id", 0); @@ -3688,7 +3680,7 @@ obtainDistributedTableName(device: string, table: string, callback: AsyncCallbac > **说明:** > -> 其中device通过调用[deviceManager.getAvailableDeviceListSync](js-apis-distributedDeviceManager.md#getAvailableDeviceListSync)方法得到。 +> 其中device通过调用[deviceManager.getAvailableDeviceListSync](js-apis-distributedDeviceManager.md#getavailabledevicelistsync)方法得到。 **需要权限:** ohos.permission.DISTRIBUTED_DATASYNC @@ -3713,19 +3705,17 @@ obtainDistributedTableName(device: string, table: string, callback: AsyncCallbac **示例:** ```js -import deviceManager from '@ohos.distributedHardware.deviceManager'; +import deviceManager from '@ohos.distributedDeviceManager'; let dmInstance = null; let deviceId = null; -deviceManager.createDeviceManager("com.example.appdatamgrverify", (err, manager) => { - if (err) { - console.log("create device manager failed, err=" + err); - return; - } - dmInstance = manager; - let devices = dmInstance.getAvailableDeviceListSync(); - deviceId = devices[0].networkId; -}) +try { + dmInstance = deviceManager.createDeviceManager("com.example.appdatamgrverify"); + let devices = dmInstance.getAvailableDeviceListSync(); + deviceId = devices[0].networkId; +} catch (err) { + console.error("createDeviceManager errCode:" + err.code + ",errMessage:" + err.message); +} store.obtainDistributedTableName(deviceId, "EMPLOYEE", function (err, tableName) { if (err) { @@ -3744,7 +3734,7 @@ store.obtainDistributedTableName(deviceId, "EMPLOYEE", function (err, tableName) > **说明:** > -> 其中device通过调用[deviceManager.getAvailableDeviceListSync](js-apis-distributedDeviceManager.md#getAvailableDeviceListSync)方法得到。 +> 其中device通过调用[deviceManager.getAvailableDeviceListSync](js-apis-distributedDeviceManager.md#getavailabledevicelistsync)方法得到。 **需要权限:** ohos.permission.DISTRIBUTED_DATASYNC @@ -3774,19 +3764,17 @@ store.obtainDistributedTableName(deviceId, "EMPLOYEE", function (err, tableName) **示例:** ```js -import deviceManager from '@ohos.distributedHardware.deviceManager'; +import deviceManager from '@ohos.distributedDeviceManager'; let dmInstance = null; let deviceId = null; -deviceManager.createDeviceManager("com.example.appdatamgrverify", (err, manager) => { - if (err) { - console.log("create device manager failed, err=" + err); - return; - } - dmInstance = manager; - let devices = dmInstance.getAvailableDeviceListSync(); - deviceId = devices[0].networkId; -}) +try { + dmInstance = deviceManager.createDeviceManager("com.example.appdatamgrverify"); + let devices = dmInstance.getAvailableDeviceListSync(); + deviceId = devices[0].networkId; +} catch (err) { + console.error("createDeviceManager errCode:" + err.code + ",errMessage:" + err.message); +} let promise = store.obtainDistributedTableName(deviceId, "EMPLOYEE"); promise.then((tableName) => { @@ -3825,21 +3813,19 @@ sync(mode: SyncMode, predicates: RdbPredicates, callback: AsyncCallback<Array **示例:** ```js -import deviceManager from '@ohos.distributedHardware.deviceManager'; +import deviceManager from '@ohos.distributedDeviceManager'; let dmInstance = null; let deviceIds = []; -deviceManager.createDeviceManager("com.example.appdatamgrverify", (err, manager) => { - if (err) { - console.log("create device manager failed, err=" + err); - return; - } - dmInstance = manager; - let devices = dmInstance.getAvailableDeviceListSync(); - for (var i = 0; i < devices.length; i++) { - deviceIds[i] = devices[i].networkId; - } -}) +try { + dmInstance = deviceManager.createDeviceManager("com.example.appdatamgrverify"); + let devices = dmInstance.getAvailableDeviceListSync(); + for (var i = 0; i < devices.length; i++) { + deviceIds[i] = devices[i].networkId; + } +} catch (err) { + console.error("createDeviceManager errCode:" + err.code + ",errMessage:" + err.message); +} let predicates = new relationalStore.RdbPredicates('EMPLOYEE'); predicates.inDevices(deviceIds); @@ -3889,21 +3875,19 @@ store.sync(relationalStore.SyncMode.SYNC_MODE_PUSH, predicates, function (err, r **示例:** ```js -import deviceManager from '@ohos.distributedHardware.deviceManager'; +import deviceManager from '@ohos.distributedDeviceManager'; let dmInstance = null; let deviceIds = []; -deviceManager.createDeviceManager("com.example.appdatamgrverify", (err, manager) => { - if (err) { - console.log("create device manager failed, err=" + err); - return; - } - dmInstance = manager; - let devices = dmInstance.getAvailableDeviceListSync(); - for (var i = 0; i < devices.length; i++) { - deviceIds[i] = devices[i].networkId; - } -}) +try { + dmInstance = deviceManager.createDeviceManager("com.example.appdatamgrverify"); + let devices = dmInstance.getAvailableDeviceListSync(); + for (var i = 0; i < devices.length; i++) { + deviceIds[i] = devices[i].networkId; + } +} catch (err) { + console.error("createDeviceManager errCode:" + err.code + ",errMessage:" + err.message); +} let predicates = new relationalStore.RdbPredicates('EMPLOYEE'); predicates.inDevices(deviceIds); -- GitLab