added in version 1.1.0
belongs to Maven artifact android.arch.persistence.room:runtime:1.1.0-beta2

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

enum RoomDatabase.JournalMode

Journal modes for SQLite database. 

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().

abstract void clearAllTables()

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

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

Fields

mCallbacks

added in version 1.1.0
List<RoomDatabase.Callback> mCallbacks

mDatabase

added in version 1.1.0
SupportSQLiteDatabase mDatabase

Public constructors

RoomDatabase

added in version 1.1.0
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

added in version 1.1.0
void beginTransaction ()

Wrapper for beginTransaction().

clearAllTables

added in version 1.1.0
void clearAllTables ()

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

This does NOT reset the auto-increment value generated by 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.

close

added in version 1.1.0
void close ()

Closes the database if it is already open.

compileStatement

added in version 1.1.0
SupportSQLiteStatement compileStatement (String sql)

Wrapper for compileStatement(String).

Parameters
sql String: The query to compile.

Returns
SupportSQLiteStatement The compiled query.

endTransaction

added in version 1.1.0
void endTransaction ()

Wrapper for endTransaction().

getInvalidationTracker

added in version 1.1.0
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

added in version 1.1.0
SupportSQLiteOpenHelper getOpenHelper ()

Returns the SQLite open helper used by this database.

Returns
SupportSQLiteOpenHelper The SQLite open helper used by this database.

inTransaction

added in version 1.1.0
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

added in version 1.1.0
void init (DatabaseConfiguration configuration)

Called by Room when it is initialized.

Parameters
configuration DatabaseConfiguration: The database configuration.

isOpen

added in version 1.1.0
boolean isOpen ()

Returns true if database connection is open and initialized.

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

query

added in version 1.1.0
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

added in version 1.1.0
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

added in version 1.1.0
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

added in version 1.1.0
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

added in version 1.1.0
void setTransactionSuccessful ()

Wrapper for setTransactionSuccessful().

Protected methods

createInvalidationTracker

added in version 1.1.0
InvalidationTracker createInvalidationTracker ()

Called when the RoomDatabase is created.

This is already implemented by the generated code.

Returns
InvalidationTracker Creates a new InvalidationTracker.

createOpenHelper

added in version 1.1.0
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

added in version 1.1.0
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.