java.lang.Object | ||
↳ | android.database.sqlite.SQLiteClosable | |
↳ | android.database.sqlite.SQLiteDatabase |
Exposes methods to manage a SQLite database.
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.
Nested Classes | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
SQLiteDatabase.CursorFactory | Used to allow returning sub-classes of Cursor when calling query. |
Constants | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
int | CREATE_IF_NECESSARY | Flag for openDatabase(String, SQLiteDatabase.CursorFactory, int) to create the database file if it does not already exist. | |||||||||
int | NO_LOCALIZED_COLLATORS | Flag for openDatabase(String, SQLiteDatabase.CursorFactory, int) to open the database without support for localized collators. | |||||||||
int | OPEN_READONLY | Flag for openDatabase(String, SQLiteDatabase.CursorFactory, int) to open the database for reading only. | |||||||||
int | OPEN_READWRITE | Flag for openDatabase(String, SQLiteDatabase.CursorFactory, int) to open the database for reading and writing. If the disk is full, this may fail even before you actually write anything. | |||||||||
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. |
Public Methods | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
Begins a transaction.
| |||||||||||
Close the database.
| |||||||||||
Compiles an SQL statement into a reusable pre-compiled statement object.
| |||||||||||
Create a memory backed SQLite database.
| |||||||||||
Convenience method for deleting rows in the database.
| |||||||||||
End a transaction.
| |||||||||||
Execute a single SQL statement that is not a query.
| |||||||||||
Execute a single SQL statement that is not a query.
| |||||||||||
Finds the name of the first table, which is editable.
| |||||||||||
Returns the maximum size the database may grow to.
| |||||||||||
Returns the current database page size, in bytes.
| |||||||||||
Getter for the path to the database file.
| |||||||||||
Gets the database version.
| |||||||||||
return true if there is a transaction pending
| |||||||||||
Convenience method for inserting a row into the database.
| |||||||||||
Convenience method for inserting a row into the database.
| |||||||||||
Checks if the database lock is held by this thread.
| |||||||||||
Checks if the database is locked by another thread.
| |||||||||||
return whether the DB is opened as read only.
| |||||||||||
Mark this table as syncable.
| |||||||||||
Mark this table as syncable, with the _sync_dirty residing in another
table.
| |||||||||||
Open the database according to the flags OPEN_READWRITE
OPEN_READONLY CREATE_IF_NECESSARY and/or NO_LOCALIZED_COLLATORS.
| |||||||||||
Equivalent to openDatabase(path, factory, CREATE_IF_NECESSARY).
| |||||||||||
Equivalent to openDatabase(file.getPath(), factory, CREATE_IF_NECESSARY).
| |||||||||||
Query the given table, returning a Cursor over the result set.
| |||||||||||
Query the given URL, returning a Cursor over the result set.
| |||||||||||
Query the given table, returning a Cursor over the result set.
| |||||||||||
Query the given URL, returning a Cursor over the result set.
| |||||||||||
Runs the provided SQL and returns a Cursor over the result set.
| |||||||||||
Runs the provided SQL and returns a cursor over the result set.
| |||||||||||
Attempts to release memory that SQLite holds but does not require to
operate properly.
| |||||||||||
Convenience method for replacing a row in the database.
| |||||||||||
Convenience method for replacing a row in the database.
| |||||||||||
Sets the locale for this database.
| |||||||||||
Control whether or not the SQLiteDatabase is made thread-safe by using locks
around critical sections.
| |||||||||||
Sets the maximum size the database will grow to.
| |||||||||||
Sets the database page size.
| |||||||||||
Marks the current transaction as successful.
| |||||||||||
Sets the database version.
| |||||||||||
Convenience method for updating rows in the database.
| |||||||||||
This method is deprecated.
if the db is locked more than once (becuase of nested transactions) then the lock
will not be yielded. Use yieldIfContendedSafely instead.
| |||||||||||
Temporarily end the transaction to let other threads run.
|
Protected Methods | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
Is called before the object's memory is being reclaimed by the VM.
| |||||||||||
[Expand]
Inherited Methods | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
From class android.database.sqlite.SQLiteClosable
| |||||||||||
From class java.lang.Object
|
Flag for openDatabase(String, SQLiteDatabase.CursorFactory, int) to create the database file if it does not already exist.
Flag for openDatabase(String, SQLiteDatabase.CursorFactory, int) 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(Locale) will do nothing.
Flag for openDatabase(String, SQLiteDatabase.CursorFactory, int) to open the database for reading only. This is the only reliable way to open a database if the disk may be full.
Flag for openDatabase(String, SQLiteDatabase.CursorFactory, int) 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.
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. To avoid denial-of-service attacks the length of the LIKE or GLOB pattern is limited to SQLITE_MAX_LIKE_PATTERN_LENGTH bytes. The default value of this limit is 50000. A modern workstation can evaluate even a pathological LIKE or GLOB pattern of 50000 bytes relatively quickly. The denial of service problem only comes into play when the pattern length gets into millions of bytes. Nevertheless, since most useful LIKE or GLOB patterns are at most a few dozen bytes in length, paranoid application developers may want to reduce this parameter to something in the range of a few hundred if they know that external users are able to generate arbitrary patterns.
Begins a transaction. 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(); }
Close the database.
Compiles an SQL statement into a reusable pre-compiled statement object. The parameters are identical to execSQL(String). You may put ?s in the statement and fill in those values with bindString(int, String) and 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. |
---|
SQLException |
---|
Create a memory backed SQLite database. Its contents will be destroyed when the database is closed.
Sets the locale of the database to the the system's current locale. Call setLocale(Locale) if you would like something else.
factory | an optional factory class that is called to instantiate a cursor when query is called |
---|
Convenience method for deleting rows in the database.
table | the table to delete from |
---|---|
whereClause | the optional WHERE clause to apply when deleting. Passing null will delete all rows. |
End a transaction. See beginTransaction for notes about how to use this and when transactions are committed and rolled back.
Execute a single SQL statement that is not a query. For example, CREATE TABLE, DELETE, INSERT, etc. Multiple statements separated by ;s are not supported. it takes a write lock,
bindArgs | only byte[], String, Long and Double are supported in bindArgs. |
---|
SQLException | If the SQL string is invalid for some reason |
---|
Execute a single SQL statement that is not a query. For example, CREATE TABLE, DELETE, INSERT, etc. Multiple statements separated by ;s are not supported. it takes a write lock
SQLException | If the SQL string is invalid for some reason |
---|
Finds the name of the first table, which is editable.
tables | a list of tables |
---|
Returns the maximum size the database may grow to.
Returns the current database page size, in bytes.
Getter for the path to the database file.
Gets the database version.
return true if there is a transaction pending
Convenience method for inserting a row into the database.
table | the table to insert the row into |
---|---|
nullColumnHack | SQL doesn't allow inserting a completely empty row, so if initialValues is empty this column will explicitly be assigned a NULL value |
values | this map contains the initial column values for the row. The keys should be the column names and the values the column values |
Convenience method for inserting a row into the database.
table | the table to insert the row into |
---|---|
nullColumnHack | SQL doesn't allow inserting a completely empty row, so if initialValues is empty this column will explicitly be assigned a NULL value |
values | this map contains the initial column values for the row. The keys should be the column names and the values the column values |
SQLException | |
SQLException |
Checks if the database lock is held by this thread.
Checks if the database is locked by another thread. This is just an estimate, since this status can change at any time, including after the call is made but before the result has been acted upon.
return whether the DB is opened as read only.
Mark this table as syncable. When an update occurs in this table the _sync_dirty field will be set to ensure proper syncing operation.
table | the table to mark as syncable |
---|---|
deletedTable | The deleted table that corresponds to the syncable table |
Mark this table as syncable, with the _sync_dirty residing in another table. When an update occurs in this table the _sync_dirty field of the row in updateTable with the _id in foreignKey will be set to ensure proper syncing operation.
table | an update on this table will trigger a sync time removal |
---|---|
foreignKey | this is the column in table whose value is an _id in updateTable |
updateTable | this is the table that will have its _sync_dirty |
Open the database according to the flags 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(Locale) if you would like something else.
path | to database file to open and/or create |
---|---|
factory | an optional factory class that is called to instantiate a cursor when query is called, or null for default |
flags | to control database access mode |
SQLiteException | if the database cannot be opened |
---|
Equivalent to openDatabase(path, factory, CREATE_IF_NECESSARY).
Equivalent to openDatabase(file.getPath(), factory, CREATE_IF_NECESSARY).
Query the given table, returning a 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. |
Query the given URL, returning a 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. |
Query the given table, returning a 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. |
Query the given URL, returning a Cursor over the result set.
cursorFactory | the cursor factory to use, or null for the default factory |
---|---|
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. |
Runs the provided SQL and returns a Cursor over the result set.
sql | the SQL query. The SQL string must not be ; terminated |
---|---|
selectionArgs | 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. |
Runs the provided SQL and returns a cursor over the result set.
cursorFactory | the cursor factory to use, or null for the default factory |
---|---|
sql | the SQL query. The SQL string must not be ; terminated |
selectionArgs | 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 editable |
Attempts to release memory that SQLite holds but does not require to operate properly. Typically this memory will come from the page cache.
Convenience method for replacing a row in the database.
table | the table in which to replace the row |
---|---|
nullColumnHack | SQL doesn't allow inserting a completely empty row, so if initialValues is empty this row will explicitly be assigned a NULL value |
initialValues | this map contains the initial column values for the row. The key |
Convenience method for replacing a row in the database.
table | the table in which to replace the row |
---|---|
nullColumnHack | SQL doesn't allow inserting a completely empty row, so if initialValues is empty this row will explicitly be assigned a NULL value |
initialValues | this map contains the initial column values for the row. The key |
SQLException | |
SQLException |
Sets the locale for this database. Does nothing if this database has the NO_LOCALIZED_COLLATORS flag set or was opened read only.
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. |
---|
Control whether or not the SQLiteDatabase is made thread-safe by using locks around critical sections. This is pretty expensive, so if you know that your DB will only be used by a single thread then you should set this to false. The default is true.
lockingEnabled | set to true to enable locks, false otherwise |
---|
Sets the maximum size the database will grow to. The maximum size cannot be set below the current size.
numBytes | the maximum database size, in bytes |
---|
Sets the database page size. The page size must be a power of two. This method does not work if any data has been written to the database file, and must be called right after the database has been created.
numBytes | the database page size, in bytes |
---|
Marks the current transaction as successful. Do not do any more database work between calling this and calling endTransaction. Do as little non-database work as possible in that situation too. If any errors are encountered between this and endTransaction the transaction will still be committed.
IllegalStateException | if the current thread is not in a transaction or the transaction is already marked as successful. |
---|
Sets the database version.
version | the new database version |
---|
Convenience method for updating rows in the database.
table | the table to update in |
---|---|
values | 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. |
This method is deprecated.if the db is locked more than once (becuase of nested transactions) then the lock will not be yielded. Use yieldIfContendedSafely instead.
Temporarily end the transaction to let other threads run. The transaction is assumed to be successful so far. Do not call setTransactionSuccessful before calling this. When this returns a new transaction will have been created but not marked as successful.
Temporarily end the transaction to let other threads run. The transaction is assumed to be successful so far. Do not call setTransactionSuccessful before calling this. When this returns a new transaction will have been created but not marked as successful. This assumes that there are no nested transactions (beginTransaction has only been called once) and will through an exception if that is not the case.
Is called before the object's memory is being reclaimed by the VM. This can only happen once the VM has detected, during a run of the garbage collector, that the object is no longer reachable by any thread of the running application.
The method can be used to free system resources or perform other cleanup
before the object is garbage collected. The default implementation of the
method is empty, which is also expected by the VM, but subclasses can
override finalize()
as required. Uncaught exceptions which are
thrown during the execution of this method cause it to terminate
immediately but are otherwise ignored.
Note that the VM does guarantee that finalize()
is called at most
once for any object, but it doesn't guarantee when (if at all) finalize()
will be called. For example, object B's finalize()
can delay the execution of object A's finalize()
method and
therefore it can delay the reclamation of A's memory. To be safe, use a
ReferenceQueue, because it provides more control
over the way the VM deals with references during garbage collection.