H2Database

This class represents a database connection.

Methods
static H2Database create(H2Database.CursorFactory factory)
Create a new in-memory database.
static H2Database create(H2Database.CursorFactory factory)
Create a new in-memory database.
Parameters:
factory - the cursor factory
Returns:
a connection to this database
static String findEditTable(String tables)
TODO
static String findEditTable(String tables)
TODO
Parameters:
tables - the list of tables
Returns:
TODO
static H2Database openDatabase(String path, H2Database.CursorFactory factory, int flags)
Open a connection to the given database.
static H2Database openDatabase(String path, H2Database.CursorFactory factory, int flags)
Open a connection to the given database.
Parameters:
path - the database file name
factory - the cursor factory
flags - 0, or a combination of OPEN_READONLY and CREATE_IF_NECESSARY
Returns:
a connection to this database
static H2Database openOrCreateDatabase(File file, H2Database.CursorFactory factory)
Open a connection to the given database.
static H2Database openOrCreateDatabase(File file, H2Database.CursorFactory factory)
Open a connection to the given database. The database is created if it doesn't exist yet.
Parameters:
file - the database file
factory - the cursor factory
Returns:
a connection to this database
static H2Database openOrCreateDatabase(String path, H2Database.CursorFactory factory)
Open a connection to the given database.
static H2Database openOrCreateDatabase(String path, H2Database.CursorFactory factory)
Open a connection to the given database. The database is created if it doesn't exist yet.
Parameters:
path - the database file name
factory - the cursor factory
Returns:
a connection to this database
static int releaseMemory()
Try to release memory.
static int releaseMemory()
Try to release memory.
Returns:
TODO
static RuntimeException unsupported()
Create a new RuntimeException that says this feature is not supported.
static RuntimeException unsupported()
Create a new RuntimeException that says this feature is not supported.
Returns:
the runtime exception
void beginTransaction()
Start a transaction.
void beginTransaction()
Start a transaction.
void beginTransactionWithListener(H2TransactionListener transactionListener)
Start a transaction.
void beginTransactionWithListener(H2TransactionListener transactionListener)
Start a transaction.
Parameters:
transactionListener - the transaction listener to use
void close()
Close the connection.
void close()
Close the connection.
H2Statement compileStatement(String sql)
Prepare a statement.
H2Statement compileStatement(String sql)
Prepare a statement.
Parameters:
sql - the statement
Returns:
the prepared statement
int delete(String table, String whereClause, String[] whereArgs)
Delete a number of rows in this database.
int delete(String table, String whereClause, String[] whereArgs)
Delete a number of rows in this database.
Parameters:
table - the table
whereClause - the condition
whereArgs - the parameter values
Returns:
the number of rows deleted
void endTransaction()
End the transaction.
void endTransaction()
End the transaction.
void execSQL(String sql, Object[] bindArgs)
Execute the given statement.
void execSQL(String sql, Object[] bindArgs)
Execute the given statement.
Parameters:
sql - the statement
bindArgs - the parameter values
void execSQL(String sql)
Execute the given statement.
void execSQL(String sql)
Execute the given statement.
Parameters:
sql - the statement
long getMaximumSize()
Get the maximum size of the database file in bytes.
long getMaximumSize()
Get the maximum size of the database file in bytes.
Returns:
the maximum size
long getPageSize()
Get the page size of the database in bytes.
long getPageSize()
Get the page size of the database in bytes.
Returns:
the page size
String getPath()
Get the name of the database file.
String getPath()
Get the name of the database file.
Returns:
the database file name
Map getSyncedTables()
TODO
Map getSyncedTables()
TODO
Returns:
TODO
int getVersion()
Get the database version.
int getVersion()
Get the database version.
Returns:
the database version
boolean inTransaction()
Check if there is an open transaction.
boolean inTransaction()
Check if there is an open transaction.
Returns:
true if there is
long insert(String table, String nullColumnHack, ContentValues values)
Insert a row.
long insert(String table, String nullColumnHack, ContentValues values)
Insert a row.
Parameters:
table - the table
nullColumnHack - not used
values - the values
Returns:
TODO
long insertOrThrow(String table, String nullColumnHack, ContentValues values)
Try to insert a row.
long insertOrThrow(String table, String nullColumnHack, ContentValues values)
Try to insert a row.
Parameters:
table - the table
nullColumnHack - not used
values - the values
Returns:
TODO
long insertWithOnConflict(String table, String nullColumnHack, ContentValues initialValues, int conflictAlgorithm)
Try to insert a row, using the given conflict resolution option.
long insertWithOnConflict(String table, String nullColumnHack, ContentValues initialValues, int conflictAlgorithm)
Try to insert a row, using the given conflict resolution option.
Parameters:
table - the table
nullColumnHack - not used
initialValues - the values
conflictAlgorithm - what conflict resolution to use
Returns:
TODO
boolean isDbLockedByCurrentThread()
Check if the database is locked by the current thread.
boolean isDbLockedByCurrentThread()
Check if the database is locked by the current thread.
Returns:
true if it is
boolean isDbLockedByOtherThreads()
Check if the database is locked by a different thread.
boolean isDbLockedByOtherThreads()
Check if the database is locked by a different thread.
Returns:
true if it is
boolean isOpen()
Check if the connection is open.
boolean isOpen()
Check if the connection is open.
Returns:
true if it is
boolean isReadOnly()
Check if the connection is read-only.
boolean isReadOnly()
Check if the connection is read-only.
Returns:
true if it is
void markTableSyncable(String table, String deletedTable)
TODO
void markTableSyncable(String table, String deletedTable)
TODO
Parameters:
table - the table
deletedTable - TODO
void markTableSyncable(String table, String foreignKey, String updateTable)
TODO
void markTableSyncable(String table, String foreignKey, String updateTable)
TODO
Parameters:
table - the table
foreignKey - the foreign key
updateTable - TODO
boolean needUpgrade(int newVersion)
Check if an upgrade is required.
boolean needUpgrade(int newVersion)
Check if an upgrade is required.
Parameters:
newVersion - the new version
Returns:
true if the current version doesn't match
Cursor query(boolean distinct, String table, String[] columns, String selection, String[] selectionArgs, String groupBy, String having, String orderBy, String limit)
Execute the SELECT statement for the given parameters.
Cursor query(boolean distinct, String table, String[] columns, String selection, String[] selectionArgs, String groupBy, String having, String orderBy, String limit)
Execute the SELECT statement for the given parameters.
Parameters:
distinct - if only distinct rows should be returned
table - the table
columns - the list of columns
selection - TODO
selectionArgs - TODO
groupBy - the group by list or null
having - the having condition or null
orderBy - the order by list or null
limit - the limit or null
Returns:
the cursor
Cursor query(String table, String[] columns, String selection, String[] selectionArgs, String groupBy, String having, String orderBy)
Execute the SELECT statement for the given parameters.
Cursor query(String table, String[] columns, String selection, String[] selectionArgs, String groupBy, String having, String orderBy)
Execute the SELECT statement for the given parameters.
Parameters:
table - the table
columns - the list of columns
selection - TODO
selectionArgs - TODO
groupBy - the group by list or null
having - the having condition or null
orderBy - the order by list or null
Returns:
the cursor
Cursor query(String table, String[] columns, String selection, String[] selectionArgs, String groupBy, String having, String orderBy, String limit)
Execute the SELECT statement for the given parameters.
Cursor query(String table, String[] columns, String selection, String[] selectionArgs, String groupBy, String having, String orderBy, String limit)
Execute the SELECT statement for the given parameters.
Parameters:
table - the table
columns - the list of columns
selection - TODO
selectionArgs - TODO
groupBy - the group by list or null
having - the having condition or null
orderBy - the order by list or null
limit - the limit or null
Returns:
the cursor
Cursor queryWithFactory(H2Database.CursorFactory cursorFactory, boolean distinct, String table, String[] columns, String selection, String[] selectionArgs, String groupBy, String having, String orderBy, String limit)
Execute the SELECT statement for the given parameters.
Cursor queryWithFactory(H2Database.CursorFactory cursorFactory, boolean distinct, String table, String[] columns, String selection, String[] selectionArgs, String groupBy, String having, String orderBy, String limit)
Execute the SELECT statement for the given parameters.
Parameters:
cursorFactory - the cursor factory to use
distinct - if only distinct rows should be returned
table - the table
columns - the list of columns
selection - TODO
selectionArgs - TODO
groupBy - the group by list or null
having - the having condition or null
orderBy - the order by list or null
limit - the limit or null
Returns:
the cursor
Cursor rawQuery(String sql, String[] selectionArgs)
Execute the query.
Cursor rawQuery(String sql, String[] selectionArgs)
Execute the query.
Parameters:
sql - the SQL statement
selectionArgs - the parameter values
Returns:
the cursor
Cursor rawQueryWithFactory(H2Database.CursorFactory cursorFactory, String sql, String[] selectionArgs, String editTable)
Execute the query using the given cursor factory.
Cursor rawQueryWithFactory(H2Database.CursorFactory cursorFactory, String sql, String[] selectionArgs, String editTable)
Execute the query using the given cursor factory.
Parameters:
cursorFactory - the cursor factory
sql - the SQL statement
selectionArgs - the parameter values
editTable - TODO
Returns:
the cursor
long replace(String table, String nullColumnHack, ContentValues initialValues)
Replace an existing row in the database.
long replace(String table, String nullColumnHack, ContentValues initialValues)
Replace an existing row in the database.
Parameters:
table - the table
nullColumnHack - ignored
initialValues - the values
Returns:
TODO
long replaceOrThrow(String table, String nullColumnHack, ContentValues initialValues)
Try to replace an existing row in the database.
long replaceOrThrow(String table, String nullColumnHack, ContentValues initialValues)
Try to replace an existing row in the database.
Parameters:
table - the table
nullColumnHack - ignored
initialValues - the values
Returns:
TODO
void setLocale(Locale locale)
Set the locale.
void setLocale(Locale locale)
Set the locale.
Parameters:
locale - the new locale
void setLockingEnabled(boolean lockingEnabled)
Enable or disable thread safety.
void setLockingEnabled(boolean lockingEnabled)
Enable or disable thread safety.
Parameters:
lockingEnabled - the new value
long setMaximumSize(long numBytes)
Set the maximum database file size.
long setMaximumSize(long numBytes)
Set the maximum database file size.
Parameters:
numBytes - the file size in bytes
Returns:
the effective maximum size
void setPageSize(long numBytes)
Set the database page size.
void setPageSize(long numBytes)
Set the database page size. The value can not be changed once the database exists.
Parameters:
numBytes - the page size
void setTransactionSuccessful()
Mark the transaction as completed successfully.
void setTransactionSuccessful()
Mark the transaction as completed successfully.
void setVersion(int version)
Update the database version.
void setVersion(int version)
Update the database version.
Parameters:
version - the version
int update(String table, ContentValues values, String whereClause, String[] whereArgs)
Update one or multiple rows.
int update(String table, ContentValues values, String whereClause, String[] whereArgs)
Update one or multiple rows.
Parameters:
table - the table
values - the values
whereClause - the where condition
whereArgs - the parameter values
Returns:
the number of rows updated
int updateWithOnConflict(String table, ContentValues values, String whereClause, String[] whereArgs, int conflictAlgorithm)
Update one or multiple rows.
int updateWithOnConflict(String table, ContentValues values, String whereClause, String[] whereArgs, int conflictAlgorithm)
Update one or multiple rows.
Parameters:
table - the table
values - the values
whereClause - the where condition
whereArgs - the parameter values
conflictAlgorithm - the conflict resolution option
Returns:
the number of rows updated
boolean yieldIfContended()
TODO
boolean yieldIfContended()
TODO
Returns:
TODO
boolean yieldIfContendedSafely(long sleepAfterYieldDelay)
Temporarily pause the transaction.
boolean yieldIfContendedSafely(long sleepAfterYieldDelay)
Temporarily pause the transaction.
Parameters:
sleepAfterYieldDelay - TODO
Returns:
TODO
boolean yieldIfContendedSafely()
Temporarily pause the transaction.
boolean yieldIfContendedSafely()
Temporarily pause the transaction.
Returns:
TODO

