RoomDatabase

abstract class RoomDatabase
kotlin.Any
   ↳ androidx.room.RoomDatabase

Base class for all Room databases. All classes that are annotated with Database must extend this class.

RoomDatabase provides direct access to the underlying database implementation but you should prefer using Dao classes.

Summary

Nested classes
open

Builder for RoomDatabase.

abstract

Callback for RoomDatabase.

Journal modes for SQLite database.

open

A container to hold migrations.

Public constructors

Creates a RoomDatabase.

Public methods
open SupportSQLiteStatement!

Wrapper for SupportSQLiteDatabase#compileStatement(String).

open Unit

Wrapper for SupportSQLiteDatabase#setTransactionSuccessful().

open Unit
init(configuration: DatabaseConfiguration)

Called by Room when it is initialized.

open Cursor!
query(query: String!, args: Array<Any!>?)

Convenience method to query the database with arguments.

open Cursor!
query(query: SupportSQLiteQuery!)

Wrapper for SupportSQLiteDatabase#query(SupportSQLiteQuery).

open Unit

Wrapper for SupportSQLiteDatabase#endTransaction().

open Boolean

Returns true if current thread is in a transaction.

open Unit

Wrapper for SupportSQLiteDatabase#beginTransaction().

open SupportSQLiteOpenHelper

Returns the SQLite open helper used by this database.

open InvalidationTracker

Returns the invalidation tracker for this database.

open Boolean

Returns true if database connection is open and initialized.

open Unit

Executes the specified Runnable in a database transaction.

open V

Executes the specified Callable in a database transaction.

abstract Unit

Deletes all rows from all the tables that are registered to this database as Database#entities().

open Unit

Closes the database if it is already open.

Protected methods
abstract SupportSQLiteOpenHelper

Creates the open helper to access the database.

abstract InvalidationTracker

Called when the RoomDatabase is created.

open Unit
internalInitInvalidationTracker(db: SupportSQLiteDatabase)

Called by the generated code when database is open.

Properties
MutableList<RoomDatabase.Callback!>!

SupportSQLiteDatabase!

Public constructors

<init>

RoomDatabase()

Creates a RoomDatabase.

You cannot create an instance of a database, instead, you should acquire it via Room#databaseBuilder(Context, Class, String) or Room#inMemoryDatabaseBuilder(Context, Class).

Public methods

compileStatement

open fun compileStatement(sql: String): SupportSQLiteStatement!

Wrapper for SupportSQLiteDatabase#compileStatement(String).

Parameters
sql String: The query to compile.
Return
SupportSQLiteStatement!: The compiled query.

setTransactionSuccessful

open fun setTransactionSuccessful(): Unit

Wrapper for SupportSQLiteDatabase#setTransactionSuccessful().

init

open fun init(configuration: DatabaseConfiguration): Unit

Called by Room when it is initialized.

Parameters
configuration DatabaseConfiguration: The database configuration.

query

open fun query(query: String!, args: Array<Any!>?): Cursor!

Convenience method to query the database with arguments.

Parameters
query String!: The sql query
args String!: The bind arguments for the placeholders in the query
Return
Cursor!: A Cursor obtained by running the given query in the Room database.

query

open fun query(query: SupportSQLiteQuery!): Cursor!

Wrapper for SupportSQLiteDatabase#query(SupportSQLiteQuery).

Parameters
query SupportSQLiteQuery!: The Query which includes the SQL and a bind callback for bind arguments.
Return
Cursor!: Result of the query.

endTransaction

open fun endTransaction(): Unit

Wrapper for SupportSQLiteDatabase#endTransaction().

inTransaction

open fun inTransaction(): Boolean

Returns true if current thread is in a transaction.

Return
Boolean: True if there is an active transaction in current thread, false otherwise.

See Also

  • SupportSQLiteDatabase#inTransaction()

beginTransaction

open fun beginTransaction(): Unit

Wrapper for SupportSQLiteDatabase#beginTransaction().

getOpenHelper

open fun getOpenHelper(): SupportSQLiteOpenHelper

Returns the SQLite open helper used by this database.

Return
SupportSQLiteOpenHelper: The SQLite open helper used by this database.

getInvalidationTracker

open fun getInvalidationTracker(): InvalidationTracker

Returns the invalidation tracker for this database.

You can use the invalidation tracker to get notified when certain tables in the database are modified.

Return
InvalidationTracker: The invalidation tracker for the database.

isOpen

open fun isOpen(): Boolean

Returns true if database connection is open and initialized.

Return
Boolean: true if the database connection is open, false otherwise.

runInTransaction

open fun runInTransaction(body: Runnable): Unit

Executes the specified Runnable in a database transaction. The transaction will be marked as successful unless an exception is thrown in the Runnable.

Parameters
body Runnable: The piece of code to execute.

runInTransaction

open fun <V : Any!> runInTransaction(body: Callable<V>): V

Executes the specified Callable in a database transaction. The transaction will be marked as successful unless an exception is thrown in the Callable.

Parameters
body Callable<V>: The piece of code to execute.
<V> Callable<V>: The type of the return value.
Return
V: The value returned from the Callable.

clearAllTables

abstract fun clearAllTables(): Unit

Deletes all rows from all the tables that are registered to this database as Database#entities().

This does NOT reset the auto-increment value generated by PrimaryKey#autoGenerate().

After deleting the rows, Room will set a WAL checkpoint and run VACUUM. This means that the data is completely erased. The space will be reclaimed by the system if the amount surpasses the threshold of database file size.

See Also

  • <a href="https://www.sqlite.org/fileformat.html">Database File Format</a>

close

open fun close(): Unit

Closes the database if it is already open.

Protected methods

createOpenHelper

protected abstract fun createOpenHelper(config: DatabaseConfiguration!): SupportSQLiteOpenHelper

Creates the open helper to access the database. Generated class already implements this method. Note that this method is called when the RoomDatabase is initialized.

Parameters
config DatabaseConfiguration!: The configuration of the Room database.
Return
SupportSQLiteOpenHelper: A new SupportSQLiteOpenHelper to be used while connecting to the database.

createInvalidationTracker

protected abstract fun createInvalidationTracker(): InvalidationTracker

Called when the RoomDatabase is created.

This is already implemented by the generated code.

Return
InvalidationTracker: Creates a new InvalidationTracker.

internalInitInvalidationTracker

protected open fun internalInitInvalidationTracker(db: SupportSQLiteDatabase): Unit

Called by the generated code when database is open.

You should never call this method manually.

Parameters
db SupportSQLiteDatabase: The database instance.

Properties

mCallbacks

protected var mCallbacks: MutableList<RoomDatabase.Callback!>!

mDatabase

protected var mDatabase: SupportSQLiteDatabase!