@ohos.data.dataSharePredicates (DataShare Predicates)

You can use DataSharePredicates to specify conditions for updating, deleting, and querying data when DataShare is used to manage data.

The APIs provided by DataSharePredicates correspond to the filter criteria of the database. Before using the APIs, you need to have basic database knowledge.

NOTE

  • The initial APIs of this module are supported since API version 9. Newly added APIs will be marked with a superscript to indicate their earliest API version.

  • The APIs of this module can be used only in the stage model.

Modules to Import

import dataSharePredicates from '@ohos.data.dataSharePredicates';

DataSharePredicates

Provides methods for setting different DataSharePredicates objects. This type is not multi-thread safe. If a DataSharePredicates instance is operated by multiple threads at the same time in an application, use a lock for the instance.

equalTo10+

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

Sets a DataSharePredicates object to match the data that is equal to the specified value.

Currently, only the relational database (RDB) and key-value database (KVDB, schema) support this DataSharePredicates object.

System capability: SystemCapability.DistributedDataManager.DataShare.Core

Parameters

Name Type Mandatory Description
field string Yes Column name in the database table.
value ValueType Yes Value to match.

Return value

Type Description
DataSharePredicates DataSharePredicates object created.

Example

let predicates = new dataSharePredicates.DataSharePredicates()
predicates.equalTo("NAME", "Rose")

and10+

and(): DataSharePredicates

Adds the AND condition to this DataSharePredicates object.

Currently, only the RDB and KVDB (schema) support this DataSharePredicates object.

System capability: SystemCapability.DistributedDataManager.DataShare.Core

Return value

Type Description
DataSharePredicates DataSharePredicates object with the AND condition.

Example

let predicates = new dataSharePredicates.DataSharePredicates()
predicates.equalTo("NAME", "lisi")
    .and()
    .equalTo("SALARY", 200.5)

orderByAsc10+

orderByAsc(field: string): DataSharePredicates

Sets a DataSharePredicates object that sorts data in ascending order.

Currently, only the RDB and KVDB (schema) support this DataSharePredicates object.

System capability: SystemCapability.DistributedDataManager.DataShare.Core

Parameters

Name Type Mandatory Description
field string Yes Column name in the database table.

Return value

Type Description
DataSharePredicates DataSharePredicates object created.

Example

let predicates = new dataSharePredicates.DataSharePredicates()
predicates.orderByAsc("AGE")

orderByDesc10+

orderByDesc(field: string): DataSharePredicates

Sets a DataSharePredicates object that sorts data in descending order.

Currently, only the RDB and KVDB (schema) support this DataSharePredicates object.

System capability: SystemCapability.DistributedDataManager.DataShare.Core

Parameters

Name Type Mandatory Description
field string Yes Column name in the database table.

Return value

Type Description
DataSharePredicates DataSharePredicates object created.

Example

let predicates = new dataSharePredicates.DataSharePredicates()
predicates.orderByDesc("AGE")

limit10+

limit(total: number, offset: number): DataSharePredicates

Sets a DataSharePredicates object to specify the number of results and the start position.

Currently, only the RDB and KVDB (schema) support this DataSharePredicates object.

System capability: SystemCapability.DistributedDataManager.DataShare.Core

Parameters

Name Type Mandatory Description
total number Yes Number of results.
offset number Yes Start position.

Return value

Type Description
DataSharePredicates DataSharePredicates object created.

Example

let predicates = new dataSharePredicates.DataSharePredicates()
predicates.equalTo("NAME", "Rose").limit(10, 3)

in10+

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

Sets a DataSharePredicates object to match the data that is within the specified value.

Currently, only the RDB and KVDB (schema) support this DataSharePredicates object.

System capability: SystemCapability.DistributedDataManager.DataShare.Core

Parameters

Name Type Mandatory Description
field string Yes Column name in the database table.
value Array<ValueType> Yes Array of the values to match.

Return value

Type Description
DataSharePredicates DataSharePredicates object created.

Example

let predicates = new dataSharePredicates.DataSharePredicates()
predicates.in("AGE", [18, 20])