SupportSQLiteDatabase


public interface SupportSQLiteDatabase extends Closeable


A database abstraction which removes the framework dependency and allows swapping underlying sql versions. It mimics the behavior of android.database.sqlite.SQLiteDatabase

Summary

Public methods

abstract void

Begins a transaction in EXCLUSIVE mode.

abstract void

Begins a transaction in IMMEDIATE mode.

default void

Begins a transaction in DEFERRED mode, with the android-specific constraint that the transaction is read-only.

abstract void

Begins a transaction in EXCLUSIVE mode.

abstract void

Begins a transaction in IMMEDIATE mode.

default void

Begins a transaction in read-only mode with a {@link SQLiteTransactionListener} listener.

abstract @NonNull SupportSQLiteStatement

Compiles the given SQL statement.

abstract int
delete(@NonNull String table, String whereClause, Object[] whereArgs)

Convenience method for deleting rows in the database.

abstract void

This method disables the features enabled by enableWriteAheadLogging.

abstract boolean

This method enables parallel execution of queries from multiple threads on the same database.

abstract void

End a transaction.

default void
execPerConnectionSQL(@NonNull String sql, Object[] bindArgs)

Execute the given SQL statement on all connections to this database.

abstract void

Execute a single SQL statement that does not return any data.

abstract void
execSQL(@NonNull String sql, @NonNull Object[] bindArgs)

Execute a single SQL statement that does not return any data.

abstract List<@NonNull Pair<@NonNull String, @NonNull String>>

The list of full path names of all attached databases including the main database by executing 'pragma database_list' on the database.

abstract long

The maximum size the database may grow to.

abstract long

The current database page size, in bytes.

abstract String

The path to the database file.

abstract int

The database version.

abstract boolean

Returns true if the current thread has a transaction pending.

abstract long
insert(
    @NonNull String table,
    int conflictAlgorithm,
    @NonNull ContentValues values
)

Convenience method for inserting a row into the database.

abstract boolean

Is true if the given database (and all its attached databases) pass integrity_check, false otherwise.

abstract boolean

True if the current thread is holding an active connection to the database.

default boolean

Is true if execPerConnectionSQL is supported by the implementation.

abstract boolean

Is true if the database is currently open.

abstract boolean

Is true if the database is opened as read only.

abstract boolean

Is true if write-ahead logging has been enabled for this database.

abstract boolean
needUpgrade(int newVersion)

Returns true if the new version code is greater than the current database version.

abstract @NonNull Cursor

Runs the given query on the database.

abstract @NonNull Cursor

Runs the given query on the database.

abstract @NonNull Cursor
query(@NonNull String query, @NonNull Object[] bindArgs)

Runs the given query on the database.

abstract @NonNull Cursor
query(
    @NonNull SupportSQLiteQuery query,
    CancellationSignal cancellationSignal
)

Runs the given query on the database.

abstract void

Sets whether foreign key constraints are enabled for the database.

abstract void

Sets the locale for this database.

abstract void
setMaxSqlCacheSize(int cacheSize)

Sets the maximum size of the prepared-statement cache for this database.

abstract long
setMaximumSize(long numBytes)

Sets the maximum size the database will grow to.

abstract void
setPageSize(long pageSize)

The current database page size, in bytes.

abstract void

Marks the current transaction as successful.

abstract void
setVersion(int version)

The database version.

abstract int
update(
    @NonNull String table,
    int conflictAlgorithm,
    @NonNull ContentValues values,
    String whereClause,
    Object[] whereArgs
)

Convenience method for updating rows in the database.

abstract boolean

Temporarily end the transaction to let other threads run.

abstract boolean
yieldIfContendedSafely(long sleepAfterYieldDelayMillis)

Temporarily end the transaction to let other threads run.

Inherited methods

From java.io.Closeable
abstract void

Public methods

beginTransaction

Added in 2.0.0
abstract void beginTransaction()

Begins a transaction in EXCLUSIVE mode.

Transactions can be nested. When the outer transaction is ended all of the work done in that transaction and all of the nested transactions will be committed or rolled back. The changes will be rolled back if any transaction is ended without being marked as clean (by calling setTransactionSuccessful). Otherwise they will be committed.

Here is the standard idiom for transactions:

db.beginTransaction()
try {
...
db.setTransactionSuccessful()
} finally {
db.endTransaction()
}

beginTransactionNonExclusive

Added in 2.0.0
abstract void beginTransactionNonExclusive()

Begins a transaction in IMMEDIATE mode. Transactions can be nested. When the outer transaction is ended all of the work done in that transaction and all of the nested transactions will be committed or rolled back. The changes will be rolled back if any transaction is ended without being marked as clean (by calling setTransactionSuccessful). Otherwise they will be committed.

Here is the standard idiom for transactions:

db.beginTransactionNonExclusive()
try {
...
db.setTransactionSuccessful()
} finally {
db.endTransaction()
}

