public interface RdbStore
This class provides methods for creating, querying, updating, and deleting RDBs.
SystemCapability.Data#DATA_APPDATAMGR| 限定符和类型 | 接口 | 说明 |
|---|---|---|
static class |
RdbStore.ConflictResolution |
Indicates the type of the method for resolving conflict.
|
| 限定符和类型 | 字段 | 说明 |
|---|---|---|
static int |
MAX_BATCH_INSERT_SIZE |
| 限定符和类型 | 方法 | 说明 |
|---|---|---|
void |
addAttach(String alias,
String name,
byte[] encryptKey) |
Attaches a database.
|
boolean |
backup(String destName) |
Backs up a database in a specified name.
|
boolean |
backup(String destName,
byte[] destEncryptKey) |
Backs up a database in a specified name using a specified encryption key.
|
List<Long> |
batchInsertOrThrowException(String tableName,
List<ValuesBucket> initialValues,
RdbStore.ConflictResolution conflictResolution) |
Inserts data into the database in batches based on the conflict handling policy.
|
void |
beginTransaction() |
Begins a transaction in EXCLUSIVE mode.
|
void |
beginTransactionWithObserver(TransactionObserver transactionObserver) |
Begins a transaction in EXCLUSIVE mode with a listener.
|
Statement |
buildStatement(String sql) |
Compiles an SQL statement into a reusable statement.
|
void |
changeEncryptKey(byte[] newEncryptKey) |
Changes the encryption key of an encrypted database that is opened.
|
boolean |
checkIntegrity() |
Checks integrity of the primary and attached databases.
|
void |
close() |
Closes an RDB.
|
void |
configLocale(Locale locale) |
Sets the database locale.
|
long |
count(String tableName,
String whereClause,
String[] whereArgs) |
Queries the number of rows that meet specified conditions in the RDB store.
|
long |
count(AbsRdbPredicates absRdbPredicates) |
Obtains the number of rows in which data meets the specified instance object of
AbsRdbPredicates. |
int |
delete(AbsRdbPredicates absRdbPredicates) |
Deletes data from the database based on a specified instance object of
AbsRdbPredicates. |
void |
endTransaction() |
Ends a transaction.
|
void |
executeSql(String sql) |
Executes an SQL statement that returns no value.
|
void |
executeSql(String sql,
Object[] sqlArgs) |
Executes an SQL statement that contains specified parameters but returns no value.
|
String |
getPath() |
Obtains the path of the database file.
|
int |
getVersion() |
Obtains the database version.
|
void |
giveConnectionTemporarily(long milliseconds) |
Disconnects from the database temporarily to allow other threads to use the database.
|
long |
insert(String table,
ValuesBucket initialValues) |
Inserts a row of data into the target table.
|
long |
insertOrThrowException(String table,
ValuesBucket initialValues) |
Inserts a row of data into the target table.
|
long |
insertWithConflictResolution(String table,
ValuesBucket initialValues,
RdbStore.ConflictResolution conflictResolution) |
Inserts a row of data into the target table and specifies the method for resolving conflict.
|
boolean |
isHoldingConnection() |
Checks whether this thread holds a database connection.
|
boolean |
isInTransaction() |
Checks whether the current thread is engaged in a transaction.
|
boolean |
isMemoryRdb() |
Checks whether the database is opened in the memory.
|
boolean |
isOpen() |
Checks whether the database is opened.
|
boolean |
isReadOnly() |
Checks whether the database is opened in read-only mode.
|
List<Pair<String,String>> |
listAttached() |
Queries the list of attached databases.
|
void |
markAsCommit() |
Marks the current transaction as commit.
|
ResultSet |
query(AbsRdbPredicates absRdbPredicates,
String[] columns) |
Queries data in the database based on specified conditions.
|
ResultSet |
queryByStep(AbsRdbPredicates absRdbPredicates,
String[] columns) |
Queries data in the database based on specified conditions.
|
ResultSet |
querySql(String sql,
String[] sqlArgs) |
Executes an SQL statement.
|
ResultSet |
querySqlByStep(String sql,
String[] sqlArgs) |
Executes an SQL statement and specifies the result set.
|
ResultSet |
querySqlWithHook(String sql,
String[] sqlArgs,
ResultSetHook resultSetHook) |
Executes an SQL statement and specifies the result set.
|
ResultSet |
queryWithHook(AbsRdbPredicates absRdbPredicates,
String[] columns,
ResultSetHook resultSetHook) |
Queries data in the database based on specified conditions and specifies the result set.
|
long |
replace(String table,
ValuesBucket initialValues) |
Replaces a row of data in the target table.
|
long |
replaceOrThrowException(String table,
ValuesBucket initialValues) |
Replaces a row of data in the target table.
|
boolean |
restore(String srcName) |
Restores a database from a specified unencrypted database file.
|
boolean |
restore(String srcName,
byte[] srcEncryptKey,
byte[] destEncryptKey) |
Restores a database from a specified encrypted or unencrypted database file.
|
void |
setVersion(int version) |
Sets the version of a new database.
|
String |
toString() |
Obtains the description of the database store.
|
int |
update(ValuesBucket values,
AbsRdbPredicates absRdbPredicates) |
Updates data in the database based on a a specified instance object of
AbsRdbPredicates. |
int |
updateWithConflictResolution(ValuesBucket values,
AbsRdbPredicates absRdbPredicates,
RdbStore.ConflictResolution conflictResolution) |
Updates a row of data in the target table and specifies the method for resolving conflict.
|
static final int MAX_BATCH_INSERT_SIZE
long insert(String table, ValuesBucket initialValues)
table - Indicates the target table.initialValues - Indicates the row of data to be inserted into the table. The data comprises key-value pairs
stored in ValuesBucket. The key-value pairs are associated with column names of the
table.-1 otherwise.long insertOrThrowException(String table, ValuesBucket initialValues)
table - Indicates the target table.initialValues - Indicates the row of data to be inserted into the table. The data comprises key-value pairs
stored in ValuesBucket. The key-value pairs are associated with column names of the
table.-1 otherwise.RdbException - Throws this exception if an SQLite database error occurs.
You must handle this exception by yourself.List<Long> batchInsertOrThrowException(String tableName, List<ValuesBucket> initialValues, RdbStore.ConflictResolution conflictResolution)
If the conflict handling policy is null, the default policy ConflictResolution#ON_CONFLICT_NONE
will be used. If an exception occurs during the data insertion, the operation varies depending on the type of
the conflict handling policy.
ConflictResolution#ON_CONFLICT_ROLLBACK: If a data conflict occurs, the database column is null,
or the database column has only one value, all the inserted data will be rolled back.
ConflictResolution#ON_CONFLICT_ABORT or ConflictResolution#ON_CONFLICT_FAIL:
If a data conflict occurs, the database column is null, or the database column has only one value, the inserted
data will be retained, and an exception will be thrown.
ConflictResolution#ON_CONFLICT_IGNORE: If a data conflict occurs or the database column is null,
the exception will be ignored, and value -1 will be returned. The data insertion will continue until
all data is inserted into the database. If the foreign key does not exist or a foreign key conflict occurs,
see ConflictResolution#ON_CONFLICT_ABORT.
ConflictResolution#ON_CONFLICT_REPLACE: If a data conflict occurs, or the database column has a
default value or only one value, the conflict data will be replaced and the data insertion will continue until
all data is inserted into the database. If the database column has no default value or a foreign key conflict
occurs, see ConflictResolution#ON_CONFLICT_ABORT.
tableName - Indicates the table name of the database.initialValues - Indicates the data to be inserted into the database.conflictResolution - Indicates the conflict handling policy.ConflictResolution#ON_CONFLICT_IGNORE policy is used, the returned list
may contain -1.IllegalArgumentException - Throws this exception if the table name is null or an empty string,
initialValues is null or an empty list, a value in initialValues is null or an empty string,
or the number of values in initialValues exceeds RdbStore#MAX_BATCH_INSERT_SIZE.RdbException - Throws this exception if the data fails to be inserted into the database.long replace(String table, ValuesBucket initialValues)
If the target row does not exist, this method inserts a row.
table - Indicates the target table.initialValues - Indicates the row of data used to replace the row of data in the target table. The data
comprises key-value pairs stored in ValuesBucket. The key-value pairs are associated
with column names of the table.-1 otherwise.long replaceOrThrowException(String table, ValuesBucket initialValues)
If the target row does not exist, this method inserts a row.
table - Indicates the target table.initialValues - Indicates the row of data used to replace the row of data in the target table. The data
comprises key-value pairs stored in ValuesBucket. The key-value pairs are associated
with column names of the table.-1 otherwise.RdbException - Throws this exception if an SQLite database error occurs.
You must handle this exception by yourself.long insertWithConflictResolution(String table, ValuesBucket initialValues, RdbStore.ConflictResolution conflictResolution)
table - Indicates the target table.initialValues - Indicates the row of data to be inserted into the table. The data comprises key-value pairs
stored in ValuesBucket. The key-value pairs are associated with column names of the
table.conflictResolution - Indicates the method for resolving conflict.-1 otherwise.int delete(AbsRdbPredicates absRdbPredicates)
AbsRdbPredicates.
You can pass an instance object of AbsRdbPredicates such as RdbPredicates and
RawRdbPredicates to specify the delete condition. If you pass the RdbPredicates object,
you can call methods such as RdbPredicates#equalTo to set the deletion condition. If you pass
the RawRdbPredicates object, you can only set the database tableName, whereClause
and whereArgs. whereArgs is the value of a placeholder in the whereClause and must
be the same as the sequence of the placeholder.
absRdbPredicates - Indicates the specified delete condition by the instance object of
AbsRdbPredicates.int update(ValuesBucket values, AbsRdbPredicates absRdbPredicates)
AbsRdbPredicates.
You can pass an instance object of AbsRdbPredicates such as RdbPredicates and
RawRdbPredicates to specify the update condition. If you pass the RdbPredicates object,
you can call methods such as RdbPredicates#equalTo to set the update condition. If you pass the
RawRdbPredicates object, you can only set the database tableName, whereClause and
whereArgs. whereArgs is the value of a placeholder in the whereClause and must be
the same as the sequence of the placeholder.
values - Indicates the row of data to be updated in the database.
The data comprises key-value pairs stored in ValuesBucket.
The key-value pairs are associated with column names of the database table.absRdbPredicates - Indicates the specified update condition by the instance object of
AbsRdbPredicates.int updateWithConflictResolution(ValuesBucket values, AbsRdbPredicates absRdbPredicates, RdbStore.ConflictResolution conflictResolution)
values - Indicates the row of data to be inserted into the table. The data comprises key-value pairs stored
in ValuesBucket. The key-value pairs are associated with column names of the table.absRdbPredicates - Indicates the specified update condition by the instance object of
RdbPredicates.conflictResolution - Indicates the method for resolving conflict.ResultSet query(AbsRdbPredicates absRdbPredicates, String[] columns)
absRdbPredicates - Indicates the query conditions.columns - Indicates the columns to query. If the value is null, the query applies to all columns.ResultSet object if the operation is successful;ResultSet queryWithHook(AbsRdbPredicates absRdbPredicates, String[] columns, ResultSetHook resultSetHook)
absRdbPredicates - Indicates the query conditions.resultSetHook - Indicates the result set factory to use.columns - Indicates the columns to query. If the value is null, the query applies to all columns.ResultSet object if the operation is successful;ResultSet querySql(String sql, String[] sqlArgs)
sql - Indicates the SQL statement to execute.sqlArgs - Indicates the values of the parameters in the SQL statement. The values are strings.ResultSet object if the operation is successful;ResultSet querySqlWithHook(String sql, String[] sqlArgs, ResultSetHook resultSetHook)
resultSetHook - Indicates the result set factory to use.sql - Indicates the SQL statement to execute.sqlArgs - Indicates the values of the parameters in the SQL statement. The values are strings.ResultSet object if the operation is successful;ResultSet queryByStep(AbsRdbPredicates absRdbPredicates, String[] columns)
absRdbPredicates - Indicates the query conditions.columns - Indicates the columns to query. If the value is null, the query applies to all columns.ResultSet object if the operation is successful;ResultSet querySqlByStep(String sql, String[] sqlArgs)
sql - Indicates the SQL statement to execute.sqlArgs - Indicates the values of the parameters in the SQL statement. The values are strings.ResultSet object if the operation is successful;void executeSql(String sql)
Do not use this method to change the journal_mode of a database connection by the "PRAGMA journal_mode'value'" statement.
sql - Indicates the SQL statement to execute.RdbException - Throws this exception if the SQL statement is invalid.
You must handle this exception by yourself.void executeSql(String sql, Object[] sqlArgs)
Do not use this method to change the journal_mode of a database connection by the "PRAGMA journal_mode'value'" statement.
sql - Indicates the SQL statement to execute.sqlArgs - Indicates the values of the parameters in the SQL statement. The values are strings.RdbException - Throws this exception if the SQL statement is invalid.
You must handle this exception by yourself.Statement buildStatement(String sql)
A Statement cannot be used by two threads at the same time.
sql - Indicates the SQL statement to compile.Statement object, which indicates a precompiled SQL statement.long count(String tableName, String whereClause, String[] whereArgs)
To obtain an executable SQL inquiry statement, concatenate the table name and condition clause into the string
select count(*) from tableName where whereClause, and replace question marks (?) in the
string with the values in whereArgs. In this way, you can then call Statement#executeAndGetLong
to execute the SQL inquiry statement to obtain the number of rows that meet the specified conditions.
If whereClause is null or empty, the total number of rows in the table is returned.
tableName - Indicates the name of the database table. If it is null or empty,
IllegalArgumentException is thrown.whereClause - Indicates an optional condition clause, such as id =? and name like ?, in which
id and name are columns of the database table.whereArgs - Indicates the array that contains the values used to replace question marks (?)
in whereClause.long count(AbsRdbPredicates absRdbPredicates)
AbsRdbPredicates.absRdbPredicates - Indicates the specified count condition by th instance object of
AbsRdbPredicates.AbsRdbPredicates.void addAttach(String alias, String name, byte[] encryptKey)
alias - Indicates the alias of the database to attach.name - Indicates the name of the database to attach.encryptKey - Indicates the key used to encrypt the database to attach.IllegalArgumentException - Throws this exception if the alias is null.List<Pair<String,String>> listAttached()
void changeEncryptKey(byte[] newEncryptKey)
This method applies only to an encrypted database and cannot be used to change it to an unencrypted one.
Before calling this method, ensure that the database is opened and that no transaction is using the database
connection. For the maximum size of the encryption key, see StoreConfig#MAX_ENCRYPT_KEY_SIZE.
newEncryptKey - Indicates the new encryption key of the database.IllegalArgumentException - Throws this exception if the new encryption key is null or an empty string.IllegalStateException - Throws this exception if the database is not opened or not encrypted, or a
transaction is using the database connection.RdbException - Throws this exception if changing the encryption key of the encrypted database fails.int getVersion()
void setVersion(int version)
version - Indicates the database version to set.void beginTransaction()
The endTransaction() method is used to end the transaction. If the markAsCommit() method is
not used to mark the transaction as commit when the transaction ends, all the changes will be rolled back.
void beginTransactionWithObserver(TransactionObserver transactionObserver)
The endTransaction() method is used to end the transaction. If the markAsCommit() method is
not used to mark the transaction as commit when the transaction ends, all the changes will be rolled back.
transactionObserver - Provides callback methods that can be called when transactions are started,
committed, or rolled back.void markAsCommit()
After calling this method, do not perform any operation before calling the endTransaction() method.
The transaction will be committed regardless of whether an error occurs.
void endTransaction()
boolean isInTransaction()
true if the current thread is engaged in a transaction; returns false otherwise.void giveConnectionTemporarily(long milliseconds)
This method can be used to commit the current transaction and disconnect from the database temporarily
so that other threads can use the database. After a specified period, a new transaction will be started.
When using this method, ensure that the current transaction is started and that the transaction is not marked
as committed using the markAsCommit() method; otherwise, calling this method will fail. Any change that
has been made before this method is called cannot be rolled back.
milliseconds - Indicates the duration (ms) during which the current connection is interrupted from the
database.IllegalStateException - Throws this exception if the current thread contains no transaction or contains
nested transactions, or if the transaction has been marked as committed using the markAsCommit() method
before this method is called.boolean isHoldingConnection()
true if this thread holds a database connection; returns false otherwise.boolean isOpen()
true if the database is opened; returns false otherwise.String getPath()
boolean isReadOnly()
true if the database is opened in read-only mode; returns false otherwise.boolean isMemoryRdb()
true if the database is opened in the memory; returns false otherwise.boolean checkIntegrity()
This method calls the integrity_check command of the SQLite database.
true if the databases are integral; returns false otherwise.void close()
A closed RDB can no longer be used.
boolean backup(String destName)
The generated backup file is unencrypted.
destName - Indicates the name that saves the database backup.
Ensure that your application has write permissions to this name.true if the database is backed up successfully; returns false otherwise.boolean restore(String srcName)
When being called, this method deletes the original data and generates an unencrypted database.
srcName - Indicates the name that saves the unencrypted database file.
Ensure that your application has write permissions to this name.true if the database is restored successfully; returns false otherwise.boolean backup(String destName, byte[] destEncryptKey)
The generated backup is encrypted. If destEncryptKey is null, the generated backup is unencrypted.
In that case, this method is equivalent to the RdbStore#backup(String) method.
For the maximum size of the encryption key, see StoreConfig#MAX_ENCRYPT_KEY_SIZE.
destName - Indicates the name that saves the database backup.
Ensure that your application has write permissions to this name.destEncryptKey - Indicates the encryption key of the database to back up.
It can be different from that of the current database.true if the database is backed up successfully; returns false otherwise.boolean restore(String srcName, byte[] srcEncryptKey, byte[] destEncryptKey)
When being called, this method deletes the original data and generates an encrypted or unencrypted database,
as defined by your application. If both srcEncryptKey and destEncryptKey are null,
this method is equivalent to the RdbStore#restore(String) method.
For the maximum size of the encryption key, see StoreConfig#MAX_ENCRYPT_KEY_SIZE.
srcName - Indicates the name that stores the source database file.
Ensure that your application has write permissions to this name.srcEncryptKey - Indicates the encryption key of the source database.
If the database is unencrypted, set the value of this parameter to null.destEncryptKey - Indicates the encryption key of the restored database.If you want
the database to be unencrypted, set the value of this parameter to null.true if the database is restored successfully; returns false otherwise.void configLocale(Locale locale)
This method is unavailable if the database is read-only or the data set is in a transaction. After the
database locale is set, you can use select * from table where column=xxxx COLLATE LOCALES; or
select * from test order by column COLLATE LOCALES ASC; to sort and query data locally.
locale - Indicates the locale to set.