|
Build 1.1_r1 (from source) | |||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
java.lang.Objectandroid.database.sqlite.SQLiteClosable
android.database.sqlite.SQLiteDatabase
public class 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 Class Summary | |
|---|---|
static class |
SQLiteDatabase.ConflictAlgorithm
Algorithms used in ON CONFLICT clause http://www.sqlite.org/lang_conflict.html |
static interface |
SQLiteDatabase.CursorFactory
Used to allow returning sub-classes of Cursor when calling query. |
| Field Summary | |
|---|---|
static int |
CREATE_IF_NECESSARY
Flag for openDatabase(java.lang.String, android.database.sqlite.SQLiteDatabase.CursorFactory, int) to create the database file if it does not already exist. |
(package private) int |
mNativeHandle
Used by native code, do not rename |
(package private) int |
mTempTableSequence
Used to make temp table names unique |
static int |
NO_LOCALIZED_COLLATORS
Flag for openDatabase(java.lang.String, android.database.sqlite.SQLiteDatabase.CursorFactory, int) to open the database without support for localized collators. |
static int |
OPEN_READONLY
Flag for openDatabase(java.lang.String, android.database.sqlite.SQLiteDatabase.CursorFactory, int) to open the database for reading only. |
static int |
OPEN_READWRITE
Flag for openDatabase(java.lang.String, android.database.sqlite.SQLiteDatabase.CursorFactory, int) 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. |
| Method Summary | |
|---|---|
(package private) void |
addSQLiteClosable(SQLiteClosable closable)
|
void |
beginTransaction()
Begins a transaction. |
void |
close()
Close the database. |
SQLiteStatement |
compileStatement(String sql)
Compiles an SQL statement into a reusable pre-compiled statement object. |
static SQLiteDatabase |
create(SQLiteDatabase.CursorFactory factory)
Create a memory backed SQLite database. |
int |
delete(String table,
String whereClause,
String[] whereArgs)
Convenience method for deleting rows in the database. |
void |
endTransaction()
End a transaction. |
void |
execSQL(String sql)
Execute a single SQL statement that is not a query. |
void |
execSQL(String sql,
Object[] bindArgs)
Execute a single SQL statement that is not a query. |
protected void |
finalize()
Is called before the object's memory is being reclaimed by the VM. |
static String |
findEditTable(String tables)
Finds the name of the first table, which is editable. |
long |
getMaximumSize()
Returns the maximum size the database may grow to. |
long |
getPageSize()
Returns the maximum size the database may grow to. |
String |
getPath()
Getter for the path to the database file. |
Map<String,String> |
getSyncedTables()
|
int |
getVersion()
Gets the database version. |
long |
insert(String table,
String nullColumnHack,
ContentValues values)
Convenience method for inserting a row into the database. |
long |
insertOrThrow(String table,
String nullColumnHack,
ContentValues values)
Convenience method for inserting a row into the database. |
long |
insertWithOnConflict(String table,
String nullColumnHack,
ContentValues initialValues,
SQLiteDatabase.ConflictAlgorithm algorithm)
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 |
isOpen()
|
boolean |
isReadOnly()
return whether the DB is opened as read only. |
(package private) int |
lastChangeCount()
Returns the number of changes made in the last statement executed. |
(package private) long |
lastInsertRow()
Returns the row ID of the last row inserted into the database. |
(package private) void |
lock()
Locks the database for exclusive access. |
void |
markTableSyncable(String table,
String deletedTable)
Mark this table as syncable. |
void |
markTableSyncable(String table,
String foreignKey,
String updateTable)
Mark this table as syncable, with the _sync_dirty residing in another table. |
(package private) void |
native_execSQL(String sql)
Native call to execute a raw SQL statement. |
(package private) void |
native_setLocale(String loc,
int flags)
Native call to set the locale. |
boolean |
needUpgrade(int newVersion)
|
protected void |
onAllReferencesReleased()
|
(package private) void |
onCorruption()
|
static SQLiteDatabase |
openDatabase(String path,
SQLiteDatabase.CursorFactory factory,
int flags)
Open the database according to the flags OPEN_READWRITE
OPEN_READONLY CREATE_IF_NECESSARY and/or NO_LOCALIZED_COLLATORS. |
static SQLiteDatabase |
openOrCreateDatabase(File file,
SQLiteDatabase.CursorFactory factory)
Equivalent to openDatabase(file.getPath(), factory, CREATE_IF_NECESSARY). |
static SQLiteDatabase |
openOrCreateDatabase(String path,
SQLiteDatabase.CursorFactory factory)
Equivalent to openDatabase(path, factory, CREATE_IF_NECESSARY). |
Cursor |
query(boolean distinct,
String table,
String[] columns,
String selection,
String[] selectionArgs,
String groupBy,
String having,
String orderBy,
String limit)
Query the given URL, returning a Cursor over the result set. |
Cursor |
query(String table,
String[] columns,
String selection,
String[] selectionArgs,
String groupBy,
String having,
String orderBy)
Query the given table, returning a Cursor over the result set. |
Cursor |
query(String table,
String[] columns,
String selection,
String[] selectionArgs,
String groupBy,
String having,
String orderBy,
String limit)
Query the given table, returning a Cursor over the result set. |
Cursor |
queryWithFactory(SQLiteDatabase.CursorFactory cursorFactory,
boolean distinct,
String table,
String[] columns,
String selection,
String[] selectionArgs,
String groupBy,
String having,
String orderBy,
String limit)
Query the given URL, returning a Cursor over the result set. |
Cursor |
rawQuery(String sql,
String[] selectionArgs)
Runs the provided SQL and returns a Cursor over the result set. |
Cursor |
rawQuery(String sql,
String[] selectionArgs,
int initialRead,
int maxRead)
Runs the provided SQL and returns a cursor over the result set. |
Cursor |
rawQueryWithFactory(SQLiteDatabase.CursorFactory cursorFactory,
String sql,
String[] selectionArgs,
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. |
(package private) void |
removeSQLiteClosable(SQLiteClosable closable)
|
long |
replace(String table,
String nullColumnHack,
ContentValues initialValues)
Convenience method for replacing a row in the database. |
long |
replaceOrThrow(String table,
String nullColumnHack,
ContentValues initialValues)
Convenience method for replacing a row in the database. |
(package private) void |
rowUpdated(String table,
long rowId)
Call for each row that is updated in a cursor. |
void |
setLocale(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 |
setPageSize(long numBytes)
Sets the database page size. |
void |
setTransactionSuccessful()
Marks the current transaction as successful. |
void |
setVersion(int version)
Sets the database version. |
(package private) void |
unlock()
Releases the database lock. |
int |
update(String table,
ContentValues values,
String whereClause,
String[] whereArgs)
Convenience method for updating rows in the database. |
int |
updateWithOnConflict(String table,
ContentValues values,
String whereClause,
String[] whereArgs,
SQLiteDatabase.ConflictAlgorithm algorithm)
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. |
| Methods inherited from class android.database.sqlite.SQLiteClosable |
|---|
acquireReference, onAllReferencesReleasedFromContainer, releaseReference, releaseReferenceFromContainer |
| Methods inherited from class java.lang.Object |
|---|
clone, equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
| Field Detail |
|---|
public static final int SQLITE_MAX_LIKE_PATTERN_LENGTH
public static final int OPEN_READWRITE
openDatabase(java.lang.String, android.database.sqlite.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.
public static final int OPEN_READONLY
openDatabase(java.lang.String, android.database.sqlite.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.
public static final int NO_LOCALIZED_COLLATORS
openDatabase(java.lang.String, android.database.sqlite.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(java.util.Locale) will do nothing.
public static final int CREATE_IF_NECESSARY
openDatabase(java.lang.String, android.database.sqlite.SQLiteDatabase.CursorFactory, int) to create the database file if it does not already exist.
int mNativeHandle
int mTempTableSequence
| Method Detail |
|---|
void addSQLiteClosable(SQLiteClosable closable)
closable - void removeSQLiteClosable(SQLiteClosable closable)
protected void onAllReferencesReleased()
onAllReferencesReleased in class SQLiteClosablepublic static int releaseMemory()
public void setLockingEnabled(boolean lockingEnabled)
lockingEnabled - set to true to enable locks, false otherwisevoid onCorruption()
void lock()
unlock()void unlock()
unlock()public void beginTransaction()
Here is the standard idiom for transactions:
db.beginTransaction();
try {
...
db.setTransactionSuccessful();
} finally {
db.endTransaction();
}
public void endTransaction()
public void setTransactionSuccessful()
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()
public boolean yieldIfContended()
public boolean yieldIfContendedSafely()
public Map<String,String> getSyncedTables()
public static SQLiteDatabase openDatabase(String path,
SQLiteDatabase.CursorFactory factory,
int 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(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 mode
SQLiteException - if the database cannot be opened
public static SQLiteDatabase openOrCreateDatabase(File file,
SQLiteDatabase.CursorFactory factory)
public static SQLiteDatabase openOrCreateDatabase(String path,
SQLiteDatabase.CursorFactory factory)
public static SQLiteDatabase create(SQLiteDatabase.CursorFactory factory)
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 called
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 bytes
public long getPageSize()
public void setPageSize(long numBytes)
numBytes - the database page size, in bytes
public void markTableSyncable(String table,
String deletedTable)
table - the table to mark as syncabledeletedTable - The deleted table that corresponds to the
syncable table
public void markTableSyncable(String table,
String foreignKey,
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_dirty
void rowUpdated(String table,
long rowId)
table - the table the row is inrowId - the row ID of the updated rowpublic static String findEditTable(String tables)
tables - a list of tables
public SQLiteStatement compileStatement(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.
SQLException
public Cursor query(boolean distinct,
String table,
String[] columns,
String selection,
String[] selectionArgs,
String groupBy,
String having,
String orderBy,
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
public Cursor queryWithFactory(SQLiteDatabase.CursorFactory cursorFactory,
boolean distinct,
String table,
String[] columns,
String selection,
String[] selectionArgs,
String groupBy,
String having,
String orderBy,
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
public Cursor query(String table,
String[] columns,
String selection,
String[] selectionArgs,
String groupBy,
String having,
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 entryCursor
public Cursor query(String table,
String[] columns,
String selection,
String[] selectionArgs,
String groupBy,
String having,
String orderBy,
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 entryCursor
public Cursor rawQuery(String sql,
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
public Cursor rawQueryWithFactory(SQLiteDatabase.CursorFactory cursorFactory,
String sql,
String[] selectionArgs,
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 editable
Cursor object, which is positioned before the first entry
public Cursor rawQuery(String sql,
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 first
Cursor object, which is positioned before the first entry
public long insert(String table,
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 values
public long insertOrThrow(String table,
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 values
SQLException
public long replace(String table,
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 key
public long replaceOrThrow(String table,
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 key
SQLException
public long insertWithOnConflict(String table,
String nullColumnHack,
ContentValues initialValues,
SQLiteDatabase.ConflictAlgorithm algorithm)
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 valuesalgorithm - SQLiteDatabase.ConflictAlgorithm for insert conflict resolver
public int delete(String table,
String whereClause,
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(String table,
ContentValues values,
String whereClause,
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(String table,
ContentValues values,
String whereClause,
String[] whereArgs,
SQLiteDatabase.ConflictAlgorithm algorithm)
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.algorithm - SQLiteDatabase.ConflictAlgorithm for update conflict resolver
public void execSQL(String sql)
throws SQLException
SQLException - If the SQL string is invalid for some reason
public void execSQL(String sql,
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()
Object
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.
finalize in class Objectpublic boolean isReadOnly()
public boolean isOpen()
public boolean needUpgrade(int newVersion)
public final String getPath()
public void setLocale(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.
void native_execSQL(String sql)
throws SQLException
mLock must be held
when calling this method.
sql - The raw SQL string
SQLException
void native_setLocale(String loc,
int flags)
mLock must be held when calling
this method.
SQLExceptionlong lastInsertRow()
int lastChangeCount()
|
Build 1.1_r1 (from source) | |||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||