public class SQLiteDatabase extends SQLiteClosable
SQLiteDatabase has methods to create, delete, execute SQL commands, and perform other common database management tasks.
See the Notepad sample application in the SDK for an example of creating and managing a database.
Database names must be unique within an application, not across all applications.
In addition to SQLite's default BINARY
collator, Android supplies
two more, LOCALIZED
, which changes with the system's current locale
if you wire it up correctly (XXX a link needed!), and UNICODE
, which
is the Unicode Collation Algorithm and not tailored to the current locale.
Modifier and Type | Class and Description |
---|---|
static interface |
SQLiteDatabase.CursorFactory
Used to allow returning sub-classes of
Cursor when calling query. |
Modifier and Type | Field and Description |
---|---|
static int |
CONFLICT_ABORT
When a constraint violation occurs,no ROLLBACK is executed
so changes from prior commands within the same transaction
are preserved.
|
static int |
CONFLICT_FAIL
When a constraint violation occurs, the command aborts with a return
code SQLITE_CONSTRAINT.
|
static int |
CONFLICT_IGNORE
When a constraint violation occurs, the one row that contains
the constraint violation is not inserted or changed.
|
static int |
CONFLICT_NONE
use the following when no conflict action is specified.
|
static int |
CONFLICT_REPLACE
When a UNIQUE constraint violation occurs, the pre-existing rows that
are causing the constraint violation are removed prior to inserting
or updating the current row.
|
static int |
CONFLICT_ROLLBACK
When a constraint violation occurs, an immediate ROLLBACK occurs,
thus ending the current transaction, and the command aborts with a
return code of SQLITE_CONSTRAINT.
|
static int |
CREATE_IF_NECESSARY
Flag for
openDatabase(java.lang.String, java.lang.String, net.sqlcipher.database.SQLiteDatabase.CursorFactory, int, net.sqlcipher.database.SQLiteDatabaseHook) to create the database file if it does not already exist. |
static int |
MAX_SQL_CACHE_SIZE |
static int |
NO_LOCALIZED_COLLATORS
Flag for
openDatabase(java.lang.String, java.lang.String, net.sqlcipher.database.SQLiteDatabase.CursorFactory, int, net.sqlcipher.database.SQLiteDatabaseHook) to open the database without support for localized collators. |
static int |
OPEN_READONLY
Flag for
openDatabase(java.lang.String, java.lang.String, net.sqlcipher.database.SQLiteDatabase.CursorFactory, int, net.sqlcipher.database.SQLiteDatabaseHook) to open the database for reading only. |
static int |
OPEN_READWRITE
Flag for
openDatabase(java.lang.String, java.lang.String, net.sqlcipher.database.SQLiteDatabase.CursorFactory, int, net.sqlcipher.database.SQLiteDatabaseHook) to open the database for reading and writing. |
static int |
SQLITE_MAX_LIKE_PATTERN_LENGTH
Maximum Length Of A LIKE Or GLOB Pattern
The pattern matching algorithm used in the default LIKE and GLOB implementation
of SQLite can exhibit O(N^2) performance (where N is the number of characters in
the pattern) for certain pathological cases.
|
Constructor and Description |
---|
SQLiteDatabase(java.lang.String path,
char[] password,
SQLiteDatabase.CursorFactory factory,
int flags) |
SQLiteDatabase(java.lang.String path,
char[] password,
SQLiteDatabase.CursorFactory factory,
int flags,
SQLiteDatabaseHook databaseHook)
Private constructor.
|
Modifier and Type | Method and Description |
---|---|
void |
beginTransaction()
Begins a transaction.
|
void |
beginTransactionWithListener(SQLiteTransactionListener transactionListener)
Begins a transaction.
|
void |
changePassword(char[] password) |
void |
changePassword(java.lang.String password) |
void |
close()
Close the database.
|
SQLiteStatement |
compileStatement(java.lang.String sql)
Compiles an SQL statement into a reusable pre-compiled statement object.
|
static SQLiteDatabase |
create(SQLiteDatabase.CursorFactory factory,
char[] password) |
static SQLiteDatabase |
create(SQLiteDatabase.CursorFactory factory,
java.lang.String password)
Create a memory backed SQLite database.
|
int |
delete(java.lang.String table,
java.lang.String whereClause,
java.lang.String[] whereArgs)
Convenience method for deleting rows in the database.
|
void |
endTransaction()
End a transaction.
|
void |
execSQL(java.lang.String sql)
Execute a single SQL statement that is not a query.
|
void |
execSQL(java.lang.String sql,
java.lang.Object[] bindArgs)
Execute a single SQL statement that is not a query.
|
protected void |
finalize() |
static java.lang.String |
findEditTable(java.lang.String tables)
Finds the name of the first table, which is editable.
|
long |
getMaximumSize()
Returns the maximum size the database may grow to.
|
int |
getMaxSqlCacheSize()
return the current maxCacheSqlCacheSize
|
long |
getPageSize()
Returns the current database page size, in bytes.
|
java.lang.String |
getPath()
Getter for the path to the database file.
|
java.util.Map<java.lang.String,java.lang.String> |
getSyncedTables() |
int |
getVersion()
Gets the database version.
|
long |
insert(java.lang.String table,
java.lang.String nullColumnHack,
ContentValues values)
Convenience method for inserting a row into the database.
|
long |
insertOrThrow(java.lang.String table,
java.lang.String nullColumnHack,
ContentValues values)
Convenience method for inserting a row into the database.
|
long |
insertWithOnConflict(java.lang.String table,
java.lang.String nullColumnHack,
ContentValues initialValues,
int conflictAlgorithm)
General method for inserting a row into the database.
|
boolean |
inTransaction()
return true if there is a transaction pending
|
boolean |
isDbLockedByCurrentThread()
Checks if the database lock is held by this thread.
|
boolean |
isDbLockedByOtherThreads()
Checks if the database is locked by another thread.
|
boolean |
isInCompiledSqlCache(java.lang.String sql)
returns true if the given sql is cached in compiled-sql cache.
|
boolean |
isOpen() |
boolean |
isReadOnly()
return whether the DB is opened as read only.
|
static void |
loadLibs(Context context) |
static void |
loadLibs(Context context,
java.io.File workingDir) |
void |
markTableSyncable(java.lang.String table,
java.lang.String deletedTable)
Mark this table as syncable.
|
void |
markTableSyncable(java.lang.String table,
java.lang.String foreignKey,
java.lang.String updateTable)
Mark this table as syncable, with the _sync_dirty residing in another
table.
|
boolean |
needUpgrade(int newVersion) |
protected void |
onAllReferencesReleased() |
static SQLiteDatabase |
openDatabase(java.lang.String path,
char[] password,
SQLiteDatabase.CursorFactory factory,
int flags) |
static SQLiteDatabase |
openDatabase(java.lang.String path,
char[] password,
SQLiteDatabase.CursorFactory factory,
int flags,
SQLiteDatabaseHook databaseHook) |
static SQLiteDatabase |
openDatabase(java.lang.String path,
java.lang.String password,
SQLiteDatabase.CursorFactory factory,
int flags) |
static SQLiteDatabase |
openDatabase(java.lang.String path,
java.lang.String password,
SQLiteDatabase.CursorFactory factory,
int flags,
SQLiteDatabaseHook databaseHook)
Open the database according to the flags
OPEN_READWRITE
OPEN_READONLY CREATE_IF_NECESSARY and/or NO_LOCALIZED_COLLATORS . |
static SQLiteDatabase |
openOrCreateDatabase(java.io.File file,
java.lang.String password,
SQLiteDatabase.CursorFactory factory)
Equivalent to openDatabase(file.getPath(), factory, CREATE_IF_NECESSARY).
|
static SQLiteDatabase |
openOrCreateDatabase(java.io.File file,
java.lang.String password,
SQLiteDatabase.CursorFactory factory,
SQLiteDatabaseHook databaseHook) |
static SQLiteDatabase |
openOrCreateDatabase(java.lang.String path,
char[] password,
SQLiteDatabase.CursorFactory factory) |
static SQLiteDatabase |
openOrCreateDatabase(java.lang.String path,
char[] password,
SQLiteDatabase.CursorFactory factory,
SQLiteDatabaseHook databaseHook) |
static SQLiteDatabase |
openOrCreateDatabase(java.lang.String path,
java.lang.String password,
SQLiteDatabase.CursorFactory factory)
Equivalent to openDatabase(path, factory, CREATE_IF_NECESSARY).
|
static SQLiteDatabase |
openOrCreateDatabase(java.lang.String path,
java.lang.String password,
SQLiteDatabase.CursorFactory factory,
SQLiteDatabaseHook databaseHook) |
void |
purgeFromCompiledSqlCache(java.lang.String sql)
purges the given sql from the compiled-sql cache.
|
Cursor |
query(boolean distinct,
java.lang.String table,
java.lang.String[] columns,
java.lang.String selection,
java.lang.String[] selectionArgs,
java.lang.String groupBy,
java.lang.String having,
java.lang.String orderBy,
java.lang.String limit)
Query the given URL, returning a
Cursor over the result set. |
Cursor |
query(java.lang.String table,
java.lang.String[] columns,
java.lang.String selection,
java.lang.String[] selectionArgs,
java.lang.String groupBy,
java.lang.String having,
java.lang.String orderBy)
Query the given table, returning a
Cursor over the result set. |
Cursor |
query(java.lang.String table,
java.lang.String[] columns,
java.lang.String selection,
java.lang.String[] selectionArgs,
java.lang.String groupBy,
java.lang.String having,
java.lang.String orderBy,
java.lang.String limit)
Query the given table, returning a
Cursor over the result set. |
Cursor |
queryWithFactory(SQLiteDatabase.CursorFactory cursorFactory,
boolean distinct,
java.lang.String table,
java.lang.String[] columns,
java.lang.String selection,
java.lang.String[] selectionArgs,
java.lang.String groupBy,
java.lang.String having,
java.lang.String orderBy,
java.lang.String limit)
Query the given URL, returning a
Cursor over the result set. |
void |
rawExecSQL(java.lang.String sql) |
Cursor |
rawQuery(java.lang.String sql,
java.lang.String[] selectionArgs)
Runs the provided SQL and returns a
Cursor over the result set. |
Cursor |
rawQuery(java.lang.String sql,
java.lang.String[] selectionArgs,
int initialRead,
int maxRead)
Runs the provided SQL and returns a cursor over the result set.
|
Cursor |
rawQueryWithFactory(SQLiteDatabase.CursorFactory cursorFactory,
java.lang.String sql,
java.lang.String[] selectionArgs,
java.lang.String editTable)
Runs the provided SQL and returns a cursor over the result set.
|
static int |
releaseMemory()
Attempts to release memory that SQLite holds but does not require to
operate properly.
|
long |
replace(java.lang.String table,
java.lang.String nullColumnHack,
ContentValues initialValues)
Convenience method for replacing a row in the database.
|
long |
replaceOrThrow(java.lang.String table,
java.lang.String nullColumnHack,
ContentValues initialValues)
Convenience method for replacing a row in the database.
|
void |
resetCompiledSqlCache()
remove everything from the compiled sql cache
|
static void |
setICURoot(java.lang.String path)
Sets the root directory to search for the ICU data file
|
void |
setLocale(java.util.Locale locale)
Sets the locale for this database.
|
void |
setLockingEnabled(boolean lockingEnabled)
Control whether or not the SQLiteDatabase is made thread-safe by using locks
around critical sections.
|
long |
setMaximumSize(long numBytes)
Sets the maximum size the database will grow to.
|
void |
setMaxSqlCacheSize(int cacheSize)
set the max size of the compiled sql cache for this database after purging the cache.
|
void |
setPageSize(long numBytes)
Sets the database page size.
|
void |
setTransactionSuccessful()
Marks the current transaction as successful.
|
void |
setVersion(int version)
Sets the database version.
|
int |
status(int operation,
boolean reset) |
int |
update(java.lang.String table,
ContentValues values,
java.lang.String whereClause,
java.lang.String[] whereArgs)
Convenience method for updating rows in the database.
|
int |
updateWithOnConflict(java.lang.String table,
ContentValues values,
java.lang.String whereClause,
java.lang.String[] whereArgs,
int conflictAlgorithm)
Convenience method for updating rows in the database.
|
boolean |
yieldIfContended()
Deprecated.
if the db is locked more than once (becuase of nested transactions) then the lock
will not be yielded. Use yieldIfContendedSafely instead.
|
boolean |
yieldIfContendedSafely()
Temporarily end the transaction to let other threads run.
|
boolean |
yieldIfContendedSafely(long sleepAfterYieldDelay)
Temporarily end the transaction to let other threads run.
|
acquireReference, onAllReferencesReleasedFromContainer, releaseReference, releaseReferenceFromContainer
public static final int CONFLICT_ROLLBACK
public static final int CONFLICT_ABORT
public static final int CONFLICT_FAIL
public static final int CONFLICT_IGNORE
public static final int CONFLICT_REPLACE
public static final int CONFLICT_NONE
public static final int SQLITE_MAX_LIKE_PATTERN_LENGTH
public static final int OPEN_READWRITE
openDatabase(java.lang.String, java.lang.String, net.sqlcipher.database.SQLiteDatabase.CursorFactory, int, net.sqlcipher.database.SQLiteDatabaseHook)
to open the database for reading and writing.
If the disk is full, this may fail even before you actually write anything.
Note that the value of this flag is 0, so it is the default.public static final int OPEN_READONLY
openDatabase(java.lang.String, java.lang.String, net.sqlcipher.database.SQLiteDatabase.CursorFactory, int, net.sqlcipher.database.SQLiteDatabaseHook)
to open the database for reading only.
This is the only reliable way to open a database if the disk may be full.public static final int NO_LOCALIZED_COLLATORS
openDatabase(java.lang.String, java.lang.String, net.sqlcipher.database.SQLiteDatabase.CursorFactory, int, net.sqlcipher.database.SQLiteDatabaseHook)
to open the database without support for localized collators.
This causes the collator LOCALIZED
not to be created.
You must be consistent when using this flag to use the setting the database was
created with. If this is set, setLocale(java.util.Locale)
will do nothing.public static final int CREATE_IF_NECESSARY
openDatabase(java.lang.String, java.lang.String, net.sqlcipher.database.SQLiteDatabase.CursorFactory, int, net.sqlcipher.database.SQLiteDatabaseHook)
to create the database file if it does not already exist.public static final int MAX_SQL_CACHE_SIZE
public SQLiteDatabase(java.lang.String path, char[] password, SQLiteDatabase.CursorFactory factory, int flags)
public SQLiteDatabase(java.lang.String path, char[] password, SQLiteDatabase.CursorFactory factory, int flags, SQLiteDatabaseHook databaseHook)
create(net.sqlcipher.database.SQLiteDatabase.CursorFactory, java.lang.String)
and openDatabase(java.lang.String, java.lang.String, net.sqlcipher.database.SQLiteDatabase.CursorFactory, int, net.sqlcipher.database.SQLiteDatabaseHook)
.path
- The full path to the databasefactory
- The factory to use when creating cursors, may be NULL.flags
- 0 or NO_LOCALIZED_COLLATORS
. If the database file already
exists, mFlags will be updated appropriately.public int status(int operation, boolean reset)
public void changePassword(java.lang.String password) throws SQLiteException
SQLiteException
public void changePassword(char[] password) throws SQLiteException
SQLiteException
public static void loadLibs(Context context)
public static void loadLibs(Context context, java.io.File workingDir)
protected void onAllReferencesReleased()
onAllReferencesReleased
in class SQLiteClosable
public static int releaseMemory()
public void setLockingEnabled(boolean lockingEnabled)
lockingEnabled
- set to true to enable locks, false otherwisepublic void beginTransaction()
Here is the standard idiom for transactions:
db.beginTransaction(); try { ... db.setTransactionSuccessful(); } finally { db.endTransaction(); }
public void beginTransactionWithListener(SQLiteTransactionListener transactionListener)
Here is the standard idiom for transactions:
db.beginTransactionWithListener(listener); try { ... db.setTransactionSuccessful(); } finally { db.endTransaction(); }
transactionListener
- listener that should be notified when the transaction begins,
commits, or is rolled back, either explicitly or by a call to
yieldIfContendedSafely()
.public void endTransaction()
public void setTransactionSuccessful()
java.lang.IllegalStateException
- if the current thread is not in a transaction or the
transaction is already marked as successful.public boolean inTransaction()
public boolean isDbLockedByCurrentThread()
public boolean isDbLockedByOtherThreads()
@Deprecated public boolean yieldIfContended()
public boolean yieldIfContendedSafely()
public boolean yieldIfContendedSafely(long sleepAfterYieldDelay)
sleepAfterYieldDelay
- if > 0, sleep this long before starting a new transaction if
the lock was actually yielded. This will allow other background threads to make some
more progress than they would if we started the transaction immediately.public java.util.Map<java.lang.String,java.lang.String> getSyncedTables()
public static SQLiteDatabase openDatabase(java.lang.String path, java.lang.String password, SQLiteDatabase.CursorFactory factory, int flags, SQLiteDatabaseHook databaseHook)
OPEN_READWRITE
OPEN_READONLY
CREATE_IF_NECESSARY
and/or NO_LOCALIZED_COLLATORS
.
Sets the locale of the database to the the system's current locale.
Call setLocale(java.util.Locale)
if you would like something else.
path
- to database file to open and/or createfactory
- an optional factory class that is called to instantiate a
cursor when query is called, or null for defaultflags
- to control database access modeSQLiteException
- if the database cannot be openedpublic static SQLiteDatabase openDatabase(java.lang.String path, char[] password, SQLiteDatabase.CursorFactory factory, int flags, SQLiteDatabaseHook databaseHook)
public static SQLiteDatabase openOrCreateDatabase(java.io.File file, java.lang.String password, SQLiteDatabase.CursorFactory factory, SQLiteDatabaseHook databaseHook)
public static SQLiteDatabase openOrCreateDatabase(java.lang.String path, java.lang.String password, SQLiteDatabase.CursorFactory factory, SQLiteDatabaseHook databaseHook)
public static SQLiteDatabase openOrCreateDatabase(java.lang.String path, char[] password, SQLiteDatabase.CursorFactory factory, SQLiteDatabaseHook databaseHook)
public static SQLiteDatabase openOrCreateDatabase(java.io.File file, java.lang.String password, SQLiteDatabase.CursorFactory factory)
public static SQLiteDatabase openOrCreateDatabase(java.lang.String path, java.lang.String password, SQLiteDatabase.CursorFactory factory)
public static SQLiteDatabase openOrCreateDatabase(java.lang.String path, char[] password, SQLiteDatabase.CursorFactory factory)
public static SQLiteDatabase openDatabase(java.lang.String path, java.lang.String password, SQLiteDatabase.CursorFactory factory, int flags)
public static SQLiteDatabase openDatabase(java.lang.String path, char[] password, SQLiteDatabase.CursorFactory factory, int flags)
public static SQLiteDatabase create(SQLiteDatabase.CursorFactory factory, java.lang.String password)
Sets the locale of the database to the the system's current locale.
Call setLocale(java.util.Locale)
if you would like something else.
factory
- an optional factory class that is called to instantiate a
cursor when query is calledpublic static SQLiteDatabase create(SQLiteDatabase.CursorFactory factory, char[] password)
public void close()
public int getVersion()
public void setVersion(int version)
version
- the new database versionpublic long getMaximumSize()
public long setMaximumSize(long numBytes)
numBytes
- the maximum database size, in bytespublic long getPageSize()
public void setPageSize(long numBytes)
numBytes
- the database page size, in bytespublic void markTableSyncable(java.lang.String table, java.lang.String deletedTable)
table
- the table to mark as syncabledeletedTable
- The deleted table that corresponds to the
syncable tablepublic void markTableSyncable(java.lang.String table, java.lang.String foreignKey, java.lang.String updateTable)
table
- an update on this table will trigger a sync time removalforeignKey
- this is the column in table whose value is an _id in
updateTableupdateTable
- this is the table that will have its _sync_dirtypublic static java.lang.String findEditTable(java.lang.String tables)
tables
- a list of tablespublic SQLiteStatement compileStatement(java.lang.String sql) throws SQLException
execSQL(String)
. You may put ?s in the
statement and fill in those values with SQLiteProgram.bindString(int, java.lang.String)
and SQLiteProgram.bindLong(int, long)
each time you want to run the
statement. Statements may not return result sets larger than 1x1.sql
- The raw SQL statement, may contain ? for unknown values to be
bound later.SQLiteStatement
object. Note that
SQLiteStatement
s are not synchronized, see the documentation for more details.SQLException
public Cursor query(boolean distinct, java.lang.String table, java.lang.String[] columns, java.lang.String selection, java.lang.String[] selectionArgs, java.lang.String groupBy, java.lang.String having, java.lang.String orderBy, java.lang.String limit)
Cursor
over the result set.distinct
- true if you want each row to be unique, false otherwise.table
- The table name to compile the query against.columns
- A list of which columns to return. Passing null will
return all columns, which is discouraged to prevent reading
data from storage that isn't going to be used.selection
- A filter declaring which rows to return, formatted as an
SQL WHERE clause (excluding the WHERE itself). Passing null
will return all rows for the given table.selectionArgs
- You may include ?s in selection, which will be
replaced by the values from selectionArgs, in order that they
appear in the selection. The values will be bound as Strings.groupBy
- A filter declaring how to group rows, formatted as an SQL
GROUP BY clause (excluding the GROUP BY itself). Passing null
will cause the rows to not be grouped.having
- A filter declare which row groups to include in the cursor,
if row grouping is being used, formatted as an SQL HAVING
clause (excluding the HAVING itself). Passing null will cause
all row groups to be included, and is required when row
grouping is not being used.orderBy
- How to order the rows, formatted as an SQL ORDER BY clause
(excluding the ORDER BY itself). Passing null will use the
default sort order, which may be unordered.limit
- Limits the number of rows returned by the query,
formatted as LIMIT clause. Passing null denotes no LIMIT clause.Cursor
object, which is positioned before the first entry. Note that
Cursor
s are not synchronized, see the documentation for more details.Cursor
public Cursor queryWithFactory(SQLiteDatabase.CursorFactory cursorFactory, boolean distinct, java.lang.String table, java.lang.String[] columns, java.lang.String selection, java.lang.String[] selectionArgs, java.lang.String groupBy, java.lang.String having, java.lang.String orderBy, java.lang.String limit)
Cursor
over the result set.cursorFactory
- the cursor factory to use, or null for the default factorydistinct
- true if you want each row to be unique, false otherwise.table
- The table name to compile the query against.columns
- A list of which columns to return. Passing null will
return all columns, which is discouraged to prevent reading
data from storage that isn't going to be used.selection
- A filter declaring which rows to return, formatted as an
SQL WHERE clause (excluding the WHERE itself). Passing null
will return all rows for the given table.selectionArgs
- You may include ?s in selection, which will be
replaced by the values from selectionArgs, in order that they
appear in the selection. The values will be bound as Strings.groupBy
- A filter declaring how to group rows, formatted as an SQL
GROUP BY clause (excluding the GROUP BY itself). Passing null
will cause the rows to not be grouped.having
- A filter declare which row groups to include in the cursor,
if row grouping is being used, formatted as an SQL HAVING
clause (excluding the HAVING itself). Passing null will cause
all row groups to be included, and is required when row
grouping is not being used.orderBy
- How to order the rows, formatted as an SQL ORDER BY clause
(excluding the ORDER BY itself). Passing null will use the
default sort order, which may be unordered.limit
- Limits the number of rows returned by the query,
formatted as LIMIT clause. Passing null denotes no LIMIT clause.Cursor
object, which is positioned before the first entry. Note that
Cursor
s are not synchronized, see the documentation for more details.Cursor
public Cursor query(java.lang.String table, java.lang.String[] columns, java.lang.String selection, java.lang.String[] selectionArgs, java.lang.String groupBy, java.lang.String having, java.lang.String orderBy)
Cursor
over the result set.table
- The table name to compile the query against.columns
- A list of which columns to return. Passing null will
return all columns, which is discouraged to prevent reading
data from storage that isn't going to be used.selection
- A filter declaring which rows to return, formatted as an
SQL WHERE clause (excluding the WHERE itself). Passing null
will return all rows for the given table.selectionArgs
- You may include ?s in selection, which will be
replaced by the values from selectionArgs, in order that they
appear in the selection. The values will be bound as Strings.groupBy
- A filter declaring how to group rows, formatted as an SQL
GROUP BY clause (excluding the GROUP BY itself). Passing null
will cause the rows to not be grouped.having
- A filter declare which row groups to include in the cursor,
if row grouping is being used, formatted as an SQL HAVING
clause (excluding the HAVING itself). Passing null will cause
all row groups to be included, and is required when row
grouping is not being used.orderBy
- How to order the rows, formatted as an SQL ORDER BY clause
(excluding the ORDER BY itself). Passing null will use the
default sort order, which may be unordered.Cursor
object, which is positioned before the first entry. Note that
Cursor
s are not synchronized, see the documentation for more details.Cursor
public Cursor query(java.lang.String table, java.lang.String[] columns, java.lang.String selection, java.lang.String[] selectionArgs, java.lang.String groupBy, java.lang.String having, java.lang.String orderBy, java.lang.String limit)
Cursor
over the result set.table
- The table name to compile the query against.columns
- A list of which columns to return. Passing null will
return all columns, which is discouraged to prevent reading
data from storage that isn't going to be used.selection
- A filter declaring which rows to return, formatted as an
SQL WHERE clause (excluding the WHERE itself). Passing null
will return all rows for the given table.selectionArgs
- You may include ?s in selection, which will be
replaced by the values from selectionArgs, in order that they
appear in the selection. The values will be bound as Strings.groupBy
- A filter declaring how to group rows, formatted as an SQL
GROUP BY clause (excluding the GROUP BY itself). Passing null
will cause the rows to not be grouped.having
- A filter declare which row groups to include in the cursor,
if row grouping is being used, formatted as an SQL HAVING
clause (excluding the HAVING itself). Passing null will cause
all row groups to be included, and is required when row
grouping is not being used.orderBy
- How to order the rows, formatted as an SQL ORDER BY clause
(excluding the ORDER BY itself). Passing null will use the
default sort order, which may be unordered.limit
- Limits the number of rows returned by the query,
formatted as LIMIT clause. Passing null denotes no LIMIT clause.Cursor
object, which is positioned before the first entry. Note that
Cursor
s are not synchronized, see the documentation for more details.Cursor
public Cursor rawQuery(java.lang.String sql, java.lang.String[] selectionArgs)
Cursor
over the result set.sql
- the SQL query. The SQL string must not be ; terminatedselectionArgs
- You may include ?s in where clause in the query,
which will be replaced by the values from selectionArgs. The
values will be bound as Strings.Cursor
object, which is positioned before the first entry. Note that
Cursor
s are not synchronized, see the documentation for more details.public Cursor rawQueryWithFactory(SQLiteDatabase.CursorFactory cursorFactory, java.lang.String sql, java.lang.String[] selectionArgs, java.lang.String editTable)
cursorFactory
- the cursor factory to use, or null for the default factorysql
- the SQL query. The SQL string must not be ; terminatedselectionArgs
- You may include ?s in where clause in the query,
which will be replaced by the values from selectionArgs. The
values will be bound as Strings.editTable
- the name of the first table, which is editableCursor
object, which is positioned before the first entry. Note that
Cursor
s are not synchronized, see the documentation for more details.public Cursor rawQuery(java.lang.String sql, java.lang.String[] selectionArgs, int initialRead, int maxRead)
sql
- the SQL query. The SQL string must not be ; terminatedselectionArgs
- You may include ?s in where clause in the query,
which will be replaced by the values from selectionArgs. The
values will be bound as Strings.initialRead
- set the initial count of items to read from the cursormaxRead
- set the count of items to read on each iteration after the firstCursor
object, which is positioned before the first entry. Note that
Cursor
s are not synchronized, see the documentation for more details.
This work is incomplete and not fully tested or reviewed, so currently
hidden.public long insert(java.lang.String table, java.lang.String nullColumnHack, ContentValues values)
table
- the table to insert the row intonullColumnHack
- SQL doesn't allow inserting a completely empty row,
so if initialValues is empty this column will explicitly be
assigned a NULL valuevalues
- this map contains the initial column values for the
row. The keys should be the column names and the values the
column valuespublic long insertOrThrow(java.lang.String table, java.lang.String nullColumnHack, ContentValues values) throws SQLException
table
- the table to insert the row intonullColumnHack
- SQL doesn't allow inserting a completely empty row,
so if initialValues is empty this column will explicitly be
assigned a NULL valuevalues
- this map contains the initial column values for the
row. The keys should be the column names and the values the
column valuesSQLException
public long replace(java.lang.String table, java.lang.String nullColumnHack, ContentValues initialValues)
table
- the table in which to replace the rownullColumnHack
- SQL doesn't allow inserting a completely empty row,
so if initialValues is empty this row will explicitly be
assigned a NULL valueinitialValues
- this map contains the initial column values for
the row. The keypublic long replaceOrThrow(java.lang.String table, java.lang.String nullColumnHack, ContentValues initialValues) throws SQLException
table
- the table in which to replace the rownullColumnHack
- SQL doesn't allow inserting a completely empty row,
so if initialValues is empty this row will explicitly be
assigned a NULL valueinitialValues
- this map contains the initial column values for
the row. The keySQLException
public long insertWithOnConflict(java.lang.String table, java.lang.String nullColumnHack, ContentValues initialValues, int conflictAlgorithm)
table
- the table to insert the row intonullColumnHack
- SQL doesn't allow inserting a completely empty row,
so if initialValues is empty this column will explicitly be
assigned a NULL valueinitialValues
- this map contains the initial column values for the
row. The keys should be the column names and the values the
column valuesconflictAlgorithm
- for insert conflict resolverCONFLICT_IGNORE
OR -1 if any errorpublic int delete(java.lang.String table, java.lang.String whereClause, java.lang.String[] whereArgs)
table
- the table to delete fromwhereClause
- the optional WHERE clause to apply when deleting.
Passing null will delete all rows.public int update(java.lang.String table, ContentValues values, java.lang.String whereClause, java.lang.String[] whereArgs)
table
- the table to update invalues
- a map from column names to new column values. null is a
valid value that will be translated to NULL.whereClause
- the optional WHERE clause to apply when updating.
Passing null will update all rows.public int updateWithOnConflict(java.lang.String table, ContentValues values, java.lang.String whereClause, java.lang.String[] whereArgs, int conflictAlgorithm)
table
- the table to update invalues
- a map from column names to new column values. null is a
valid value that will be translated to NULL.whereClause
- the optional WHERE clause to apply when updating.
Passing null will update all rows.conflictAlgorithm
- for update conflict resolverpublic void execSQL(java.lang.String sql) throws SQLException
SQLException
- If the SQL string is invalid for some reasonpublic void rawExecSQL(java.lang.String sql)
public void execSQL(java.lang.String sql, java.lang.Object[] bindArgs) throws SQLException
sql
- bindArgs
- only byte[], String, Long and Double are supported in bindArgs.SQLException
- If the SQL string is invalid for some reasonprotected void finalize()
finalize
in class java.lang.Object
public boolean isReadOnly()
public boolean isOpen()
public boolean needUpgrade(int newVersion)
public final java.lang.String getPath()
public void setLocale(java.util.Locale locale)
SQLException
- if the locale could not be set. The most common reason
for this is that there is no collator available for the locale you requested.
In this case the database remains unchanged.public boolean isInCompiledSqlCache(java.lang.String sql)
public void purgeFromCompiledSqlCache(java.lang.String sql)
public void resetCompiledSqlCache()
public int getMaxSqlCacheSize()
public void setMaxSqlCacheSize(int cacheSize)
cacheSize
- the size of the cache. can be (0 to MAX_SQL_CACHE_SIZE)java.lang.IllegalStateException
- if input cacheSize > MAX_SQL_CACHE_SIZE or < 0 or
< the value set with previous setMaxSqlCacheSize() call.public static void setICURoot(java.lang.String path)