Skip to content

Most visited

Recently visited

navigation

RoomDatabase

public abstract class RoomDatabase
extends Object

java.lang.Object
   ↳ android.arch.persistence.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.

See also:

Summary

Nested classes

class RoomDatabase.Builder<T extends RoomDatabase>

Builder for RoomDatabase. 

class RoomDatabase.Callback

Callback for RoomDatabase

class RoomDatabase.MigrationContainer

A container to hold migrations. 

Fields

protected List<RoomDatabase.Callback> mCallbacks

protected SupportSQLiteDatabase mDatabase

Public constructors

RoomDatabase()

Creates a RoomDatabase.

Public methods

void beginTransaction()

Wrapper for beginTransaction().

void close()

Closes the database if it is already open.

SupportSQLiteStatement compileStatement(String sql)

Wrapper for compileStatement(String).

void endTransaction()

Wrapper for endTransaction().

InvalidationTracker getInvalidationTracker()

Returns the invalidation tracker for this database.

SupportSQLiteOpenHelper getOpenHelper()

Returns the SQLite open helper used by this database.

boolean inTransaction()

Returns true if current thread is in a transaction.

void init(DatabaseConfiguration configuration)

Called by Room when it is initialized.

boolean isOpen()

Returns true if database connection is open and initialized.

Cursor query(SupportSQLiteQuery query)

Wrapper for query(SupportSQLiteQuery).

Cursor query(String query, Object[] args)

Convenience method to query the database with arguments.

<V> V runInTransaction(Callable<V> body)

Executes the specified Callable in a database transaction.

void runInTransaction(Runnable body)

Executes the specified Runnable in a database transaction.

void setTransactionSuccessful()

Wrapper for setTransactionSuccessful().

Protected methods

abstract InvalidationTracker createInvalidationTracker()

Called when the RoomDatabase is created.

abstract SupportSQLiteOpenHelper createOpenHelper(DatabaseConfiguration config)

Creates the open helper to access the database.

void internalInitInvalidationTracker(SupportSQLiteDatabase db)

Called by the generated code when database is open.

Inherited methods

From class java.lang.Object

Fields

mCallbacks

List<RoomDatabase.Callback> mCallbacks

mDatabase

SupportSQLiteDatabase mDatabase

Public constructors

RoomDatabase

RoomDatabase ()

Creates a RoomDatabase.

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

Public methods

beginTransaction

void beginTransaction ()

Wrapper for beginTransaction().

close

void close ()

Closes the database if it is already open.

compileStatement

SupportSQLiteStatement compileStatement (String sql)

Wrapper for compileStatement(String).

Parameters
sql String: The query to compile.

Returns
SupportSQLiteStatement The compiled query.

endTransaction

void endTransaction ()

Wrapper for endTransaction().

getInvalidationTracker

InvalidationTracker getInvalidationTracker ()

Returns the invalidation tracker for this database.

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

Returns
InvalidationTracker The invalidation tracker for the database.

getOpenHelper

SupportSQLiteOpenHelper getOpenHelper ()

Returns the SQLite open helper used by this database.

Returns
SupportSQLiteOpenHelper The SQLite open helper used by this database.

inTransaction

boolean inTransaction ()

Returns true if current thread is in a transaction.

Returns
boolean True if there is an active transaction in current thread, false otherwise.

See also:

init

void init (DatabaseConfiguration configuration)

Called by Room when it is initialized.

Parameters
configuration DatabaseConfiguration: The database configuration.

isOpen

boolean isOpen ()

Returns true if database connection is open and initialized.

Returns
boolean true if the database connection is open, false otherwise.

query

Cursor query (SupportSQLiteQuery query)

Wrapper for query(SupportSQLiteQuery).

Parameters
query SupportSQLiteQuery: The Query which includes the SQL and a bind callback for bind arguments.

Returns
Cursor Result of the query.

query

Cursor query (String query, 
                Object[] args)

Convenience method to query the database with arguments.

Parameters
query String: The sql query

args Object: The bind arguments for the placeholders in the query

Returns
Cursor A Cursor obtained by running the given query in the Room database.

runInTransaction

V runInTransaction (Callable<V> body)

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: The piece of code to execute.

Returns
V The value returned from the Callable.

runInTransaction

void runInTransaction (Runnable body)

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.

setTransactionSuccessful

void setTransactionSuccessful ()

Wrapper for setTransactionSuccessful().

Protected methods

createInvalidationTracker

InvalidationTracker createInvalidationTracker ()

Called when the RoomDatabase is created.

This is already implemented by the generated code.

Returns
InvalidationTracker Creates a new InvalidationTracker.

createOpenHelper

SupportSQLiteOpenHelper createOpenHelper (DatabaseConfiguration config)

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.

Returns
SupportSQLiteOpenHelper A new SupportSQLiteOpenHelper to be used while connecting to the database.

internalInitInvalidationTracker

void internalInitInvalidationTracker (SupportSQLiteDatabase db)

Called by the generated code when database is open.

You should never call this method manually.

Parameters
db SupportSQLiteDatabase: The database instance.

This site uses cookies to store your preferences for site-specific language and display options.

Get the latest Android developer news and tips that will help you find success on Google Play.

* Required Fields

Hooray!

Browse this site in ?

You requested a page in , but your language preference for this site is .

Would you like to change your language preference and browse this site in ? If you want to change your language preference later, use the language menu at the bottom of each page.

This class requires API level or higher

This doc is hidden because your selected API level for the documentation is . You can change the documentation API level with the selector above the left navigation.

For more information about specifying the API level your app requires, read Supporting Different Platform Versions.

Take a short survey?
Help us improve the Android developer experience.
(Sep 2017 survey)