public class DatabaseHelper extends Object
Context#getDatabaseDir() and Context#getPreferencesDir().
This class provides methods for constructing and deleting ORM, RDB, and Preferences databases. It is the main entry class in the database scenario.
SystemCapability.Data#DATA_APPDATAMGR| 构造器 | 说明 |
|---|---|
DatabaseHelper(Context context) |
A constructor used to create a
DatabaseHelper instance based on the specified context. |
| 限定符和类型 | 方法 | 说明 |
|---|---|---|
boolean |
deletePreferences(String name) |
Deletes a
Preferences instance matching a specified preferences file name
from the cache which is performed by DatabaseHelper#removePreferencesFromCache and deletes the
preferences file. |
boolean |
deleteRdbStore(String name) |
Deletes the database with a specified name.
|
boolean |
deleteRdbStore(DatabaseFileConfig fileConfig) |
Deletes a database with a specified database file configuration.
|
OrmContext |
getOrmContext(String alias) |
Obtains an ORM context mapping to an ORM database with a specified alias.
|
<T extends OrmDatabase> |
getOrmContext(String alias,
String name,
Class<T> ormDatabase,
OrmMigration... migrations) |
Creates an object-relational mapping (ORM) database and obtains the context mapped to this database.
|
<T extends OrmDatabase> |
getOrmContext(OrmConfig ormConfig,
Class<T> ormDatabase,
OrmMigration... migrations) |
Creates an object-relational mapping (ORM) database and obtains the
OrmContext object mapped to the database. |
Preferences |
getPreferences(String name) |
Obtains a
Preferences instance matching a specified preferences file name. |
RdbStore |
getRdbStore(StoreConfig config,
int version,
RdbOpenCallback openCallback) |
Obtains an RDB store.
|
RdbStore |
getRdbStore(StoreConfig config,
int version,
RdbOpenCallback openCallback,
ResultSetHook resultSetHook) |
Obtains an RDB store.
|
boolean |
moveDatabase(Context srcContext,
String srcName,
String destName) |
Moves the database.
|
boolean |
movePreferences(Context sourceContext,
String sourceName,
String targetName) |
Moves the preferences file.
|
static int |
releaseRdbMemory() |
Releases the heap memory in the SQLite database.
|
void |
removePreferencesFromCache(String name) |
Deletes a
Preferences instance matching a specified preferences file name
from the cache. |
public DatabaseHelper(Context context)
DatabaseHelper instance based on the specified context.
The context will also be included in the created DatabaseHelper instance.
context - Indicates the context Context. The context cannot be null if the database is
an encrypted database.public RdbStore getRdbStore(StoreConfig config, int version, RdbOpenCallback openCallback)
You can set parameters of the RDB store as required. In general, this method is recommended to obtain a rdb store.
config - Indicates the configuration of the database related to this RDB store. The configurations include
the database path, storage mode, and whether the database is read-only.version - Indicates the database version for upgrade or downgrade.openCallback - Indicates a helper used to handle RdbOpenCallback.onCreate(ohos.data.rdb.RdbStore),
RdbOpenCallback.onUpgrade(ohos.data.rdb.RdbStore, int, int), RdbOpenCallback.onDowngrade(ohos.data.rdb.RdbStore, int, int)
and RdbOpenCallback.onOpen(ohos.data.rdb.RdbStore).RdbStore.IllegalArgumentException - Throws this exception if any of the input parameters is invalid.public RdbStore getRdbStore(StoreConfig config, int version, RdbOpenCallback openCallback, ResultSetHook resultSetHook)
You can set parameters of the RDB store as required, and call the related interfaces to perform data operations.
config - Indicates the configuration of the database related to this RDB store. The configurations include
the database path, storage mode, and whether the database is read-only.version - Indicates the database version for upgrade or downgrade.openCallback - Indicates a helper used to handle RdbOpenCallback.onCreate(ohos.data.rdb.RdbStore),
RdbOpenCallback.onUpgrade(ohos.data.rdb.RdbStore, int, int), RdbOpenCallback.onDowngrade(ohos.data.rdb.RdbStore, int, int)
and RdbOpenCallback.onOpen(ohos.data.rdb.RdbStore).resultSetHook - Indicates a result set factory ResultSetHook.RdbStore.IllegalArgumentException - Throws this exception if any of the input parameters is invalid.public <T extends OrmDatabase> OrmContext getOrmContext(String alias, String name, Class<T> ormDatabase, OrmMigration... migrations)
You can call this method to add, delete, update, and query data in the ORM database.
To delete a database file, call the deleteRdbStore(java.lang.String) method with the database name specified.
T - Indicates an ORM database class that inherits from OrmDatabase.alias - Indicates the alias of the ORM database.name - Indicates the database file name.ormDatabase - Indicates the ORM database.migrations - Indicates the configuration instance used for database upgrade and downgrade.IllegalArgumentException - Throws this exception if any of the input parameters is invalid.public <T extends OrmDatabase> OrmContext getOrmContext(OrmConfig ormConfig, Class<T> ormDatabase, OrmMigration... migrations)
OrmContext object mapped to the database.
You can call the OrmContext interface
to add, delete, update, and query data in the ORM database.
To delete a database file, call the deleteRdbStore(java.lang.String)
method with the database file path specified.
T - Indicates an ORM database class that inherits from OrmDatabase.ormConfig - Indicates the ORM database configuration.ormDatabase - Indicates the ORM database used to store data.migrations - Indicates the ORM database migration callback.OrmContext object.IllegalArgumentException - Throws this exception if any of the input parameters is invalid.public OrmContext getOrmContext(String alias)
This method applies to an ORM database that has been created.
alias - Indicates the alias of the ORM database, such as name..OrmContext object.IllegalArgumentException - Throws this exception if any of the input parameters is invalid.public boolean deleteRdbStore(String name)
name - Indicates the database name.true if the database is deleted; returns false otherwise.IllegalArgumentException - Throws this exception if the passed name or path is invalid,
or a relative path is passed.public boolean deleteRdbStore(DatabaseFileConfig fileConfig)
fileConfig - Indicates the database file configuration.true if the database is successfully deleted; returns false otherwise.IllegalArgumentException - Throws this exception if fileConfig is invalid.public static int releaseRdbMemory()
This method depends on the sqlite3_release_memory method of the SQLite database, which
determines how much heap memory will be released.
public Preferences getPreferences(String name)
Preferences instance matching a specified preferences file name.
The Preferences instance loads all data of the preferences file and
resides in the memory. You can use removePreferencesFromCache(java.lang.String) to remove the instance from the memory.
name - Indicates the name of preferences file stored in a path specified by
Context.getPreferencesDir().Preferences instance matching the specified preferences
file name.IllegalArgumentException - Throws this exception if name or
Context.getPreferencesDir() is invalid.public boolean deletePreferences(String name)
Preferences instance matching a specified preferences file name
from the cache which is performed by DatabaseHelper#removePreferencesFromCache and deletes the
preferences file.
When deleting the Preferences instance, you must release all references
of the instance. In addition, do not use the instance to perform data operations. Otherwise, data inconsistency
will occur.
name - Indicates the name of preferences file stored in a path specified by
Context.getPreferencesDir().true if the Preferences instance and the preferences
file are deleted; returns false otherwise.IllegalArgumentException - Throws this exception if name or
Context.getPreferencesDir() is invalid.public void removePreferencesFromCache(String name)
Preferences instance matching a specified preferences file name
from the cache.
When deleting the Preferences instance, you must release all references
of the instance. In addition, do not use the instance to perform data operations. Otherwise, data inconsistency
will occur.
name - Indicates the name of preferences file stored in a path specified by
Context.getPreferencesDir().IllegalArgumentException - Throws this exception if name or
Context.getPreferencesDir() is invalid.public boolean movePreferences(Context sourceContext, String sourceName, String targetName)
sourceContext - Indicates the context used to obtain the path of the source file.sourceName - Indicates the source file name.targetName - Indicates the target file name. The file path can be obtained using the
Context of this DatabaseHelper instance.true if the preferences file is moved successfully; returns false otherwise.IllegalArgumentException - Throws this exception if any of the input parameters is invalid.IllegalStateException - Throws this exception if an error occurs during the file move.public boolean moveDatabase(Context srcContext, String srcName, String destName)
srcContext - Indicates the context used to obtain the path of the source database.srcName - Identifies the source database. It can be the database name, in which case the database path
can be obtained using srcContext. It can also be the absolute path of the source database, for
example, /xxx/xxx/xxx. In this case, srcContext can be null.destName - Indicates the target database name.true if the database is moved successfully; returns false otherwise.IllegalArgumentException - Throws this exception if any of the input parameters is invalid.IllegalStateException - Throws this exception if an error occurs during the database move.