關(guān)系型數(shù)據(jù)庫(kù)

2024-01-23 16:09 更新

關(guān)系型數(shù)據(jù)庫(kù)(Relational Database,RDB)是一種基于關(guān)系模型來(lái)管理數(shù)據(jù)的數(shù)據(jù)庫(kù)。關(guān)系型數(shù)據(jù)庫(kù)基于SQLite組件提供了一套完整的對(duì)本地?cái)?shù)據(jù)庫(kù)進(jìn)行管理的機(jī)制,對(duì)外提供了一系列的增、刪、改、查等接口,也可以直接運(yùn)行用戶輸入的SQL語(yǔ)句來(lái)滿足復(fù)雜的場(chǎng)景需要。不支持Worker線程。

該模塊提供以下關(guān)系型數(shù)據(jù)庫(kù)相關(guān)的常用功能:

  • RdbPredicates: 數(shù)據(jù)庫(kù)中用來(lái)代表數(shù)據(jù)實(shí)體的性質(zhì)、特征或者數(shù)據(jù)實(shí)體之間關(guān)系的詞項(xiàng),主要用來(lái)定義數(shù)據(jù)庫(kù)的操作條件。
  • RdbStore:提供管理關(guān)系數(shù)據(jù)庫(kù)(RDB)方法的接口。
  • ResultSet:提供用戶調(diào)用關(guān)系型數(shù)據(jù)庫(kù)查詢接口之后返回的結(jié)果集合。
說(shuō)明
  • 本模塊首批接口從API version 9開始支持。后續(xù)版本的新增接口,采用上角標(biāo)單獨(dú)標(biāo)記接口的起始版本。
  • 關(guān)系型數(shù)據(jù)庫(kù)當(dāng)前僅支持本地功能,分布式功能暫不支持。

導(dǎo)入模塊

  1. import relationalStore from '@ohos.data.relationalStore'

relationalStore.getRdbStore

getRdbStore(context: Context, config: StoreConfig, callback: AsyncCallback<RdbStore>): void

獲得一個(gè)相關(guān)的RdbStore,操作關(guān)系型數(shù)據(jù)庫(kù),用戶可以根據(jù)自己的需求配置RdbStore的參數(shù),然后通過(guò)RdbStore調(diào)用相關(guān)接口可以執(zhí)行相關(guān)的數(shù)據(jù)操作,使用callback異步回調(diào)。

系統(tǒng)能力: SystemCapability.DistributedDataManager.RelationalStore.Core

參數(shù):

參數(shù)名

類型

必填

說(shuō)明

context

Context

應(yīng)用的上下文。

FA模型的應(yīng)用Context定義見(jiàn)Context。

Stage模型的應(yīng)用Context定義見(jiàn)Context。

config

StoreConfig

與此RDB存儲(chǔ)相關(guān)的數(shù)據(jù)庫(kù)配置。

callback

AsyncCallback<RdbStore>

指定callback回調(diào)函數(shù),返回RdbStore對(duì)象。

錯(cuò)誤碼:

以下錯(cuò)誤碼的詳細(xì)介紹請(qǐng)參見(jiàn)關(guān)系型數(shù)據(jù)庫(kù)錯(cuò)誤碼。

錯(cuò)誤碼ID

錯(cuò)誤信息

14800010

If failed delete database by invalid database name.

14800011

If failed open database by database corrupted.

示例:

FA模型示例:

  1. import featureAbility from '@ohos.ability.featureAbility'
  2. var store;
  3. // 獲取context
  4. let context = featureAbility.getContext();
  5. const STORE_CONFIG = {
  6. name: "RdbTest.db",
  7. securityLevel: relationalStore.SecurityLevel.S1
  8. };
  9. relationalStore.getRdbStore(context, STORE_CONFIG, function (err, rdbStore) {
  10. store = rdbStore;
  11. if (err) {
  12. console.error(`Get RdbStore failed, code is ${err.code},message is ${err.message}`);
  13. return;
  14. }
  15. console.info(`Get RdbStore successfully.`);
  16. })

Stage模型示例:

  1. import UIAbility from '@ohos.app.ability.UIAbility'
  2. class EntryAbility extends UIAbility {
  3. onWindowStageCreate(windowStage) {
  4. var store;
  5. const STORE_CONFIG = {
  6. name: "RdbTest.db",
  7. securityLevel: relationalStore.SecurityLevel.S1
  8. };
  9. relationalStore.getRdbStore(this.context, STORE_CONFIG, function (err, rdbStore) {
  10. store = rdbStore;
  11. if (err) {
  12. console.error(`Get RdbStore failed, code is ${err.code},message is ${err.message}`);
  13. return;
  14. }
  15. console.info(`Get RdbStore successfully.`);
  16. })
  17. }
  18. }

relationalStore.getRdbStore

getRdbStore(context: Context, config: StoreConfig): Promise<RdbStore>

獲得一個(gè)相關(guān)的RdbStore,操作關(guān)系型數(shù)據(jù)庫(kù),用戶可以根據(jù)自己的需求配置RdbStore的參數(shù),然后通過(guò)RdbStore調(diào)用相關(guān)接口可以執(zhí)行相關(guān)的數(shù)據(jù)操作,使用Promise異步回調(diào)。

系統(tǒng)能力: SystemCapability.DistributedDataManager.RelationalStore.Core

參數(shù):

參數(shù)名

類型

必填

說(shuō)明

context

Context

應(yīng)用的上下文。

FA模型的應(yīng)用Context定義見(jiàn)Context

Stage模型的應(yīng)用Context定義見(jiàn)Context。

config

StoreConfig

與此RDB存儲(chǔ)相關(guān)的數(shù)據(jù)庫(kù)配置。

返回值

類型

說(shuō)明

Promise<RdbStore>

Promise對(duì)象。返回RdbStore對(duì)象。

錯(cuò)誤碼:

以下錯(cuò)誤碼的詳細(xì)介紹請(qǐng)參見(jiàn)關(guān)系型數(shù)據(jù)庫(kù)錯(cuò)誤碼。

錯(cuò)誤碼ID

錯(cuò)誤信息

14800010

If failed delete database by invalid database name.

14800011

If failed open database by database corrupted.

示例:

FA模型示例:

  1. import featureAbility from '@ohos.ability.featureAbility'
  2. var store;
  3. // 獲取context
  4. let context = featureAbility.getContext();
  5. const STORE_CONFIG = {
  6. name: "RdbTest.db",
  7. securityLevel: relationalStore.SecurityLevel.S1
  8. };
  9. let promise = relationalStore.getRdbStore(context, STORE_CONFIG);
  10. promise.then(async (rdbStore) => {
  11. store = rdbStore;
  12. console.info(`Get RdbStore successfully.`);
  13. }).catch((err) => {
  14. console.error(`Get RdbStore failed, code is ${err.code},message is ${err.message}`);
  15. })

Stage模型示例:

  1. import UIAbility from '@ohos.app.ability.UIAbility'
  2. class EntryAbility extends UIAbility {
  3. onWindowStageCreate(windowStage) {
  4. var store;
  5. const STORE_CONFIG = {
  6. name: "RdbTest.db",
  7. securityLevel: relationalStore.SecurityLevel.S1
  8. };
  9. let promise = relationalStore.getRdbStore(this.context, STORE_CONFIG);
  10. promise.then(async (rdbStore) => {
  11. store = rdbStore;
  12. console.info(`Get RdbStore successfully.`)
  13. }).catch((err) => {
  14. console.error(`Get RdbStore failed, code is ${err.code},message is ${err.message}`);
  15. })
  16. }
  17. }

relationalStore.deleteRdbStore

deleteRdbStore(context: Context, name: string, callback: AsyncCallback<void>): void

刪除數(shù)據(jù)庫(kù),使用callback異步回調(diào)。

系統(tǒng)能力: SystemCapability.DistributedDataManager.RelationalStore.Core

參數(shù):

參數(shù)名

類型

必填

說(shuō)明

context

Context

應(yīng)用的上下文。

FA模型的應(yīng)用Context定義見(jiàn)Context。

Stage模型的應(yīng)用Context定義見(jiàn)Context。

name

string

數(shù)據(jù)庫(kù)名稱。

callback

AsyncCallback<void>

指定callback回調(diào)函數(shù)。

錯(cuò)誤碼:

以下錯(cuò)誤碼的詳細(xì)介紹請(qǐng)參見(jiàn)關(guān)系型數(shù)據(jù)庫(kù)錯(cuò)誤碼。

錯(cuò)誤碼ID

錯(cuò)誤信息

14800010

If failed delete database by invalid database name.

示例:

FA模型示例:

  1. import featureAbility from '@ohos.ability.featureAbility'
  2. // 獲取context
  3. let context = featureAbility.getContext()
  4. relationalStore.deleteRdbStore(context, "RdbTest.db", function (err) {
  5. if (err) {
  6. console.error(`Delete RdbStore failed, code is ${err.code},message is ${err.message}`);
  7. return;
  8. }
  9. console.info(`Delete RdbStore successfully.`);
  10. })

Stage模型示例:

  1. import UIAbility from '@ohos.app.ability.UIAbility'
  2. class EntryAbility extends UIAbility {
  3. onWindowStageCreate(windowStage){
  4. relationalStore.deleteRdbStore(this.context, "RdbTest.db", function (err) {
  5. if (err) {
  6. console.error(`Delete RdbStore failed, code is ${err.code},message is ${err.message}`);
  7. return;
  8. }
  9. console.info(`Delete RdbStore successfully.`);
  10. })
  11. }
  12. }

relationalStore.deleteRdbStore

deleteRdbStore(context: Context, name: string): Promise<void>

使用指定的數(shù)據(jù)庫(kù)文件配置刪除數(shù)據(jù)庫(kù),使用Promise異步回調(diào)。

系統(tǒng)能力: SystemCapability.DistributedDataManager.RelationalStore.Core

參數(shù)

參數(shù)名

類型

必填

說(shuō)明

context

Context

應(yīng)用的上下文。

FA模型的應(yīng)用Context定義見(jiàn)Context。

Stage模型的應(yīng)用Context定義見(jiàn)Context

name

string

數(shù)據(jù)庫(kù)名稱。

返回值

類型

說(shuō)明

Promise<void>

無(wú)返回結(jié)果的Promise對(duì)象。

錯(cuò)誤碼:

以下錯(cuò)誤碼的詳細(xì)介紹請(qǐng)參見(jiàn)關(guān)系型數(shù)據(jù)庫(kù)錯(cuò)誤碼。

錯(cuò)誤碼ID

錯(cuò)誤信息

14800010

If failed delete database by invalid database name.

示例:

FA模型示例:

  1. import featureAbility from '@ohos.ability.featureAbility'
  2. // 獲取context
  3. let context = featureAbility.getContext();
  4. let promise = relationalStore.deleteRdbStore(context, "RdbTest.db");
  5. promise.then(()=>{
  6. console.info(`Delete RdbStore successfully.`);
  7. }).catch((err) => {
  8. console.error(`Delete RdbStore failed, code is ${err.code},message is ${err.message}`);
  9. })

Stage模型示例:

  1. import UIAbility from '@ohos.app.ability.UIAbility'
  2. class EntryAbility extends UIAbility {
  3. onWindowStageCreate(windowStage){
  4. let promise = relationalStore.deleteRdbStore(this.context, "RdbTest.db");
  5. promise.then(()=>{
  6. console.info(`Delete RdbStore successfully.`);
  7. }).catch((err) => {
  8. console.error(`Delete RdbStore failed, code is ${err.code},message is ${err.message}`);
  9. })
  10. }
  11. }