beginTransactionReadOnly

Added in 2.5.0
default void beginTransactionReadOnly()

Begins a transaction in DEFERRED mode, with the android-specific constraint that the transaction is read-only. The database may not be modified inside a read-only transaction otherwise a android.database.sqlite.SQLiteDatabaseLockedException might be thrown.

Read-only transactions may run concurrently with other read-only transactions, and if they database is in WAL mode, they may also run concurrently with IMMEDIATE or EXCLUSIVE transactions.

Transactions can be nested. However, the behavior of the transaction is not altered by nested transactions. A nested transaction may be any of the three transaction types but if the outermost type is read-only then nested transactions remain read-only, regardless of how they are started.

Here is the standard idiom for read-only transactions:

db.beginTransactionReadOnly();
try {
...
} finally {
db.endTransaction();
}

If the implementation does not support read-only transactions then the default implementation delegates to beginTransaction.

beginTransactionWithListener

Added in 2.0.0
abstract void beginTransactionWithListener(
    @NonNull SQLiteTransactionListener transactionListener
)

Begins a transaction in EXCLUSIVE mode.

Transactions can be nested. When the outer transaction is ended all of the work done in that transaction and all of the nested transactions will be committed or rolled back. The changes will be rolled back if any transaction is ended without being marked as clean (by calling setTransactionSuccessful). Otherwise they will be committed.

Here is the standard idiom for transactions:

db.beginTransactionWithListener(listener)
try {
...
db.setTransactionSuccessful()
} finally {
db.endTransaction()
}
Parameters
@NonNull SQLiteTransactionListener transactionListener

listener that should be notified when the transaction begins, commits, or is rolled back, either explicitly or by a call to yieldIfContendedSafely.

beginTransactionWithListenerNonExclusive

Added in 2.0.0
abstract void beginTransactionWithListenerNonExclusive(
    @NonNull SQLiteTransactionListener transactionListener
)

Begins a transaction in IMMEDIATE mode. Transactions can be nested. When the outer transaction is ended all of the work done in that transaction and all of the nested transactions will be committed or rolled back. The changes will be rolled back if any transaction is ended without being marked as clean (by calling setTransactionSuccessful). Otherwise they will be committed.

Here is the standard idiom for transactions:

db.beginTransactionWithListenerNonExclusive(listener)
try {
...
db.setTransactionSuccessful()
} finally {
db.endTransaction()
}
Parameters
@NonNull SQLiteTransactionListener transactionListener

listener that should be notified when the transaction begins, commits, or is rolled back, either explicitly or by a call to yieldIfContendedSafely.

beginTransactionWithListenerReadOnly

Added in 2.5.0
default void beginTransactionWithListenerReadOnly(
    @NonNull SQLiteTransactionListener transactionListener
)

Begins a transaction in read-only mode with a {@link SQLiteTransactionListener} listener. The database may not be modified inside a read-only transaction otherwise a android.database.sqlite.SQLiteDatabaseLockedException might be thrown.

Transactions can be nested. However, the behavior of the transaction is not altered by nested transactions. A nested transaction may be any of the three transaction types but if the outermost type is read-only then nested transactions remain read-only, regardless of how they are started.

Here is the standard idiom for read-only transactions:

db.beginTransactionWightListenerReadOnly(listener);
try {
...
} finally {
db.endTransaction();
}

If the implementation does not support read-only transactions then the default implementation delegates to beginTransactionWithListener.

compileStatement

Added in 2.0.0
abstract @NonNull SupportSQLiteStatement compileStatement(@NonNull String sql)

Compiles the given SQL statement.

Parameters
@NonNull String sql

The sql query.

Returns
@NonNull SupportSQLiteStatement

Compiled statement.

delete

Added in 2.0.0
abstract int delete(@NonNull String table, String whereClause, Object[] whereArgs)

Convenience method for deleting rows in the database.

Parameters
@NonNull String table

the table to delete from

String whereClause

the optional WHERE clause to apply when deleting. Passing null will delete all rows.

Object[] whereArgs

You may include ?s in the where clause, which will be replaced by the values from whereArgs. The values will be bound as Strings.

Returns
int

the number of rows affected if a whereClause is passed in, 0 otherwise. To remove all rows and get a count pass "1" as the whereClause.

disableWriteAheadLogging

Added in 2.0.0
abstract void disableWriteAheadLogging()

This method disables the features enabled by enableWriteAheadLogging.

Throws
kotlin.IllegalStateException

if there are transactions in progress at the time this method is called. WAL mode can only be changed when there are no transactions in progress.

enableWriteAheadLogging

Added in 2.0.0
abstract boolean enableWriteAheadLogging()

This method enables parallel execution of queries from multiple threads on the same database. It does this by opening multiple connections to the database and using a different database connection for each query. The database journal mode is also changed to enable writes to proceed concurrently with reads.