Fields
static int CONFLICT_ABORT = 2
static int CONFLICT_FAIL = 3
static int CONFLICT_IGNORE = 4
static int CONFLICT_NONE = 0
static int CONFLICT_REPLACE = 5
static int CONFLICT_ROLLBACK = 1
static int CREATE_IF_NECESSARY = 268435456
static int NO_LOCALIZED_COLLATORS = 16
static int OPEN_READONLY = 1
static int OPEN_READWRITE = 0

CONFLICT_NONE = 0

When a conflict occurs, do nothing.

OPEN_READWRITE = 0

Open the database in read-write mode (default).

CONFLICT_ROLLBACK = 1

When a conflict occurs, the transaction is rolled back.

OPEN_READONLY = 1

Open the database in read-only mode.

NO_LOCALIZED_COLLATORS = 16

This flag has no effect.

CONFLICT_ABORT = 2

When a conflict occurs, abort the current statement, but don't roll back the transaction. This is the default value.

CREATE_IF_NECESSARY = 268435456

Create a new database if it doesn't exist.

CONFLICT_FAIL = 3

When a conflict occurs, return SQLITE_CONSTRAINT, but don't roll back the transaction.

CONFLICT_IGNORE = 4

When a conflict occurs, continue, but don't modify the conflicting row.

CONFLICT_REPLACE = 5

When a conflict occurs, the existing rows are replaced.