StoreConfig

管理關(guān)系數(shù)據(jù)庫(kù)配置。

系統(tǒng)能力: SystemCapability.DistributedDataManager.RelationalStore.Core

名稱

類型

必填

說(shuō)明

name

string

數(shù)據(jù)庫(kù)文件名。

securityLevel

SecurityLevel

設(shè)置數(shù)據(jù)庫(kù)安全級(jí)別

encrypt

boolean

指定數(shù)據(jù)庫(kù)是否加密,默認(rèn)不加密。

true:加密。

false:非加密。

SecurityLevel

數(shù)據(jù)庫(kù)的安全級(jí)別枚舉。

系統(tǒng)能力: SystemCapability.DistributedDataManager.RelationalStore.Core

名稱

說(shuō)明

S1

1

表示數(shù)據(jù)庫(kù)的安全級(jí)別為低級(jí)別,當(dāng)數(shù)據(jù)泄露時(shí)會(huì)產(chǎn)生較低影響。例如,包含壁紙等系統(tǒng)數(shù)據(jù)的數(shù)據(jù)庫(kù)。

S2

2

表示數(shù)據(jù)庫(kù)的安全級(jí)別為中級(jí)別,當(dāng)數(shù)據(jù)泄露時(shí)會(huì)產(chǎn)生較大影響。例如,包含錄音、視頻等用戶生成數(shù)據(jù)或通話記錄等信息的數(shù)據(jù)庫(kù)。

S3

3

表示數(shù)據(jù)庫(kù)的安全級(jí)別為高級(jí)別,當(dāng)數(shù)據(jù)泄露時(shí)會(huì)產(chǎn)生重大影響。例如,包含用戶運(yùn)動(dòng)、健康、位置等信息的數(shù)據(jù)庫(kù)。

S4

4

表示數(shù)據(jù)庫(kù)的安全級(jí)別為關(guān)鍵級(jí)別,當(dāng)數(shù)據(jù)泄露時(shí)會(huì)產(chǎn)生嚴(yán)重影響。例如,包含認(rèn)證憑據(jù)、財(cái)務(wù)數(shù)據(jù)等信息的數(shù)據(jù)庫(kù)。

ValueType

用于表示允許的數(shù)據(jù)字段類型。

系統(tǒng)能力: SystemCapability.DistributedDataManager.RelationalStore.Core

類型

說(shuō)明

number

表示值類型為數(shù)字。

string

表示值類型為字符。

boolean

表示值類型為布爾值。

ValuesBucket

用于存儲(chǔ)鍵值對(duì)的類型。該類型不是多線程安全的,如果應(yīng)用中存在多線程同時(shí)操作該類派生出的實(shí)例,注意加鎖保護(hù)。

系統(tǒng)能力: SystemCapability.DistributedDataManager.RelationalStore.Core

鍵類型

值類型

string

ValueType| Uint8Array | null

SyncMode

指數(shù)據(jù)庫(kù)同步模式。

說(shuō)明

當(dāng)前HarmonyOS設(shè)備均不支持此接口,此接口在被sync()接口調(diào)用時(shí)將返回801錯(cuò)誤碼。

系統(tǒng)能力: SystemCapability.DistributedDataManager.RelationalStore.Core

名稱

說(shuō)明

SYNC_MODE_PUSH

0

表示數(shù)據(jù)從本地設(shè)備推送到遠(yuǎn)程設(shè)備。

SYNC_MODE_PULL

1

表示數(shù)據(jù)從遠(yuǎn)程設(shè)備拉至本地設(shè)備。

SubscribeType

描述訂閱類型。

說(shuō)明

當(dāng)前HarmonyOS設(shè)備均不支持此接口,此接口在被on('dataChange')、off('dataChange')接口調(diào)用時(shí)將返回801錯(cuò)誤碼。

需要權(quán)限: ohos.permission.DISTRIBUTED_DATASYNC

系統(tǒng)能力: SystemCapability.DistributedDataManager.RelationalStore.Core

名稱

說(shuō)明

SUBSCRIBE_TYPE_REMOTE

0

訂閱遠(yuǎn)程數(shù)據(jù)更改。

RdbPredicates

表示關(guān)系型數(shù)據(jù)庫(kù)(RDB)的謂詞。該類確定RDB中條件表達(dá)式的值是true還是false。該類型不是多線程安全的,如果應(yīng)用中存在多線程同時(shí)操作該類派生出的實(shí)例,注意加鎖保護(hù)。

constructor

constructor(name: string)

構(gòu)造函數(shù)。

系統(tǒng)能力: SystemCapability.DistributedDataManager.RelationalStore.Core

參數(shù):

參數(shù)名

類型

必填

說(shuō)明

name

string

數(shù)據(jù)庫(kù)表名。

示例:

  1. let predicates = new relationalStore.RdbPredicates("EMPLOYEE");

inDevices

inDevices(devices: Array<string>): RdbPredicates

同步分布式數(shù)據(jù)庫(kù)時(shí)連接到組網(wǎng)內(nèi)指定的遠(yuǎn)程設(shè)備。

說(shuō)明

當(dāng)前HarmonyOS設(shè)備均不支持此接口,應(yīng)用調(diào)用此接口后再調(diào)用remoteQuery()接口將返回801錯(cuò)誤碼。

系統(tǒng)能力: SystemCapability.DistributedDataManager.RelationalStore.Core

參數(shù):

參數(shù)名

類型

必填

說(shuō)明

devices

Array<string>

指定的組網(wǎng)內(nèi)的遠(yuǎn)程設(shè)備ID。

返回值

類型

說(shuō)明

RdbPredicates

返回與指定字段匹配的謂詞。

示例:

  1. let predicates = new relationalStore.RdbPredicates("EMPLOYEE");
  2. predicates.inDevices(['12345678abcde']);

inAllDevices

inAllDevices(): RdbPredicates

同步分布式數(shù)據(jù)庫(kù)時(shí)連接到組網(wǎng)內(nèi)所有的遠(yuǎn)程設(shè)備。

說(shuō)明

當(dāng)前HarmonyOS設(shè)備均不支持此接口,應(yīng)用調(diào)用此接口后再調(diào)用remoteQuery()接口將返回801錯(cuò)誤碼。

系統(tǒng)能力: SystemCapability.DistributedDataManager.RelationalStore.Core

返回值

類型

說(shuō)明

RdbPredicates

返回與指定字段匹配的謂詞。

示例:

  1. let predicates = new relationalStore.RdbPredicates("EMPLOYEE");
  2. predicates.inAllDevices();

equalTo

equalTo(field: string, value: ValueType): RdbPredicates

配置謂詞以匹配數(shù)據(jù)表的field列中值為value的字段。

系統(tǒng)能力: SystemCapability.DistributedDataManager.RelationalStore.Core

參數(shù):

參數(shù)名

類型

必填

說(shuō)明

field

string

數(shù)據(jù)庫(kù)表中的列名。

value

ValueType

指示要與謂詞匹配的值。

返回值

類型

說(shuō)明

RdbPredicates

返回與指定字段匹配的謂詞。

示例:

  1. let predicates = new relationalStore.RdbPredicates("EMPLOYEE");
  2. predicates.equalTo("NAME", "lisi");

notEqualTo

notEqualTo(field: string, value: ValueType): RdbPredicates

配置謂詞以匹配數(shù)據(jù)表的field列中值不為value的字段。

系統(tǒng)能力: SystemCapability.DistributedDataManager.RelationalStore.Core

參數(shù):

參數(shù)名

類型

必填

說(shuō)明

field

string

數(shù)據(jù)庫(kù)表中的列名。

value

ValueType

指示要與謂詞匹配的值。

返回值

類型

說(shuō)明

RdbPredicates

返回與指定字段匹配的謂詞。

示例:

  1. let predicates = new relationalStore.RdbPredicates("EMPLOYEE");
  2. predicates.notEqualTo("NAME", "lisi");

beginWrap

beginWrap(): RdbPredicates

向謂詞添加左括號(hào)。

系統(tǒng)能力: SystemCapability.DistributedDataManager.RelationalStore.Core

返回值

類型

說(shuō)明

RdbPredicates

返回帶有左括號(hào)的Rdb謂詞。

示例:

  1. let predicates = new relationalStore.RdbPredicates("EMPLOYEE");
  2. predicates.equalTo("NAME", "lisi")
  3. .beginWrap()
  4. .equalTo("AGE", 18)
  5. .or()
  6. .equalTo("SALARY", 200.5)
  7. .endWrap()

endWrap

endWrap(): RdbPredicates

向謂詞添加右括號(hào)。

系統(tǒng)能力: SystemCapability.DistributedDataManager.RelationalStore.Core

返回值

類型

說(shuō)明

RdbPredicates

返回帶有右括號(hào)的Rdb謂詞。

示例:

  1. let predicates = new relationalStore.RdbPredicates("EMPLOYEE");
  2. predicates.equalTo("NAME", "lisi")
  3. .beginWrap()
  4. .equalTo("AGE", 18)
  5. .or()
  6. .equalTo("SALARY", 200.5)
  7. .endWrap()

or

or(): RdbPredicates

將或條件添加到謂詞中。

系統(tǒng)能力: SystemCapability.DistributedDataManager.RelationalStore.Core

返回值

類型

說(shuō)明

RdbPredicates

返回帶有或條件的Rdb謂詞。

示例:

  1. let predicates = new relationalStore.RdbPredicates("EMPLOYEE");
  2. predicates.equalTo("NAME", "Lisa")
  3. .or()
  4. .equalTo("NAME", "Rose")

and

and(): RdbPredicates

向謂詞添加和條件。

系統(tǒng)能力: SystemCapability.DistributedDataManager.RelationalStore.Core

返回值

類型

說(shuō)明

RdbPredicates

返回帶有和條件的Rdb謂詞。

示例:

  1. let predicates = new relationalStore.RdbPredicates("EMPLOYEE");
  2. predicates.equalTo("NAME", "Lisa")
  3. .and()
  4. .equalTo("SALARY", 200.5)

contains

contains(field: string, value: string): RdbPredicates

配置謂詞以匹配數(shù)據(jù)表的field列中包含value的字段。

系統(tǒng)能力: SystemCapability.DistributedDataManager.RelationalStore.Core

參數(shù):

參數(shù)名

類型

必填

說(shuō)明

field

string

數(shù)據(jù)庫(kù)表中的列名。

value

string

指示要與謂詞匹配的值。

返回值

類型

說(shuō)明

RdbPredicates

返回與指定字段匹配的謂詞。

示例:

  1. let predicates = new relationalStore.RdbPredicates("EMPLOYEE");
  2. predicates.contains("NAME", "os");

beginsWith

beginsWith(field: string, value: string): RdbPredicates

配置謂詞以匹配數(shù)據(jù)表的field列中以value開頭的字段。

系統(tǒng)能力: SystemCapability.DistributedDataManager.RelationalStore.Core

參數(shù):

參數(shù)名

類型

必填

說(shuō)明

field

string

數(shù)據(jù)庫(kù)表中的列名。

value

string

指示要與謂詞匹配的值。

返回值

類型

說(shuō)明

RdbPredicates

返回與指定字段匹配的謂詞。

示例:

  1. let predicates = new relationalStore.RdbPredicates("EMPLOYEE");
  2. predicates.beginsWith("NAME", "os");

endsWith

endsWith(field: string, value: string): RdbPredicates

配置謂詞以匹配數(shù)據(jù)表的field列中以value結(jié)尾的字段。

系統(tǒng)能力: SystemCapability.DistributedDataManager.RelationalStore.Core