When write-ahead logging is not enabled (the default), it is not possible for reads and writes to occur on the database at the same time. Before modifying the database, the writer implicitly acquires an exclusive lock on the database which prevents readers from accessing the database until the write is completed.

In contrast, when write-ahead logging is enabled (by calling this method), write operations occur in a separate log file which allows reads to proceed concurrently. While a write is in progress, readers on other threads will perceive the state of the database as it was before the write began. When the write completes, readers on other threads will then perceive the new state of the database.

It is a good idea to enable write-ahead logging whenever a database will be concurrently accessed and modified by multiple threads at the same time. However, write-ahead logging uses significantly more memory than ordinary journaling because there are multiple connections to the same database. So if a database will only be used by a single thread, or if optimizing concurrency is not very important, then write-ahead logging should be disabled.

After calling this method, execution of queries in parallel is enabled as long as the database remains open. To disable execution of queries in parallel, either call disableWriteAheadLogging or close the database and reopen it.

The maximum number of connections used to execute queries in parallel is dependent upon the device memory and possibly other properties.

If a query is part of a transaction, then it is executed on the same database handle the transaction was begun.

Writers should use beginTransactionNonExclusive or beginTransactionWithListenerNonExclusive to start a transaction. Non-exclusive mode allows database file to be in readable by other threads executing queries.

If the database has any attached databases, then execution of queries in parallel is NOT possible. Likewise, write-ahead logging is not supported for read-only databases or memory databases. In such cases, enableWriteAheadLogging returns false.

The best way to enable write-ahead logging is to pass the android.database.sqlite.SQLiteDatabase.ENABLE_WRITE_AHEAD_LOGGING flag to android.database.sqlite.SQLiteDatabase.openDatabase. This is more efficient than calling

SQLiteDatabase db = SQLiteDatabase.openDatabase("db_filename", cursorFactory, SQLiteDatabase.CREATE_IF_NECESSARY | SQLiteDatabase.ENABLE_WRITE_AHEAD_LOGGING, myDatabaseErrorHandler) db.enableWriteAheadLogging()

Another way to enable write-ahead logging is to call enableWriteAheadLogging after opening the database.

SQLiteDatabase db = SQLiteDatabase.openDatabase("db_filename", cursorFactory, SQLiteDatabase.CREATE_IF_NECESSARY, myDatabaseErrorHandler) db.enableWriteAheadLogging()

See also SQLite Write-Ahead Logging for more details about how write-ahead logging works.

Returns
boolean

True if write-ahead logging is enabled.

Throws
kotlin.IllegalStateException

if there are transactions in progress at the time this method is called. WAL mode can only be changed when there are no transactions in progress.

endTransaction

Added in 2.0.0
abstract void endTransaction()

End a transaction. See beginTransaction for notes about how to use this and when transactions are committed and rolled back.

execPerConnectionSQL

Added in 2.2.0
default void execPerConnectionSQL(@NonNull String sql, Object[] bindArgs)

Execute the given SQL statement on all connections to this database.

This statement will be immediately executed on all existing connections, and will be automatically executed on all future connections.

Some example usages are changes like PRAGMA trusted_schema=OFF or functions like SELECT icu_load_collation(). If you execute these statements using execSQL then they will only apply to a single database connection; using this method will ensure that they are uniformly applied to all current and future connections.

An implementation of SupportSQLiteDatabase might not support this operation. Use isExecPerConnectionSQLSupported to check if this operation is supported before calling this method.

Parameters
@NonNull String sql

The SQL statement to be executed. Multiple statements separated by semicolons are not supported.

Object[] bindArgs

The arguments that should be bound to the SQL statement.

Throws
kotlin.UnsupportedOperationException

if this operation is not supported. To check if it supported use isExecPerConnectionSQLSupported

execSQL

Added in 2.0.0
abstract void execSQL(@NonNull String sql)

Execute a single SQL statement that does not return any data.

When using enableWriteAheadLogging, journal_mode is automatically managed by this class. So, do not set journal_mode using "PRAGMA journal_mode" statement if your app is using enableWriteAheadLogging

Parameters
@NonNull String sql

the SQL statement to be executed. Multiple statements separated by semicolons are not supported.

Throws
android.database.SQLException

if the SQL string is invalid

execSQL

Added in 2.0.0
abstract void execSQL(@NonNull String sql, @NonNull Object[] bindArgs)

Execute a single SQL statement that does not return any data.

When using enableWriteAheadLogging, journal_mode is automatically managed by this class. So, do not set journal_mode using "PRAGMA journal_mode" statement if your app is using enableWriteAheadLogging

Parameters
@NonNull String sql

the SQL statement to be executed. Multiple statements separated by semicolons are not supported.

@NonNull Object[] bindArgs

only byte[], String, Long and Double are supported in selectionArgs.

Throws