參數(shù):

參數(shù)名

類型

必填

說(shuō)明

field

string

數(shù)據(jù)庫(kù)表中的列名。

value

string

指示要與謂詞匹配的值。

返回值

類型

說(shuō)明

RdbPredicates

返回與指定字段匹配的謂詞。

示例:

  1. let predicates = new relationalStore.RdbPredicates("EMPLOYEE");
  2. predicates.endsWith("NAME", "se");

isNull

isNull(field: string): RdbPredicates

配置謂詞以匹配數(shù)據(jù)表的field列中值為null的字段。

系統(tǒng)能力: SystemCapability.DistributedDataManager.RelationalStore.Core

參數(shù):

參數(shù)名

類型

必填

說(shuō)明

field

string

數(shù)據(jù)庫(kù)表中的列名。

返回值

類型

說(shuō)明

RdbPredicates

返回與指定字段匹配的謂詞。

示例

  1. let predicates = new relationalStore.RdbPredicates("EMPLOYEE");
  2. predicates.isNull("NAME");

isNotNull

isNotNull(field: string): RdbPredicates

配置謂詞以匹配數(shù)據(jù)表的field列中值不為null的字段。

系統(tǒng)能力: SystemCapability.DistributedDataManager.RelationalStore.Core

參數(shù):

參數(shù)名

類型

必填

說(shuō)明

field

string

數(shù)據(jù)庫(kù)表中的列名。

返回值

類型

說(shuō)明

RdbPredicates

返回與指定字段匹配的謂詞。

示例:

  1. let predicates = new relationalStore.RdbPredicates("EMPLOYEE");
  2. predicates.isNotNull("NAME");

like

like(field: string, value: string): RdbPredicates

配置謂詞以匹配數(shù)據(jù)表的field列中值類似于value的字段。

系統(tǒng)能力: SystemCapability.DistributedDataManager.RelationalStore.Core

參數(shù):

參數(shù)名

類型

必填

說(shuō)明

field

string

數(shù)據(jù)庫(kù)表中的列名。

value

string

指示要與謂詞匹配的值。

返回值

類型

說(shuō)明

RdbPredicates

返回與指定字段匹配的謂詞。

示例:

  1. let predicates = new relationalStore.RdbPredicates("EMPLOYEE");
  2. predicates.like("NAME", "%os%");

glob

glob(field: string, value: string): RdbPredicates

配置謂詞匹配數(shù)據(jù)字段為string的指定字段。

系統(tǒng)能力: SystemCapability.DistributedDataManager.RelationalStore.Core

參數(shù):

參數(shù)名

類型

必填

說(shuō)明

field

string

數(shù)據(jù)庫(kù)表中的列名。

value

string

指示要與謂詞匹配的值。

支持通配符,*表示0個(gè)、1個(gè)或多個(gè)數(shù)字或字符,?表示1個(gè)數(shù)字或字符。

返回值

類型

說(shuō)明

RdbPredicates

返回與指定字段匹配的謂詞。

示例:

  1. let predicates = new relationalStore.RdbPredicates("EMPLOYEE");
  2. predicates.glob("NAME", "?h*g");

between

between(field: string, low: ValueType, high: ValueType): RdbPredicates

配置謂詞以匹配數(shù)據(jù)表的field列中值在給定范圍內(nèi)的字段。

系統(tǒng)能力: SystemCapability.DistributedDataManager.RelationalStore.Core

參數(shù):

參數(shù)名

類型

必填

說(shuō)明

field

string

數(shù)據(jù)庫(kù)表中的列名。

low

ValueType

指示與謂詞匹配的最小值。

high

ValueType

指示要與謂詞匹配的最大值。

返回值

類型

說(shuō)明

RdbPredicates

返回與指定字段匹配的謂詞。

示例:

  1. let predicates = new relationalStore.RdbPredicates("EMPLOYEE");
  2. predicates.between("AGE", 10, 50);

notBetween

notBetween(field: string, low: ValueType, high: ValueType): RdbPredicates

配置謂詞以匹配數(shù)據(jù)表的field列中值超出給定范圍的字段。

系統(tǒng)能力: SystemCapability.DistributedDataManager.RelationalStore.Core

參數(shù):

參數(shù)名

類型

必填

說(shuō)明

field

string

數(shù)據(jù)庫(kù)表中的列名。

low

ValueType

指示與謂詞匹配的最小值。

high

ValueType

指示要與謂詞匹配的最大值。

返回值

類型

說(shuō)明

RdbPredicates

返回與指定字段匹配的謂詞。

示例:

  1. let predicates = new relationalStore.RdbPredicates("EMPLOYEE");
  2. predicates.notBetween("AGE", 10, 50);

greaterThan

greaterThan(field: string, value: ValueType): RdbPredicates

配置謂詞以匹配數(shù)據(jù)表的field列中值大于value的字段。

系統(tǒng)能力: SystemCapability.DistributedDataManager.RelationalStore.Core

參數(shù):

參數(shù)名

類型

必填

說(shuō)明

field

string

數(shù)據(jù)庫(kù)表中的列名。

value

ValueType

指示要與謂詞匹配的值。

返回值

類型

說(shuō)明

RdbPredicates

返回與指定字段匹配的謂詞。

示例:

  1. let predicates = new relationalStore.RdbPredicates("EMPLOYEE");
  2. predicates.greaterThan("AGE", 18);

lessThan

lessThan(field: string, value: ValueType): RdbPredicates

配置謂詞以匹配數(shù)據(jù)表的field列中值小于value的字段。

系統(tǒng)能力: SystemCapability.DistributedDataManager.RelationalStore.Core

參數(shù):

參數(shù)名

類型

必填

說(shuō)明

field

string

數(shù)據(jù)庫(kù)表中的列名。

value

ValueType

指示要與謂詞匹配的值。

返回值

類型

說(shuō)明

RdbPredicates

返回與指定字段匹配的謂詞。

示例:

  1. let predicates = new relationalStore.RdbPredicates("EMPLOYEE");
  2. predicates.lessThan("AGE", 20);

greaterThanOrEqualTo

greaterThanOrEqualTo(field: string, value: ValueType): RdbPredicates

配置謂詞以匹配數(shù)據(jù)表的field列中值大于或者等于value的字段。

系統(tǒng)能力: SystemCapability.DistributedDataManager.RelationalStore.Core

參數(shù):

參數(shù)名

類型

必填

說(shuō)明

field

string

數(shù)據(jù)庫(kù)表中的列名。

value

ValueType

指示要與謂詞匹配的值。

返回值

類型

說(shuō)明

RdbPredicates

返回與指定字段匹配的謂詞。

示例:

  1. let predicates = new relationalStore.RdbPredicates("EMPLOYEE");
  2. predicates.greaterThanOrEqualTo("AGE", 18);

lessThanOrEqualTo

lessThanOrEqualTo(field: string, value: ValueType): RdbPredicates

配置謂詞以匹配數(shù)據(jù)表的field列中值小于或者等于value的字段。

系統(tǒng)能力: SystemCapability.DistributedDataManager.RelationalStore.Core

參數(shù):

參數(shù)名

類型

必填

說(shuō)明

field

string

數(shù)據(jù)庫(kù)表中的列名。

value

ValueType

指示要與謂詞匹配的值。

返回值

類型

說(shuō)明

RdbPredicates

返回與指定字段匹配的謂詞。

示例:

  1. let predicates = new relationalStore.RdbPredicates("EMPLOYEE");
  2. predicates.lessThanOrEqualTo("AGE", 20);

orderByAsc

orderByAsc(field: string): RdbPredicates

配置謂詞以匹配數(shù)據(jù)表的field列中值按升序排序的列。

系統(tǒng)能力: SystemCapability.DistributedDataManager.RelationalStore.Core

參數(shù):

參數(shù)名

類型

必填

說(shuō)明

field

string

數(shù)據(jù)庫(kù)表中的列名。

返回值

類型

說(shuō)明

RdbPredicates

返回與指定字段匹配的謂詞。

示例:

  1. let predicates = new relationalStore.RdbPredicates("EMPLOYEE");
  2. predicates.orderByAsc("NAME");

orderByDesc

orderByDesc(field: string): RdbPredicates

配置謂詞以匹配數(shù)據(jù)表的field列中值按降序排序的列。

系統(tǒng)能力: SystemCapability.DistributedDataManager.RelationalStore.Core

參數(shù):

參數(shù)名

類型

必填

說(shuō)明

field

string

數(shù)據(jù)庫(kù)表中的列名。

返回值

類型

說(shuō)明

RdbPredicates

返回與指定字段匹配的謂詞。

示例:

  1. let predicates = new relationalStore.RdbPredicates("EMPLOYEE");
  2. predicates.orderByDesc("AGE");

distinct

distinct(): RdbPredicates

配置謂詞以過(guò)濾重復(fù)記錄并僅保留其中一個(gè)。

系統(tǒng)能力: SystemCapability.DistributedDataManager.RelationalStore.Core

返回值

類型

說(shuō)明

RdbPredicates

返回可用于過(guò)濾重復(fù)記錄的謂詞。

示例:

  1. let predicates = new relationalStore.RdbPredicates("EMPLOYEE");
  2. predicates.equalTo("NAME", "Rose").distinct();

limitAs

limitAs(value: number): RdbPredicates

設(shè)置最大數(shù)據(jù)記錄數(shù)的謂詞。

系統(tǒng)能力: SystemCapability.DistributedDataManager.RelationalStore.Core

參數(shù):

參數(shù)名

類型

必填

說(shuō)明

value

number

最大數(shù)據(jù)記錄數(shù)。

返回值

類型

說(shuō)明

RdbPredicates

返回可用于設(shè)置最大數(shù)據(jù)記錄數(shù)的謂詞。

示例:

  1. let predicates = new relationalStore.RdbPredicates("EMPLOYEE");
  2. predicates.equalTo("NAME", "Rose").limitAs(3);

offsetAs

offsetAs(rowOffset: number): RdbPredicates

配置謂詞以指定返回結(jié)果的起始位置。

系統(tǒng)能力: SystemCapability.DistributedDataManager.RelationalStore.Core

參數(shù):

參數(shù)名

類型

必填

說(shuō)明

rowOffset

number

返回結(jié)果的起始位置,取值為正整數(shù)。

返回值

類型

說(shuō)明

RdbPredicates

返回具有指定返回結(jié)果起始位置的謂詞。

示例:

  1. let predicates = new relationalStore.RdbPredicates("EMPLOYEE");
  2. predicates.equalTo("NAME", "Rose").offsetAs(3);

groupBy

groupBy(fields: Array<string>): RdbPredicates

配置謂詞按指定列分組查詢結(jié)果。

系統(tǒng)能力: SystemCapability.DistributedDataManager.RelationalStore.Core

參數(shù):

參數(shù)名

類型

必填

說(shuō)明

fields

Array<string>

指定分組依賴的列名。

返回值

類型

說(shuō)明

RdbPredicates

返回分組查詢列的謂詞。

示例:

  1. let predicates = new relationalStore.RdbPredicates("EMPLOYEE");
  2. predicates.groupBy(["AGE", "NAME"]);

indexedBy

indexedBy(field: string): RdbPredicates

配置謂詞以指定索引列。

系統(tǒng)能力: SystemCapability.DistributedDataManager.RelationalStore.Core

參數(shù):

參數(shù)名

類型

必填

說(shuō)明

field

string

索引列的名稱。

返回值

類型

說(shuō)明

RdbPredicates

返回具有指定索引列的RdbPredicates。

示例:

  1. let predicates = new relationalStore.RdbPredicates("EMPLOYEE");
  2. predicates.indexedBy("SALARY_INDEX");

in

in(field: string, value: Array<ValueType>): RdbPredicates

配置謂詞以匹配數(shù)據(jù)表的field列中值在給定范圍內(nèi)的字段。

系統(tǒng)能力: SystemCapability.DistributedDataManager.RelationalStore.Core

參數(shù):

參數(shù)名

類型

必填

說(shuō)明

field

string

數(shù)據(jù)庫(kù)表中的列名。

value

Array<ValueType>

以ValueType型數(shù)組形式指定的要匹配的值。

返回值

類型

說(shuō)明

RdbPredicates

返回與指定字段匹配的謂詞。

示例:

  1. let predicates = new relationalStore.RdbPredicates("EMPLOYEE");
  2. predicates.in("AGE", [18, 20]);

notIn

notIn(field: string, value: Array<ValueType>): RdbPredicates

將謂詞配置為匹配數(shù)據(jù)字段為ValueType且值超出給定范圍的指定字段。

系統(tǒng)能力: SystemCapability.DistributedDataManager.RelationalStore.Core

參數(shù):

參數(shù)名

類型

必填

說(shuō)明

field

string

數(shù)據(jù)庫(kù)表中的列名。

value

Array<ValueType>

以ValueType數(shù)組形式指定的要匹配的值。

返回值

類型

說(shuō)明

RdbPredicates

返回與指定字段匹配的謂詞。

示例:

  1. let predicates = new relationalStore.RdbPredicates("EMPLOYEE");
  2. predicates.notIn("NAME", ["Lisa", "Rose"]);

RdbStore

提供管理關(guān)系數(shù)據(jù)庫(kù)(RDB)方法的接口。

在使用以下相關(guān)接口前,請(qǐng)使用executeSql接口初始化數(shù)據(jù)庫(kù)表結(jié)構(gòu)和相關(guān)數(shù)據(jù),具體可見(jiàn)通過(guò)關(guān)系型數(shù)據(jù)庫(kù)實(shí)現(xiàn)數(shù)據(jù)持久化

insert

insert(table: string, values: ValuesBucket, callback: AsyncCallback<number>):void

向目標(biāo)表中插入一行數(shù)據(jù),使用callback異步回調(diào)。

系統(tǒng)能力: SystemCapability.DistributedDataManager.RelationalStore.Core

參數(shù):

參數(shù)名

類型

必填

說(shuō)明

table

string

指定的目標(biāo)表名。

values

ValuesBucket

表示要插入到表中的數(shù)據(jù)行。

callback

AsyncCallback<number>

指定callback回調(diào)函數(shù)。如果操作成功,返回行ID;否則返回-1。

示例:

  1. const valueBucket = {
  2. "NAME": "Lisa",
  3. "AGE": 18,
  4. "SALARY": 100.5,
  5. "CODES": new Uint8Array([1, 2, 3, 4, 5]),
  6. };
  7. store.insert("EMPLOYEE", valueBucket, function (err, rowId) {
  8. if (err) {
  9. console.error(`Insert is failed, code is ${err.code},message is ${err.message}`);
  10. return;
  11. }
  12. console.info(`Insert is successful, rowId = ${rowId}`);
  13. })

insert

insert(table: string, values: ValuesBucket):Promise<number>

向目標(biāo)表中插入一行數(shù)據(jù),使用Promise異步回調(diào)。

系統(tǒng)能力: SystemCapability.DistributedDataManager.RelationalStore.Core

參數(shù):

參數(shù)名

類型

必填

說(shuō)明

table

string

指定的目標(biāo)表名。

values

ValuesBucket

表示要插入到表中的數(shù)據(jù)行。

返回值

類型

說(shuō)明

Promise<number>

Promise對(duì)象。如果操作成功,返回行ID;否則返回-1。

示例:

  1. const valueBucket = {
  2. "NAME": "Lisa",
  3. "AGE": 18,
  4. "SALARY": 100.5,
  5. "CODES": new Uint8Array([1, 2, 3, 4, 5]),
  6. };
  7. let promise = store.insert("EMPLOYEE", valueBucket);
  8. promise.then((rowId) => {
  9. console.info(`Insert is successful, rowId = ${rowId}`);
  10. }).catch((err) => {
  11. console.error(`Insert is failed, code is ${err.code},message is ${err.message}`);
  12. })

batchInsert

batchInsert(table: string, values: Array<ValuesBucket>, callback: AsyncCallback<number>):void

向目標(biāo)表中插入一組數(shù)據(jù),使用callback異步回調(diào)。

系統(tǒng)能力: SystemCapability.DistributedDataManager.RelationalStore.Core

參數(shù):

參數(shù)名

類型

必填

說(shuō)明

table

string

指定的目標(biāo)表名。

values

Array<ValuesBucket>

表示要插入到表中的一組數(shù)據(jù)。

callback

AsyncCallback<number>

指定callback回調(diào)函數(shù)。如果操作成功,返回插入的數(shù)據(jù)個(gè)數(shù),否則返回-1。

示例:

  1. const valueBucket1 = {
  2. "NAME": "Lisa",
  3. "AGE": 18,
  4. "SALARY": 100.5,
  5. "CODES": new Uint8Array([1, 2, 3, 4, 5])
  6. };
  7. const valueBucket2 = {
  8. "NAME": "Jack",
  9. "AGE": 19,
  10. "SALARY": 101.5,
  11. "CODES": new Uint8Array([6, 7, 8, 9, 10])
  12. };
  13. const valueBucket3 = {
  14. "NAME": "Tom",
  15. "AGE": 20,
  16. "SALARY": 102.5,
  17. "CODES": new Uint8Array([11, 12, 13, 14, 15])
  18. };
  19. let valueBuckets = new Array(valueBucket1, valueBucket2, valueBucket3);
  20. store.batchInsert("EMPLOYEE", valueBuckets, function(err, insertNum) {
  21. if (err) {
  22. console.error(`batchInsert is failed, code is ${err.code},message is ${err.message}`);
  23. return;
  24. }
  25. console.info(`batchInsert is successful, the number of values that were inserted = ${insertNum}`);
  26. })

batchInsert

batchInsert(table: string, values: Array<ValuesBucket>):Promise<number>

向目標(biāo)表中插入一組數(shù)據(jù),使用Promise異步回調(diào)。

系統(tǒng)能力: SystemCapability.DistributedDataManager.RelationalStore.Core

參數(shù):

參數(shù)名

類型

必填

說(shuō)明

table

string

指定的目標(biāo)表名。

values

Array<ValuesBucket>

表示要插入到表中的一組數(shù)據(jù)。

返回值

類型

說(shuō)明

Promise<number>

Promise對(duì)象。如果操作成功,返回插入的數(shù)據(jù)個(gè)數(shù),否則返回-1。

示例:

  1. const valueBucket1 = {
  2. "NAME": "Lisa",
  3. "AGE": 18,
  4. "SALARY": 100.5,
  5. "CODES": new Uint8Array([1, 2, 3, 4, 5])
  6. };
  7. const valueBucket2 = {
  8. "NAME": "Jack",
  9. "AGE": 19,
  10. "SALARY": 101.5,
  11. "CODES": new Uint8Array([6, 7, 8, 9, 10])
  12. };
  13. const valueBucket3 = {
  14. "NAME": "Tom",
  15. "AGE": 20,
  16. "SALARY": 102.5,
  17. "CODES": new Uint8Array([11, 12, 13, 14, 15])
  18. };
  19. let valueBuckets = new Array(valueBucket1, valueBucket2, valueBucket3);
  20. let promise = store.batchInsert("EMPLOYEE", valueBuckets);
  21. promise.then((insertNum) => {
  22. console.info(`batchInsert is successful, the number of values that were inserted = ${insertNum}`);
  23. }).catch((err) => {
  24. console.error(`batchInsert is failed, code is ${err.code},message is ${err.message}`);
  25. })

update

update(values: ValuesBucket, predicates: RdbPredicates, callback: AsyncCallback<number>):void

根據(jù)RdbPredicates的指定實(shí)例對(duì)象更新數(shù)據(jù)庫(kù)中的數(shù)據(jù),使用callback異步回調(diào)。

系統(tǒng)能力: SystemCapability.DistributedDataManager.RelationalStore.Core

參數(shù):

參數(shù)名

類型

必填

說(shuō)明

values

ValuesBucket

values指示數(shù)據(jù)庫(kù)中要更新的數(shù)據(jù)行。鍵值對(duì)與數(shù)據(jù)庫(kù)表的列名相關(guān)聯(lián)。

predicates

RdbPredicates

RdbPredicates的實(shí)例對(duì)象指定的更新條件。

callback

AsyncCallback<number>

指定的callback回調(diào)方法。返回受影響的行數(shù)。

示例:

  1. const valueBucket = {
  2. "NAME": "Rose",
  3. "AGE": 22,
  4. "SALARY": 200.5,
  5. "CODES": new Uint8Array([1, 2, 3, 4, 5]),
  6. };
  7. let predicates = new relationalStore.RdbPredicates("EMPLOYEE");
  8. predicates.equalTo("NAME", "Lisa");
  9. store.update(valueBucket, predicates, function (err, rows) {
  10. if (err) {
  11. console.error(`Updated failed, code is ${err.code},message is ${err.message}`);
  12. return;
  13. }
  14. console.info(`Updated row count: ${rows}`);
  15. })

update

update(values: ValuesBucket, predicates: RdbPredicates):Promise<number>

根據(jù)RdbPredicates的指定實(shí)例對(duì)象更新數(shù)據(jù)庫(kù)中的數(shù)據(jù),使用Promise異步回調(diào)。

系統(tǒng)能力: SystemCapability.DistributedDataManager.RelationalStore.Core

參數(shù):

參數(shù)名

類型

必填

說(shuō)明

values

ValuesBucket

values指示數(shù)據(jù)庫(kù)中要更新的數(shù)據(jù)行。鍵值對(duì)與數(shù)據(jù)庫(kù)表的列名相關(guān)聯(lián)。

predicates

RdbPredicates

RdbPredicates的實(shí)例對(duì)象指定的更新條件。

返回值

類型

說(shuō)明

Promise<number>

指定的Promise回調(diào)方法。返回受影響的行數(shù)。

示例:

  1. const valueBucket = {
  2. "NAME": "Rose",
  3. "AGE": 22,
  4. "SALARY": 200.5,
  5. "CODES": new Uint8Array([1, 2, 3, 4, 5]),
  6. };
  7. let predicates = new relationalStore.RdbPredicates("EMPLOYEE");
  8. predicates.equalTo("NAME", "Lisa");
  9. let promise = store.update(valueBucket, predicates);
  10. promise.then(async (rows) => {
  11. console.info(`Updated row count: ${rows}`);
  12. }).catch((err) => {
  13. console.error(`Updated failed, code is ${err.code},message is ${err.message}`);
  14. })

delete

delete(predicates: RdbPredicates, callback: AsyncCallback<number>):void

根據(jù)RdbPredicates的指定實(shí)例對(duì)象從數(shù)據(jù)庫(kù)中刪除數(shù)據(jù),使用callback異步回調(diào)。

系統(tǒng)能力: SystemCapability.DistributedDataManager.RelationalStore.Core

參數(shù):

參數(shù)名

類型

必填

說(shuō)明

predicates

RdbPredicates

RdbPredicates的實(shí)例對(duì)象指定的刪除條件。

callback

AsyncCallback<number>

指定callback回調(diào)函數(shù)。返回受影響的行數(shù)。

示例:

  1. let predicates = new relationalStore.RdbPredicates("EMPLOYEE");
  2. predicates.equalTo("NAME", "Lisa");
  3. store.delete(predicates, function (err, rows) {
  4. if (err) {
  5. console.error(`Delete failed, code is ${err.code},message is ${err.message}`);
  6. return;
  7. }
  8. console.info(`Delete rows: ${rows}`);
  9. })

delete

delete(predicates: RdbPredicates):Promise<number>

根據(jù)RdbPredicates的指定實(shí)例對(duì)象從數(shù)據(jù)庫(kù)中刪除數(shù)據(jù),使用Promise異步回調(diào)。

系統(tǒng)能力: SystemCapability.DistributedDataManager.RelationalStore.Core

參數(shù):

參數(shù)名

類型

必填

說(shuō)明

predicates

RdbPredicates

RdbPredicates的實(shí)例對(duì)象指定的刪除條件。

返回值

類型

說(shuō)明

Promise<number>

Promise對(duì)象。返回受影響的行數(shù)。

示例:

  1. let predicates = new relationalStore.RdbPredicates("EMPLOYEE");
  2. predicates.equalTo("NAME", "Lisa");
  3. let promise = store.delete(predicates);
  4. promise.then((rows) => {
  5. console.info(`Delete rows: ${rows}`);
  6. }).catch((err) => {
  7. console.error(`Delete failed, code is ${err.code},message is ${err.message}`);
  8. })

query

query(predicates: RdbPredicates, columns: Array<string>, callback: AsyncCallback<ResultSet>):void

根據(jù)指定條件查詢數(shù)據(jù)庫(kù)中的數(shù)據(jù),使用callback異步回調(diào)。

系統(tǒng)能力: SystemCapability.DistributedDataManager.RelationalStore.Core

參數(shù):

參數(shù)名

類型

必填

說(shuō)明

predicates

RdbPredicates

RdbPredicates的實(shí)例對(duì)象指定的查詢條件。

columns

Array<string>

表示要查詢的列。如果值為空,則查詢應(yīng)用于所有列。

callback

AsyncCallback<ResultSet>

指定callback回調(diào)函數(shù)。如果操作成功,則返回ResultSet對(duì)象。

示例:

  1. let predicates = new relationalStore.RdbPredicates("EMPLOYEE");
  2. predicates.equalTo("NAME", "Rose");
  3. store.query(predicates, ["ID", "NAME", "AGE", "SALARY", "CODES"], function (err, resultSet) {
  4. if (err) {
  5. console.error(`Query failed, code is ${err.code},message is ${err.message}`);
  6. return;
  7. }
  8. console.info(`ResultSet column names: ${resultSet.columnNames}`);
  9. console.info(`ResultSet column count: ${resultSet.columnCount}`);
  10. })

query

query(predicates: RdbPredicates, columns?: Array<string>):Promise<ResultSet>

根據(jù)指定條件查詢數(shù)據(jù)庫(kù)中的數(shù)據(jù),使用Promise異步回調(diào)。

系統(tǒng)能力: SystemCapability.DistributedDataManager.RelationalStore.Core

參數(shù):

參數(shù)名

類型

必填

說(shuō)明

predicates

RdbPredicates

RdbPredicates的實(shí)例對(duì)象指定的查詢條件。

columns

Array<string>

表示要查詢的列。如果值為空,則查詢應(yīng)用于所有列。

返回值

類型

說(shuō)明

Promise<ResultSet>

Promise對(duì)象。如果操作成功,則返回ResultSet對(duì)象。

示例:

  1. let predicates = new relationalStore.RdbPredicates("EMPLOYEE");
  2. predicates.equalTo("NAME", "Rose");
  3. let promise = store.query(predicates, ["ID", "NAME", "AGE", "SALARY", "CODES"]);
  4. promise.then((resultSet) => {
  5. console.info(`ResultSet column names: ${resultSet.columnNames}`);
  6. console.info(`ResultSet column count: ${resultSet.columnCount}`);
  7. }).catch((err) => {
  8. console.error(`Query failed, code is ${err.code},message is ${err.message}`);
  9. })

remoteQuery

remoteQuery(device: string, table: string, predicates: RdbPredicates, columns: Array<string> , callback: AsyncCallback<ResultSet>): void

根據(jù)指定條件查詢遠(yuǎn)程設(shè)備數(shù)據(jù)庫(kù)中的數(shù)據(jù)。使用callback異步回調(diào)。

說(shuō)明

當(dāng)前HarmonyOS設(shè)備均不支持此接口,應(yīng)用調(diào)用此接口將返回801錯(cuò)誤碼。

系統(tǒng)能力: SystemCapability.DistributedDataManager.RelationalStore.Core

參數(shù):

參數(shù)名

類型

必填

說(shuō)明

device

string

指定的遠(yuǎn)程設(shè)備的networkId。

table

string

指定的目標(biāo)表名。

predicates

RdbPredicates

RdbPredicates的實(shí)例對(duì)象,指定查詢的條件。

columns

Array<string>

表示要查詢的列。如果值為空,則查詢應(yīng)用于所有列。

callback

AsyncCallback<ResultSet>

指定callback回調(diào)函數(shù)。如果操作成功,則返回ResultSet對(duì)象。

示例:

  1. let predicates = new relationalStore.RdbPredicates('EMPLOYEE');
  2. predicates.greaterThan("id", 0);
  3. store.remoteQuery("deviceId", "EMPLOYEE", predicates, ["ID", "NAME", "AGE", "SALARY", "CODES"],
  4. function(err, resultSet) {
  5. if (err) {
  6. console.error(`Failed to remoteQuery, code is ${err.code},message is ${err.message}`);
  7. return;
  8. }
  9. console.info(`ResultSet column names: ${resultSet.columnNames}`);
  10. console.info(`ResultSet column count: ${resultSet.columnCount}`);
  11. }
  12. )

remoteQuery

remoteQuery(device: string, table: string, predicates: RdbPredicates, columns: Array<string>): Promise<ResultSet>

根據(jù)指定條件查詢遠(yuǎn)程設(shè)備數(shù)據(jù)庫(kù)中的數(shù)據(jù)。使用Promise異步回調(diào)。

說(shuō)明

當(dāng)前HarmonyOS設(shè)備均不支持此接口,應(yīng)用調(diào)用此接口將返回801錯(cuò)誤碼。

系統(tǒng)能力: SystemCapability.DistributedDataManager.RelationalStore.Core

參數(shù):

參數(shù)名

類型

必填

說(shuō)明

device

string

指定的遠(yuǎn)程設(shè)備的networkId。

table

string

指定的目標(biāo)表名。

predicates

RdbPredicates

RdbPredicates的實(shí)例對(duì)象,指定查詢的條件。

columns

Array<string>

表示要查詢的列。如果值為空,則查詢應(yīng)用于所有列。

返回值

類型

說(shuō)明

Promise<ResultSet>

Promise對(duì)象。如果操作成功,則返回ResultSet對(duì)象。

示例:

  1. let predicates = new relationalStore.RdbPredicates('EMPLOYEE');
  2. predicates.greaterThan("id", 0);
  3. let promise = store.remoteQuery("deviceId", "EMPLOYEE", predicates, ["ID", "NAME", "AGE", "SALARY", "CODES"]);
  4. promise.then((resultSet) => {
  5. console.info(`ResultSet column names: ${resultSet.columnNames}`);
  6. console.info(`ResultSet column count: ${resultSet.columnCount}`);
  7. }).catch((err) => {
  8. console.error(`Failed to remoteQuery, code is ${err.code},message is ${err.message}`);
  9. })

querySql

querySql(sql: string, bindArgs: Array<ValueType>, callback: AsyncCallback<ResultSet>):void

根據(jù)指定SQL語(yǔ)句查詢數(shù)據(jù)庫(kù)中的數(shù)據(jù),使用callback異步回調(diào)。

系統(tǒng)能力: SystemCapability.DistributedDataManager.RelationalStore.Core

參數(shù):

參數(shù)名

類型

必填

說(shuō)明

sql

string

指定要執(zhí)行的SQL語(yǔ)句。

bindArgs

Array<ValueType>

SQL語(yǔ)句中參數(shù)的值。該值與sql參數(shù)語(yǔ)句中的占位符相對(duì)應(yīng)。當(dāng)sql參數(shù)語(yǔ)句完整時(shí),該參數(shù)需為空數(shù)組。

callback

AsyncCallback<ResultSet>

指定callback回調(diào)函數(shù)。如果操作成功,則返回ResultSet對(duì)象。

示例:

  1. store.querySql("SELECT * FROM EMPLOYEE CROSS JOIN BOOK WHERE BOOK.NAME = ?", ['sanguo'], function (err, resultSet) {
  2. if (err) {
  3. console.error(`Query failed, code is ${err.code},message is ${err.message}`);
  4. return;
  5. }
  6. console.info(`ResultSet column names: ${resultSet.columnNames}`);
  7. console.info(`ResultSet column count: ${resultSet.columnCount}`);
  8. })

querySql

querySql(sql: string, bindArgs?: Array<ValueType>):Promise<ResultSet>

根據(jù)指定SQL語(yǔ)句查詢數(shù)據(jù)庫(kù)中的數(shù)據(jù),使用Promise異步回調(diào)。

系統(tǒng)能力: SystemCapability.DistributedDataManager.RelationalStore.Core

參數(shù):

參數(shù)名

類型

必填

說(shuō)明

sql

string

指定要執(zhí)行的SQL語(yǔ)句。

bindArgs

Array<ValueType>

SQL語(yǔ)句中參數(shù)的值。當(dāng)sql參數(shù)語(yǔ)句完整時(shí),該參數(shù)不填。

返回值

類型

說(shuō)明

Promise<ResultSet>

Promise對(duì)象。如果操作成功,則返回ResultSet對(duì)象。

示例:

  1. let promise = store.querySql("SELECT * FROM EMPLOYEE CROSS JOIN BOOK WHERE BOOK.NAME = 'sanguo'");
  2. promise.then((resultSet) => {
  3. console.info(`ResultSet column names: ${resultSet.columnNames}`);
  4. console.info(`ResultSet column count: ${resultSet.columnCount}`);
  5. }).catch((err) => {
  6. console.error(`Query failed, code is ${err.code},message is ${err.message}`);
  7. })

executeSql

executeSql(sql: string, bindArgs: Array<ValueType>, callback: AsyncCallback<void>):void

執(zhí)行包含指定參數(shù)但不返回值的SQL語(yǔ)句,使用callback異步回調(diào)。

系統(tǒng)能力: SystemCapability.DistributedDataManager.RelationalStore.Core

參數(shù):

參數(shù)名

類型

必填

說(shuō)明

sql

string

指定要執(zhí)行的SQL語(yǔ)句。

bindArgs

Array<ValueType>

SQL語(yǔ)句中參數(shù)的值。該值與sql參數(shù)語(yǔ)句中的占位符相對(duì)應(yīng)。當(dāng)sql參數(shù)語(yǔ)句完整時(shí),該參數(shù)需為空數(shù)組。

callback

AsyncCallback<void>

指定callback回調(diào)函數(shù)。

示例:

  1. const SQL_DELETE_TABLE = "DELETE FROM test WHERE name = ?"
  2. store.executeSql(SQL_DELETE_TABLE, ['zhangsan'], function(err) {
  3. if (err) {
  4. console.error(`ExecuteSql failed, code is ${err.code},message is ${err.message}`);
  5. return;
  6. }
  7. console.info(`Delete table done.`);
  8. })

executeSql

executeSql(sql: string, bindArgs?: Array<ValueType>):Promise<void>

執(zhí)行包含指定參數(shù)但不返回值的SQL語(yǔ)句,使用Promise異步回調(diào)。

系統(tǒng)能力: SystemCapability.DistributedDataManager.RelationalStore.Core

參數(shù):

參數(shù)名

類型

必填

說(shuō)明

sql

string

指定要執(zhí)行的SQL語(yǔ)句。

bindArgs

Array<ValueType>

SQL語(yǔ)句中參數(shù)的值。當(dāng)sql參數(shù)語(yǔ)句完整時(shí),該參數(shù)不填。

返回值

類型

說(shuō)明

Promise<void>

無(wú)返回結(jié)果的Promise對(duì)象。

示例:

  1. const SQL_DELETE_TABLE = "DELETE FROM test WHERE name = 'zhangsan'"
  2. let promise = store.executeSql(SQL_DELETE_TABLE);
  3. promise.then(() => {
  4. console.info(`Delete table done.`);
  5. }).catch((err) => {
  6. console.error(`ExecuteSql failed, code is ${err.code},message is ${err.message}`);
  7. })

beginTransaction

beginTransaction():void

在開始執(zhí)行SQL語(yǔ)句之前,開始事務(wù)。

系統(tǒng)能力: SystemCapability.DistributedDataManager.RelationalStore.Core

示例:

  1. import featureAbility from '@ohos.ability.featureAbility'
  2. let context = featureAbility.getContext();
  3. const STORE_CONFIG = {
  4. name: "RdbTest.db",
  5. securityLevel: relationalStore.SecurityLevel.S1
  6. };
  7. relationalStore.getRdbStore(context, STORE_CONFIG, async function (err, store) {
  8. if (err) {
  9. console.error(`GetRdbStore failed, code is ${err.code},message is ${err.message}`);
  10. return;
  11. }
  12. store.beginTransaction();
  13. const valueBucket = {
  14. "name": "lisi",
  15. "age": 18,
  16. "salary": 100.5,
  17. "blobType": new Uint8Array([1, 2, 3]),
  18. };
  19. await store.insert("test", valueBucket);
  20. store.commit();
  21. })

commit

commit():void

提交已執(zhí)行的SQL語(yǔ)句。

系統(tǒng)能力: SystemCapability.DistributedDataManager.RelationalStore.Core

示例:

  1. import featureAbility from '@ohos.ability.featureAbility'
  2. let context = featureAbility.getContext();
  3. const STORE_CONFIG = {
  4. name: "RdbTest.db",
  5. securityLevel: relationalStore.SecurityLevel.S1
  6. };
  7. relationalStore.getRdbStore(context, STORE_CONFIG, async function (err, store) {
  8. if (err) {
  9. console.error(`GetRdbStore failed, code is ${err.code},message is ${err.message}`);
  10. return;
  11. }
  12. store.beginTransaction();
  13. const valueBucket = {
  14. "name": "lisi",
  15. "age": 18,
  16. "salary": 100.5,
  17. "blobType": new Uint8Array([1, 2, 3]),
  18. };
  19. await store.insert("test", valueBucket);
  20. store.commit();
  21. })

rollBack

rollBack():void

回滾已經(jīng)執(zhí)行的SQL語(yǔ)句。

系統(tǒng)能力: SystemCapability.DistributedDataManager.RelationalStore.Core

示例:

  1. import featureAbility from '@ohos.ability.featureAbility'
  2. let context = featureAbility.getContext();
  3. const STORE_CONFIG = {
  4. name: "RdbTest.db",
  5. securityLevel: relationalStore.SecurityLevel.S1
  6. };
  7. relationalStore.getRdbStore(context, STORE_CONFIG, async function (err, store) {
  8. if (err) {
  9. console.error(`GetRdbStore failed, code is ${err.code},message is ${err.message}`);
  10. return;
  11. }
  12. try {
  13. store.beginTransaction()
  14. const valueBucket = {
  15. "id": 1,
  16. "name": "lisi",
  17. "age": 18,
  18. "salary": 100.5,
  19. "blobType": new Uint8Array([1, 2, 3]),
  20. };
  21. await store.insert("test", valueBucket);
  22. store.commit();
  23. } catch (err) {
  24. console.error(`Transaction failed, code is ${err.code},message is ${err.message}`);
  25. store.rollBack();
  26. }
  27. })

backup

backup(destName:string, callback: AsyncCallback<void>):void

以指定名稱備份數(shù)據(jù)庫(kù),使用callback異步回調(diào)。

系統(tǒng)能力: SystemCapability.DistributedDataManager.RelationalStore.Core

參數(shù):

參數(shù)名

類型

必填

說(shuō)明

destName

string

指定數(shù)據(jù)庫(kù)的備份文件名。

callback

AsyncCallback<void>

指定callback回調(diào)函數(shù)。

示例:

  1. store.backup("dbBackup.db", function(err) {
  2. if (err) {
  3. console.error(`Backup failed, code is ${err.code},message is ${err.message}`);
  4. return;
  5. }
  6. console.info(`Backup success.`);
  7. })

backup

backup(destName:string): Promise<void>

以指定名稱備份數(shù)據(jù)庫(kù),使用Promise異步回調(diào)。

系統(tǒng)能力: SystemCapability.DistributedDataManager.RelationalStore.Core

參數(shù):

參數(shù)名

類型

必填

說(shuō)明

destName

string

指定數(shù)據(jù)庫(kù)的備份文件名。

返回值

類型

說(shuō)明

Promise<void>

無(wú)返回結(jié)果的Promise對(duì)象。

示例:

  1. let promiseBackup = store.backup("dbBackup.db");
  2. promiseBackup.then(()=>{
  3. console.info(`Backup success.`);
  4. }).catch((err)=>{
  5. console.error(`Backup failed, code is ${err.code},message is ${err.message}`);
  6. })

restore

restore(srcName:string, callback: AsyncCallback<void>):void

從指定的數(shù)據(jù)庫(kù)備份文件恢復(fù)數(shù)據(jù)庫(kù),使用callback異步回調(diào)。

系統(tǒng)能力: SystemCapability.DistributedDataManager.RelationalStore.Core

參數(shù):

參數(shù)名

類型

必填

說(shuō)明

srcName

string

指定數(shù)據(jù)庫(kù)的備份文件名。

callback

AsyncCallback<void>

指定callback回調(diào)函數(shù)。

示例:

  1. store.restore("dbBackup.db", function(err) {
  2. if (err) {
  3. console.error(`Restore failed, code is ${err.code},message is ${err.message}`);
  4. return;
  5. }
  6. console.info(`Restore success.`);
  7. })

restore

restore(srcName:string): Promise<void>

從指定的數(shù)據(jù)庫(kù)備份文件恢復(fù)數(shù)據(jù)庫(kù),使用Promise異步回調(diào)。

系統(tǒng)能力: SystemCapability.DistributedDataManager.RelationalStore.Core

參數(shù):

參數(shù)名

類型

必填

說(shuō)明

srcName

string

指定數(shù)據(jù)庫(kù)的備份文件名。

返回值

類型

說(shuō)明

Promise<void>

無(wú)返回結(jié)果的Promise對(duì)象。

示例:

  1. let promiseRestore = store.restore("dbBackup.db");
  2. promiseRestore.then(()=>{
  3. console.info(`Restore success.`);
  4. }).catch((err)=>{
  5. console.error(`Restore failed, code is ${err.code},message is ${err.message}`);
  6. })

setDistributedTables

setDistributedTables(tables: Array<string>, callback: AsyncCallback<void>): void

設(shè)置分布式列表,使用callback異步回調(diào)。

說(shuō)明

當(dāng)前HarmonyOS設(shè)備均不支持此接口,應(yīng)用調(diào)用此接口將返回801錯(cuò)誤碼。

需要權(quán)限: ohos.permission.DISTRIBUTED_DATASYNC

系統(tǒng)能力: SystemCapability.DistributedDataManager.RelationalStore.Core

參數(shù):

參數(shù)名

類型

必填

說(shuō)明

tables

Array<string>

要設(shè)置的分布式列表表名

callback

AsyncCallback<void>

指定callback回調(diào)函數(shù)。

示例:

  1. store.setDistributedTables(["EMPLOYEE"], function (err) {
  2. if (err) {
  3. console.error(`SetDistributedTables failed, code is ${err.code},message is ${err.message}`);
  4. return;
  5. }
  6. console.info(`SetDistributedTables successfully.`);
  7. })

setDistributedTables

setDistributedTables(tables: Array<string>): Promise<void>

設(shè)置分布式列表,使用Promise異步回調(diào)。

說(shuō)明

當(dāng)前HarmonyOS設(shè)備均不支持此接口,應(yīng)用調(diào)用此接口將返回801錯(cuò)誤碼。

需要權(quán)限: ohos.permission.DISTRIBUTED_DATASYNC

系統(tǒng)能力: SystemCapability.DistributedDataManager.RelationalStore.Core

參數(shù):

參數(shù)名

類型

必填

說(shuō)明

tables

Array<string>

要設(shè)置的分布式列表表名。

返回值

類型

說(shuō)明

Promise<void>

無(wú)返回結(jié)果的Promise對(duì)象。

示例:

  1. let promise = store.setDistributedTables(["EMPLOYEE"]);
  2. promise.then(() => {
  3. console.info(`SetDistributedTables successfully.`);
  4. }).catch((err) => {
  5. console.error(`SetDistributedTables failed, code is ${err.code},message is ${err.message}`);
  6. })

obtainDistributedTableName

obtainDistributedTableName(device: string, table: string, callback: AsyncCallback<string>): void

根據(jù)本地表名獲取指定遠(yuǎn)程設(shè)備的分布式表名。在查詢遠(yuǎn)程設(shè)備數(shù)據(jù)庫(kù)時(shí),需要使用分布式表名, 使用callback異步回調(diào)。

說(shuō)明

當(dāng)前HarmonyOS設(shè)備均不支持此接口,應(yīng)用調(diào)用此接口將返回801錯(cuò)誤碼。

需要權(quán)限: ohos.permission.DISTRIBUTED_DATASYNC

系統(tǒng)能力: SystemCapability.DistributedDataManager.RelationalStore.Core

參數(shù):

參數(shù)名

類型

必填

說(shuō)明

device

string

遠(yuǎn)程設(shè)備 。

table

string

本地表名。

callback

AsyncCallback<string>

指定的callback回調(diào)函數(shù)。如果操作成功,返回遠(yuǎn)程設(shè)備的分布式表名。

示例:

  1. store.obtainDistributedTableName("12345678abcde", "EMPLOYEE", function (err, tableName) {
  2. if (err) {
  3. console.error(`ObtainDistributedTableName failed, code is ${err.code},message is ${err.message}`);
  4. return;
  5. }
  6. console.info(`ObtainDistributedTableName successfully, tableName= ${tableName}`);
  7. })

obtainDistributedTableName

obtainDistributedTableName(device: string, table: string): Promise<string>

根據(jù)本地表名獲取指定遠(yuǎn)程設(shè)備的分布式表名。在查詢遠(yuǎn)程設(shè)備數(shù)據(jù)庫(kù)時(shí),需要使用分布式表名,使用Promise異步回調(diào)。

說(shuō)明

當(dāng)前HarmonyOS設(shè)備均不支持此接口,應(yīng)用調(diào)用此接口將返回801錯(cuò)誤碼。

需要權(quán)限: ohos.permission.DISTRIBUTED_DATASYNC

系統(tǒng)能力: SystemCapability.DistributedDataManager.RelationalStore.Core

參數(shù):

參數(shù)名

類型

必填

說(shuō)明

device

string

遠(yuǎn)程設(shè)備。

table

string

本地表名。

返回值

類型

說(shuō)明

Promise<string>

Promise對(duì)象。如果操作成功,返回遠(yuǎn)程設(shè)備的分布式表名。

示例:

  1. let promise = store.obtainDistributedTableName("12345678abcde", "EMPLOYEE");
  2. promise.then((tableName) => {
  3. console.info(`ObtainDistributedTableName successfully, tableName= ${tableName}`);
  4. }).catch((err) => {
  5. console.error(`ObtainDistributedTableName failed, code is ${err.code},message is ${err.message}`);
  6. })

sync

sync(mode: SyncMode, predicates: RdbPredicates, callback: AsyncCallback<Array<[string, number]>>): void

在設(shè)備之間同步數(shù)據(jù), 使用callback異步回調(diào)。

說(shuō)明

當(dāng)前HarmonyOS設(shè)備均不支持此接口,應(yīng)用調(diào)用此接口將返回801錯(cuò)誤碼。

需要權(quán)限: ohos.permission.DISTRIBUTED_DATASYNC

系統(tǒng)能力: SystemCapability.DistributedDataManager.RelationalStore.Core

參數(shù):

參數(shù)名

類型

必填

說(shuō)明

mode

SyncMode

指同步模式。該值可以是推、拉。

predicates

RdbPredicates

約束同步數(shù)據(jù)和設(shè)備。

callback

AsyncCallback<Array<[string, number]>>

指定的callback回調(diào)函數(shù),用于向調(diào)用者發(fā)送同步結(jié)果。string:設(shè)備ID;number:每個(gè)設(shè)備同步狀態(tài),0表示成功,其他值表示失敗。

示例:

  1. let predicates = new relationalStore.RdbPredicates('EMPLOYEE');
  2. predicates.inDevices(['12345678abcde']);
  3. store.sync(relationalStore.SyncMode.SYNC_MODE_PUSH, predicates, function (err, result) {
  4. if (err) {
  5. console.error(`Sync failed, code is ${err.code},message is ${err.message}`);
  6. return;
  7. }
  8. console.info(`Sync done.`);
  9. for (let i = 0; i < result.length; i++) {
  10. console.info(`device= ${result[i][0]}, status= ${result[i][1]}`);
  11. }
  12. })

sync

sync(mode: SyncMode, predicates: RdbPredicates): Promise<Array<[string, number]>>

在設(shè)備之間同步數(shù)據(jù),使用Promise異步回調(diào)。

說(shuō)明

當(dāng)前HarmonyOS設(shè)備均不支持此接口,應(yīng)用調(diào)用此接口將返回801錯(cuò)誤碼。

需要權(quán)限: ohos.permission.DISTRIBUTED_DATASYNC

系統(tǒng)能力: SystemCapability.DistributedDataManager.RelationalStore.Core

參數(shù):

參數(shù)名

類型

必填

說(shuō)明

mode

SyncMode

指同步模式。該值可以是推、拉。

predicates

RdbPredicates

約束同步數(shù)據(jù)和設(shè)備。

返回值

類型

說(shuō)明

Promise<Array<[string, number]>>

Promise對(duì)象,用于向調(diào)用者發(fā)送同步結(jié)果。string:設(shè)備ID;number:每個(gè)設(shè)備同步狀態(tài),0表示成功,其他值表示失敗。

示例:

  1. let predicates = new relationalStore.RdbPredicates('EMPLOYEE');
  2. predicates.inDevices(['12345678abcde']);
  3. let promise = store.sync(relationalStore.SyncMode.SYNC_MODE_PUSH, predicates);
  4. promise.then((result) =>{
  5. console.info(`Sync done.`);
  6. for (let i = 0; i < result.length; i++) {
  7. console.info(`device= ${result[i][0]}, status= ${result[i][1]}`);
  8. }
  9. }).catch((err) => {
  10. console.error(`Sync failed, code is ${err.code},message is ${err.message}`);
  11. })

on('dataChange')

on(event: 'dataChange', type: SubscribeType, observer: Callback<Array<string>>): void

注冊(cè)數(shù)據(jù)庫(kù)的觀察者。當(dāng)分布式數(shù)據(jù)庫(kù)中的數(shù)據(jù)發(fā)生更改時(shí),將調(diào)用回調(diào)。

說(shuō)明

當(dāng)前HarmonyOS設(shè)備均不支持此接口,應(yīng)用調(diào)用此接口將返回801錯(cuò)誤碼。

系統(tǒng)能力: SystemCapability.DistributedDataManager.RelationalStore.Core

參數(shù):

參數(shù)名

類型

必填

說(shuō)明

event

string

取值為'dataChange',表示數(shù)據(jù)更改。

type

SubscribeType

訂閱類型。

observer

Callback<Array<string>>

指分布式數(shù)據(jù)庫(kù)中數(shù)據(jù)更改事件的觀察者。Array<string>為數(shù)據(jù)庫(kù)中的數(shù)據(jù)發(fā)生改變的對(duì)端設(shè)備ID。

示例:

  1. function storeObserver(devices) {
  2. for (let i = 0; i < devices.length; i++) {
  3. console.info(`device= ${devices[i]} data changed`);
  4. }
  5. }
  6. try {
  7. store.on('dataChange', relationalStore.SubscribeType.SUBSCRIBE_TYPE_REMOTE, storeObserver);
  8. } catch (err) {
  9. console.error(`Register observer failed, code is ${err.code},message is ${err.message}`);
  10. }

off('dataChange')

off(event:'dataChange', type: SubscribeType, observer: Callback<Array<string>>): void

從數(shù)據(jù)庫(kù)中刪除指定類型的指定觀察者。

說(shuō)明

當(dāng)前HarmonyOS設(shè)備均不支持此接口,應(yīng)用調(diào)用此接口將返回801錯(cuò)誤碼。

系統(tǒng)能力: SystemCapability.DistributedDataManager.RelationalStore.Core

參數(shù):

參數(shù)名

類型

必填

說(shuō)明

event

string

取值為'dataChange',表示數(shù)據(jù)更改。

type

SubscribeType

訂閱類型。

observer

Callback<Array<string>>

指已注冊(cè)的數(shù)據(jù)更改觀察者。Array<string>為數(shù)據(jù)庫(kù)中的數(shù)據(jù)發(fā)生改變的對(duì)端設(shè)備ID。

示例:

  1. function storeObserver(devices) {
  2. for (let i = 0; i < devices.length; i++) {
  3. console.info(`device= ${devices[i]} data changed`);
  4. }
  5. }
  6. try {
  7. store.off('dataChange', relationalStore.SubscribeType.SUBSCRIBE_TYPE_REMOTE, storeObserver);
  8. } catch (err) {
  9. console.error(`Unregister observer failed, code is ${err.code},message is ${err.message}`);
  10. }

ResultSet

提供通過(guò)查詢數(shù)據(jù)庫(kù)生成的數(shù)據(jù)庫(kù)結(jié)果集的訪問(wèn)方法。結(jié)果集是指用戶調(diào)用關(guān)系型數(shù)據(jù)庫(kù)查詢接口之后返回的結(jié)果集合,提供了多種靈活的數(shù)據(jù)訪問(wèn)方式,以便用戶獲取各項(xiàng)數(shù)據(jù)。

使用說(shuō)明

首先需要獲取resultSet對(duì)象。

  1. let resultSet = null;
  2. let predicates = new relationalStore.RdbPredicates("EMPLOYEE");
  3. predicates.equalTo("AGE", 18);
  4. let promise = store.query(predicates, ["ID", "NAME", "AGE", "SALARY", "CODES"]);
  5. promise.then((result) => {
  6. resultSet = result;
  7. console.info(`resultSet columnNames: ${resultSet.columnNames}`);
  8. console.info(`resultSet columnCount: ${resultSet.columnCount}`);
  9. });

屬性

系統(tǒng)能力: SystemCapability.DistributedDataManager.RelationalStore.Core

名稱

類型

必填

說(shuō)明

columnNames

Array<string>

獲取結(jié)果集中所有列的名稱。

columnCount

number

獲取結(jié)果集中的列數(shù)。

rowCount

number

獲取結(jié)果集中的行數(shù)。

rowIndex

number

獲取結(jié)果集當(dāng)前行的索引。

isAtFirstRow

boolean

檢查結(jié)果集是否位于第一行。

isAtLastRow

boolean

檢查結(jié)果集是否位于最后一行。

isEnded

boolean

檢查結(jié)果集是否位于最后一行之后。

isStarted

boolean

檢查指針是否移動(dòng)過(guò)。

isClosed

boolean

檢查當(dāng)前結(jié)果集是否關(guān)閉。

getColumnIndex

getColumnIndex(columnName: string): number

根據(jù)指定的列名獲取列索引。

系統(tǒng)能力: SystemCapability.DistributedDataManager.RelationalStore.Core

參數(shù):

參數(shù)名

類型

必填

說(shuō)明

columnName

string

表示結(jié)果集中指定列的名稱。

返回值:

類型

說(shuō)明

number

返回指定列的索引。

錯(cuò)誤碼:

以下錯(cuò)誤碼的詳細(xì)介紹請(qǐng)參見(jiàn)關(guān)系型數(shù)據(jù)庫(kù)錯(cuò)誤碼。

錯(cuò)誤碼ID

錯(cuò)誤信息

14800013

The column value is null or the column type is incompatible.

示例:

  1. resultSet.goToFirstRow();
  2. const id = resultSet.getLong(resultSet.getColumnIndex("ID"));
  3. const name = resultSet.getString(resultSet.getColumnIndex("NAME"));
  4. const age = resultSet.getLong(resultSet.getColumnIndex("AGE"));
  5. const salary = resultSet.getDouble(resultSet.getColumnIndex("SALARY"));

getColumnName

getColumnName(columnIndex: number): string

根據(jù)指定的列索引獲取列名。

系統(tǒng)能力: SystemCapability.DistributedDataManager.RelationalStore.Core

參數(shù):

參數(shù)名

類型

必填

說(shuō)明

columnIndex

number

表示結(jié)果集中指定列的索引。

返回值:

類型

說(shuō)明

string

返回指定列的名稱。

錯(cuò)誤碼:

以下錯(cuò)誤碼的詳細(xì)介紹請(qǐng)參見(jiàn)關(guān)系型數(shù)據(jù)庫(kù)錯(cuò)誤碼

錯(cuò)誤碼ID

錯(cuò)誤信息

14800013

The column value is null or the column type is incompatible.

示例:

  1. const id = resultSet.getColumnName(0);
  2. const name = resultSet.getColumnName(1);
  3. const age = resultSet.getColumnName(2);

goTo

goTo(offset:number): boolean

向前或向后轉(zhuǎn)至結(jié)果集的指定行,相對(duì)于其當(dāng)前位置偏移。

系統(tǒng)能力: SystemCapability.DistributedDataManager.RelationalStore.Core

參數(shù):

參數(shù)名

類型

必填

說(shuō)明

offset

number

表示相對(duì)于當(dāng)前位置的偏移量。

返回值:

類型

說(shuō)明

boolean

如果成功移動(dòng)結(jié)果集,則為true;否則返回false。

錯(cuò)誤碼:

以下錯(cuò)誤碼的詳細(xì)介紹請(qǐng)參見(jiàn)關(guān)系型數(shù)據(jù)庫(kù)錯(cuò)誤碼

錯(cuò)誤碼ID

錯(cuò)誤信息

14800012

The result set is empty or the specified location is invalid.

示例:

  1. let predicates = new relationalStore.RdbPredicates("EMPLOYEE");
  2. let promise= store.query(predicates, ["ID", "NAME", "AGE", "SALARY", "CODES"]);
  3. promise.then((resultSet) => {
  4. resultSet.goTo(1);
  5. resultSet.close();
  6. }).catch((err) => {
  7. console.error(`query failed, code is ${err.code},message is ${err.message}`);
  8. });

goToRow

goToRow(position: number): boolean

轉(zhuǎn)到結(jié)果集的指定行。

系統(tǒng)能力: SystemCapability.DistributedDataManager.RelationalStore.Core

參數(shù):

參數(shù)名

類型

必填

說(shuō)明

position

number

表示要移動(dòng)到的指定位置。

返回值:

類型

說(shuō)明

boolean

如果成功移動(dòng)結(jié)果集,則為true;否則返回false。

錯(cuò)誤碼:

以下錯(cuò)誤碼的詳細(xì)介紹請(qǐng)參見(jiàn)關(guān)系型數(shù)據(jù)庫(kù)錯(cuò)誤碼

錯(cuò)誤碼ID

錯(cuò)誤信息

14800012

The result set is empty or the specified location is invalid.

示例:

  1. let predicates = new relationalStore.RdbPredicates("EMPLOYEE");
  2. let promise = store.query(predicates, ["ID", "NAME", "AGE", "SALARY", "CODES"]);
  3. promise.then((resultSet) => {
  4. resultSet.goToRow(5);
  5. resultSet.close();
  6. }).catch((err) => {
  7. console.error(`query failed, code is ${err.code},message is ${err.message}`);
  8. });

goToFirstRow

goToFirstRow(): boolean

轉(zhuǎn)到結(jié)果集的第一行。

系統(tǒng)能力: SystemCapability.DistributedDataManager.RelationalStore.Core

返回值:

類型

說(shuō)明

boolean

如果成功移動(dòng)結(jié)果集,則為true;否則返回false。

錯(cuò)誤碼:

以下錯(cuò)誤碼的詳細(xì)介紹請(qǐng)參見(jiàn)關(guān)系型數(shù)據(jù)庫(kù)錯(cuò)誤碼。

錯(cuò)誤碼ID

錯(cuò)誤信息

14800012

The result set is empty or the specified location is invalid.

示例:

  1. let predicates = new relationalStore.RdbPredicates("EMPLOYEE");
  2. let promise = store.query(predicates, ["ID", "NAME", "AGE", "SALARY", "CODES"]);
  3. promise.then((resultSet) => {
  4. resultSet.goToFirstRow();
  5. resultSet.close();
  6. }).catch((err) => {
  7. console.error(`query failed, code is ${err.code},message is ${err.message}`);
  8. });

goToLastRow

goToLastRow(): boolean

轉(zhuǎn)到結(jié)果集的最后一行。

系統(tǒng)能力: SystemCapability.DistributedDataManager.RelationalStore.Core

返回值:

類型

說(shuō)明

boolean

如果成功移動(dòng)結(jié)果集,則為true;否則返回false。

錯(cuò)誤碼:

以下錯(cuò)誤碼的詳細(xì)介紹請(qǐng)參見(jiàn)關(guān)系型數(shù)據(jù)庫(kù)錯(cuò)誤碼。

錯(cuò)誤碼ID

錯(cuò)誤信息

14800012

The result set is empty or the specified location is invalid.

示例:

  1. let predicates = new relationalStore.RdbPredicates("EMPLOYEE");
  2. let promise = store.query(predicates, ["ID", "NAME", "AGE", "SALARY", "CODES"]);
  3. promise.then((resultSet) => {
  4. resultSet.goToLastRow();
  5. resultSet.close();
  6. }).catch((err) => {
  7. console.error(`query failed, code is ${err.code},message is ${err.message}`);
  8. });

goToNextRow

goToNextRow(): boolean

轉(zhuǎn)到結(jié)果集的下一行。

系統(tǒng)能力: SystemCapability.DistributedDataManager.RelationalStore.Core

返回值:

類型

說(shuō)明

boolean

如果成功移動(dòng)結(jié)果集,則為true;否則返回false。

錯(cuò)誤碼:

以下錯(cuò)誤碼的詳細(xì)介紹請(qǐng)參見(jiàn)關(guān)系型數(shù)據(jù)庫(kù)錯(cuò)誤碼。

錯(cuò)誤碼ID

錯(cuò)誤信息

14800012

The result set is empty or the specified location is invalid.

示例:

  1. let predicates = new relationalStore.RdbPredicates("EMPLOYEE");
  2. let promise = store.query(predicates, ["ID", "NAME", "AGE", "SALARY", "CODES"]);
  3. promise.then((resultSet) => {
  4. resultSet.goToNextRow();
  5. resultSet.close();
  6. }).catch((err) => {
  7. console.error(`query failed, code is ${err.code},message is ${err.message}`);
  8. });

goToPreviousRow

goToPreviousRow(): boolean

轉(zhuǎn)到結(jié)果集的上一行。

系統(tǒng)能力: SystemCapability.DistributedDataManager.RelationalStore.Core

返回值:

類型

說(shuō)明

boolean

如果成功移動(dòng)結(jié)果集,則為true;否則返回false。

錯(cuò)誤碼:

以下錯(cuò)誤碼的詳細(xì)介紹請(qǐng)參見(jiàn)關(guān)系型數(shù)據(jù)庫(kù)錯(cuò)誤碼。

錯(cuò)誤碼ID

錯(cuò)誤信息

14800012

The result set is empty or the specified location is invalid.

示例:

  1. let predicates = new relationalStore.RdbPredicates("EMPLOYEE");
  2. let promise = store.query(predicates, ["ID", "NAME", "AGE", "SALARY", "CODES"]);
  3. promise.then((resultSet) => {
  4. resultSet.goToPreviousRow();
  5. resultSet.close();
  6. }).catch((err) => {
  7. console.error(`query failed, code is ${err.code},message is ${err.message}`);
  8. });

getBlob

getBlob(columnIndex: number): Uint8Array

以字節(jié)數(shù)組的形式獲取當(dāng)前行中指定列的值。

系統(tǒng)能力: SystemCapability.DistributedDataManager.RelationalStore.Core

參數(shù):

參數(shù)名

類型

必填

說(shuō)明

columnIndex

number

指定的列索引,從0開始。

返回值:

類型

說(shuō)明

Uint8Array

以字節(jié)數(shù)組的形式返回指定列的值。

錯(cuò)誤碼:

以下錯(cuò)誤碼的詳細(xì)介紹請(qǐng)參見(jiàn)關(guān)系型數(shù)據(jù)庫(kù)錯(cuò)誤碼。

錯(cuò)誤碼ID

錯(cuò)誤信息

14800013

The column value is null or the column type is incompatible.

示例:

  1. const codes = resultSet.getBlob(resultSet.getColumnIndex("CODES"));

getString

getString(columnIndex: number): string

以字符串形式獲取當(dāng)前行中指定列的值。

系統(tǒng)能力: SystemCapability.DistributedDataManager.RelationalStore.Core

參數(shù):

參數(shù)名

類型

必填

說(shuō)明

columnIndex

number

指定的列索引,從0開始。

返回值:

類型

說(shuō)明

string

以字符串形式返回指定列的值。

錯(cuò)誤碼:

以下錯(cuò)誤碼的詳細(xì)介紹請(qǐng)參見(jiàn)關(guān)系型數(shù)據(jù)庫(kù)錯(cuò)誤碼。

錯(cuò)誤碼ID

錯(cuò)誤信息

14800013

The column value is null or the column type is incompatible.

示例:

  1. const name = resultSet.getString(resultSet.getColumnIndex("NAME"));

getLong

getLong(columnIndex: number): number

以Long形式獲取當(dāng)前行中指定列的值。

系統(tǒng)能力: SystemCapability.DistributedDataManager.RelationalStore.Core

參數(shù):

參數(shù)名

類型

必填

說(shuō)明

columnIndex

number

指定的列索引,從0開始。

返回值:

類型

說(shuō)明

number

以Long形式返回指定列的值。

該接口支持的數(shù)據(jù)范圍是:Number.MIN_SAFE_INTEGER ~ Number.MAX_SAFE_INTEGER,若超出該范圍,建議使用getDouble。

錯(cuò)誤碼:

以下錯(cuò)誤碼的詳細(xì)介紹請(qǐng)參見(jiàn)關(guān)系型數(shù)據(jù)庫(kù)錯(cuò)誤碼。

錯(cuò)誤碼ID

錯(cuò)誤信息

14800013

The column value is null or the column type is incompatible.

示例:

  1. const age = resultSet.getLong(resultSet.getColumnIndex("AGE"));

getDouble

getDouble(columnIndex: number): number

以double形式獲取當(dāng)前行中指定列的值。

系統(tǒng)能力: SystemCapability.DistributedDataManager.RelationalStore.Core

參數(shù):

參數(shù)名

類型

必填

說(shuō)明

columnIndex

number

指定的列索引,從0開始。

返回值:

類型

說(shuō)明

number

以double形式返回指定列的值。

錯(cuò)誤碼:

以下錯(cuò)誤碼的詳細(xì)介紹請(qǐng)參見(jiàn)關(guān)系型數(shù)據(jù)庫(kù)錯(cuò)誤碼。

錯(cuò)誤碼ID

錯(cuò)誤信息

14800013

The column value is null or the column type is incompatible.

示例:

  1. const salary = resultSet.getDouble(resultSet.getColumnIndex("SALARY"));

isColumnNull

isColumnNull(columnIndex: number): boolean

檢查當(dāng)前行中指定列的值是否為null。

系統(tǒng)能力: SystemCapability.DistributedDataManager.RelationalStore.Core

參數(shù):

參數(shù)名

類型

必填

說(shuō)明

columnIndex

number

指定的列索引,從0開始。

返回值:

類型

說(shuō)明

boolean

如果當(dāng)前行中指定列的值為null,則返回true,否則返回false。

錯(cuò)誤碼:

以下錯(cuò)誤碼的詳細(xì)介紹請(qǐng)參見(jiàn)關(guān)系型數(shù)據(jù)庫(kù)錯(cuò)誤碼。

錯(cuò)誤碼ID

錯(cuò)誤信息

14800013

The column value is null or the column type is incompatible.

示例:

  1. const isColumnNull = resultSet.isColumnNull(resultSet.getColumnIndex("CODES"));

close

close(): void

關(guān)閉結(jié)果集。

系統(tǒng)能力: SystemCapability.DistributedDataManager.RelationalStore.Core

示例:

  1. let predicatesClose = new relationalStore.RdbPredicates("EMPLOYEE");
  2. let promiseClose = store.query(predicatesClose, ["ID", "NAME", "AGE", "SALARY", "CODES"]);
  3. promiseClose.then((resultSet) => {
  4. resultSet.close();
  5. }).catch((err) => {
  6. console.error(`resultset close failed, code is ${err.code},message is ${err.message}`);
  7. });

錯(cuò)誤碼:

以下錯(cuò)誤碼的詳細(xì)介紹請(qǐng)參見(jiàn)關(guān)系型數(shù)據(jù)庫(kù)錯(cuò)誤碼。

錯(cuò)誤碼ID

錯(cuò)誤信息

14800012

The result set is empty or the specified location is invalid.

以上內(nèi)容是否對(duì)您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號(hào)
微信公眾號(hào)

編程獅公眾號(hào)