-
public class DatabaseUtils
Static utility methods for dealing with databases and Cursors.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description public class
DatabaseUtils.InsertHelper
This class allows users to do multiple inserts into a table usingthe same statement.
This class is not thread-safe.
-
Field Summary
Fields Modifier and Type Field Description public final static int
STATEMENT_SELECT
public final static int
STATEMENT_UPDATE
public final static int
STATEMENT_ATTACH
public final static int
STATEMENT_BEGIN
public final static int
STATEMENT_COMMIT
public final static int
STATEMENT_ABORT
public final static int
STATEMENT_PRAGMA
public final static int
STATEMENT_DDL
public final static int
STATEMENT_UNPREPARED
public final static int
STATEMENT_OTHER
-
Method Summary
Modifier and Type Method Description final static void
writeExceptionToParcel(Parcel reply, Exception e)
Special function for writing an exception result at the header ofa parcel, to be used when returning an exception from a transaction. static void
bindObjectToProgram(SQLiteProgram prog, int index, Object value)
Binds the given Object to the given SQLiteProgram using the propertyping. static int
getTypeOfObject(Object obj)
Returns data type of the given object's value. static void
cursorFillWindow(Cursor cursor, int position, CursorWindow window)
Fills the specified cursor window by iterating over the contents of the cursor.The window is filled until the cursor is exhausted or the window runs outof space.The original position of the cursor is left unchanged by this operation. static void
appendEscapedSQLString(StringBuilder sb, String sqlString)
Appends an SQL string to the given StringBuilder, including the openingand closing single quotes. static String
sqlEscapeString(String value)
SQL-escape a string. final static void
appendValueToSql(StringBuilder sql, Object value)
Appends an Object to an SQL string with the proper escaping, etc. static String
concatenateWhere(String a, String b)
Concatenates two SQL WHERE clauses, handling empty or null values. static String
getCollationKey(String name)
return the collation key static String
getHexCollationKey(String name)
return the collation key in hex format static void
dumpCursor(Cursor cursor)
Prints the contents of a Cursor to System.out. static void
dumpCursor(Cursor cursor, PrintStream stream)
Prints the contents of a Cursor to a PrintSteam. static void
dumpCursor(Cursor cursor, StringBuilder sb)
Prints the contents of a Cursor to a StringBuilder. static String
dumpCursorToString(Cursor cursor)
Prints the contents of a Cursor to a String. static void
dumpCurrentRow(Cursor cursor)
Prints the contents of a Cursor's current row to System.out. static void
dumpCurrentRow(Cursor cursor, PrintStream stream)
Prints the contents of a Cursor's current row to a PrintSteam. static void
dumpCurrentRow(Cursor cursor, StringBuilder sb)
Prints the contents of a Cursor's current row to a StringBuilder. static String
dumpCurrentRowToString(Cursor cursor)
Dump the contents of a Cursor's current row to a String. static void
cursorStringToContentValues(Cursor cursor, String field, ContentValues values)
Reads a String out of a field in a Cursor and writes it to a Map. static void
cursorStringToInsertHelper(Cursor cursor, String field, DatabaseUtils.InsertHelper inserter, int index)
Reads a String out of a field in a Cursor and writes it to an InsertHelper. static void
cursorStringToContentValues(Cursor cursor, String field, ContentValues values, String key)
Reads a String out of a field in a Cursor and writes it to a Map. static void
cursorIntToContentValues(Cursor cursor, String field, ContentValues values)
Reads an Integer out of a field in a Cursor and writes it to a Map. static void
cursorIntToContentValues(Cursor cursor, String field, ContentValues values, String key)
Reads a Integer out of a field in a Cursor and writes it to a Map. static void
cursorLongToContentValues(Cursor cursor, String field, ContentValues values)
Reads a Long out of a field in a Cursor and writes it to a Map. static void
cursorLongToContentValues(Cursor cursor, String field, ContentValues values, String key)
Reads a Long out of a field in a Cursor and writes it to a Map. static void
cursorDoubleToCursorValues(Cursor cursor, String field, ContentValues values)
Reads a Double out of a field in a Cursor and writes it to a Map. static void
cursorDoubleToContentValues(Cursor cursor, String field, ContentValues values, String key)
Reads a Double out of a field in a Cursor and writes it to a Map. static void
cursorRowToContentValues(Cursor cursor, ContentValues values)
Read the entire contents of a cursor row and store them in a ContentValues. static int
cursorPickFillWindowStartPosition(int cursorPosition, int cursorWindowCapacity)
Picks a start position for fillWindow such that thewindow will contain the requested row and a useful range of rowsaround it.When the data set is too large to fit in a cursor window, seeking thecursor can become a very expensive operation since we have to run thequery again when we move outside the bounds of the current window.We try to choose a start position for the cursor window such that1/3 of the window's capacity is used to hold rows before the requestedposition and 2/3 of the window's capacity is used to hold rows after therequested position. static long
queryNumEntries(SQLiteDatabase db, String table)
Query the table for the number of rows in the table. static long
queryNumEntries(SQLiteDatabase db, String table, String selection)
Query the table for the number of rows in the table. static long
queryNumEntries(SQLiteDatabase db, String table, String selection, Array<String> selectionArgs)
Query the table for the number of rows in the table. static boolean
queryIsEmpty(SQLiteDatabase db, String table)
Query the table to check whether a table is empty or not static long
longForQuery(SQLiteDatabase db, String query, Array<String> selectionArgs)
Utility method to run the query on the db and return the value in thefirst column of the first row. static long
longForQuery(SQLiteStatement prog, Array<String> selectionArgs)
Utility method to run the pre-compiled query and return the value in thefirst column of the first row. static String
stringForQuery(SQLiteDatabase db, String query, Array<String> selectionArgs)
Utility method to run the query on the db and return the value in thefirst column of the first row. static String
stringForQuery(SQLiteStatement prog, Array<String> selectionArgs)
Utility method to run the pre-compiled query and return the value in thefirst column of the first row. static ParcelFileDescriptor
blobFileDescriptorForQuery(SQLiteDatabase db, String query, Array<String> selectionArgs)
Utility method to run the query on the db and return the blob value in thefirst column of the first row. static ParcelFileDescriptor
blobFileDescriptorForQuery(SQLiteStatement prog, Array<String> selectionArgs)
Utility method to run the pre-compiled query and return the blob value in thefirst column of the first row. static void
cursorStringToContentValuesIfPresent(Cursor cursor, ContentValues values, String column)
Reads a String out of a column in a Cursor and writes it to a ContentValues.Adds nothing to the ContentValues if the column isn't present or if its value is null. static void
cursorLongToContentValuesIfPresent(Cursor cursor, ContentValues values, String column)
Reads a Long out of a column in a Cursor and writes it to a ContentValues.Adds nothing to the ContentValues if the column isn't present or if its value is null. static void
cursorShortToContentValuesIfPresent(Cursor cursor, ContentValues values, String column)
Reads a Short out of a column in a Cursor and writes it to a ContentValues.Adds nothing to the ContentValues if the column isn't present or if its value is null. static void
cursorIntToContentValuesIfPresent(Cursor cursor, ContentValues values, String column)
Reads a Integer out of a column in a Cursor and writes it to a ContentValues.Adds nothing to the ContentValues if the column isn't present or if its value is null. static void
cursorFloatToContentValuesIfPresent(Cursor cursor, ContentValues values, String column)
Reads a Float out of a column in a Cursor and writes it to a ContentValues.Adds nothing to the ContentValues if the column isn't present or if its value is null. static void
cursorDoubleToContentValuesIfPresent(Cursor cursor, ContentValues values, String column)
Reads a Double out of a column in a Cursor and writes it to a ContentValues.Adds nothing to the ContentValues if the column isn't present or if its value is null. static void
createDbFromSqlStatements(Context context, String dbName, int dbVersion, String sqlStatements)
Creates a db and populates it with the sql statements in sqlStatements. static int
getSqlStatementType(String sql)
Returns one of the following which represent the type of the given SQL statement. static Array<String>
appendSelectionArgs(Array<String> originalValues, Array<String> newValues)
Appends one set of selection args to another. static int
findRowIdColumnIndex(Array<String> columnNames)
Returns column index of "_id" column, or -1 if not found. -
-
Method Detail
-
writeExceptionToParcel
final static void writeExceptionToParcel(Parcel reply, Exception e)
Special function for writing an exception result at the header ofa parcel, to be used when returning an exception from a transaction.exception will be re-thrown by the function in another process
- Parameters:
reply
- Parcel to write toe
- The Exception to be written.
-
bindObjectToProgram
static void bindObjectToProgram(SQLiteProgram prog, int index, Object value)
Binds the given Object to the given SQLiteProgram using the propertyping. For example, bind numbers as longs/doubles, and everything elseas a string by call toString() on it.
- Parameters:
prog
- the program to bind the object toindex
- the 1-based index to bind atvalue
- the value to bind
-
getTypeOfObject
static int getTypeOfObject(Object obj)
Returns data type of the given object's value.
Returned values are
- Parameters:
obj
- the object whose value type is to be returned
-
cursorFillWindow
static void cursorFillWindow(Cursor cursor, int position, CursorWindow window)
Fills the specified cursor window by iterating over the contents of the cursor.The window is filled until the cursor is exhausted or the window runs outof space.The original position of the cursor is left unchanged by this operation.
- Parameters:
cursor
- The cursor that contains the data to put in the window.position
- The start position for filling the window.window
- The window to fill.
-
appendEscapedSQLString
static void appendEscapedSQLString(StringBuilder sb, String sqlString)
Appends an SQL string to the given StringBuilder, including the openingand closing single quotes. Any single quotes internal to sqlString willbe escaped.This method is deprecated because we want to encourage everyoneto use the "?" binding form. However, when implementing aContentProvider, one may want to add WHERE clauses that werenot provided by the caller. Since "?" is a positional form,using it in this case could break the caller because theindexes would be shifted to accomodate the ContentProvider'sinternal bindings. In that case, it may be necessary toconstruct a WHERE clause manually. This method is useful forthose cases.
- Parameters:
sb
- the StringBuilder that the SQL string will be appended tosqlString
- the raw string to be appended, which may contain singlequotes
-
sqlEscapeString
static String sqlEscapeString(String value)
SQL-escape a string.
-
appendValueToSql
final static void appendValueToSql(StringBuilder sql, Object value)
Appends an Object to an SQL string with the proper escaping, etc.
-
concatenateWhere
static String concatenateWhere(String a, String b)
Concatenates two SQL WHERE clauses, handling empty or null values.
-
getCollationKey
static String getCollationKey(String name)
return the collation key
-
getHexCollationKey
static String getHexCollationKey(String name)
return the collation key in hex format
-
dumpCursor
static void dumpCursor(Cursor cursor)
Prints the contents of a Cursor to System.out. The position is restoredafter printing.
- Parameters:
cursor
- the cursor to print
-
dumpCursor
static void dumpCursor(Cursor cursor, PrintStream stream)
Prints the contents of a Cursor to a PrintSteam. The position is restoredafter printing.
- Parameters:
cursor
- the cursor to printstream
- the stream to print to
-
dumpCursor
static void dumpCursor(Cursor cursor, StringBuilder sb)
Prints the contents of a Cursor to a StringBuilder. The positionis restored after printing.
- Parameters:
cursor
- the cursor to printsb
- the StringBuilder to print to
-
dumpCursorToString
static String dumpCursorToString(Cursor cursor)
Prints the contents of a Cursor to a String. The position is restoredafter printing.
- Parameters:
cursor
- the cursor to print
-
dumpCurrentRow
static void dumpCurrentRow(Cursor cursor)
Prints the contents of a Cursor's current row to System.out.
- Parameters:
cursor
- the cursor to print from
-
dumpCurrentRow
static void dumpCurrentRow(Cursor cursor, PrintStream stream)
Prints the contents of a Cursor's current row to a PrintSteam.
- Parameters:
cursor
- the cursor to printstream
- the stream to print to
-
dumpCurrentRow
static void dumpCurrentRow(Cursor cursor, StringBuilder sb)
Prints the contents of a Cursor's current row to a StringBuilder.
- Parameters:
cursor
- the cursor to printsb
- the StringBuilder to print to
-
dumpCurrentRowToString
static String dumpCurrentRowToString(Cursor cursor)
Dump the contents of a Cursor's current row to a String.
- Parameters:
cursor
- the cursor to print
-
cursorStringToContentValues
static void cursorStringToContentValues(Cursor cursor, String field, ContentValues values)
Reads a String out of a field in a Cursor and writes it to a Map.
- Parameters:
cursor
- The cursor to read fromfield
- The TEXT field to readvalues
- The ContentValues to put the value into, with the field as the key
-
cursorStringToInsertHelper
static void cursorStringToInsertHelper(Cursor cursor, String field, DatabaseUtils.InsertHelper inserter, int index)
Reads a String out of a field in a Cursor and writes it to an InsertHelper.
- Parameters:
cursor
- The cursor to read fromfield
- The TEXT field to readinserter
- The InsertHelper to bind intoindex
- the index of the bind entry in the InsertHelper
-
cursorStringToContentValues
static void cursorStringToContentValues(Cursor cursor, String field, ContentValues values, String key)
Reads a String out of a field in a Cursor and writes it to a Map.
- Parameters:
cursor
- The cursor to read fromfield
- The TEXT field to readvalues
- The ContentValues to put the value into, with the field as the keykey
- The key to store the value with in the map
-
cursorIntToContentValues
static void cursorIntToContentValues(Cursor cursor, String field, ContentValues values)
Reads an Integer out of a field in a Cursor and writes it to a Map.
- Parameters:
cursor
- The cursor to read fromfield
- The INTEGER field to readvalues
- The ContentValues to put the value into, with the field as the key
-
cursorIntToContentValues
static void cursorIntToContentValues(Cursor cursor, String field, ContentValues values, String key)
Reads a Integer out of a field in a Cursor and writes it to a Map.
- Parameters:
cursor
- The cursor to read fromfield
- The INTEGER field to readvalues
- The ContentValues to put the value into, with the field as the keykey
- The key to store the value with in the map
-
cursorLongToContentValues
static void cursorLongToContentValues(Cursor cursor, String field, ContentValues values)
Reads a Long out of a field in a Cursor and writes it to a Map.
- Parameters:
cursor
- The cursor to read fromfield
- The INTEGER field to readvalues
- The ContentValues to put the value into, with the field as the key
-
cursorLongToContentValues
static void cursorLongToContentValues(Cursor cursor, String field, ContentValues values, String key)
Reads a Long out of a field in a Cursor and writes it to a Map.
- Parameters:
cursor
- The cursor to read fromfield
- The INTEGER field to readvalues
- The ContentValues to put the value intokey
- The key to store the value with in the map
-
cursorDoubleToCursorValues
static void cursorDoubleToCursorValues(Cursor cursor, String field, ContentValues values)
Reads a Double out of a field in a Cursor and writes it to a Map.
- Parameters:
cursor
- The cursor to read fromfield
- The REAL field to readvalues
- The ContentValues to put the value into
-
cursorDoubleToContentValues
static void cursorDoubleToContentValues(Cursor cursor, String field, ContentValues values, String key)
Reads a Double out of a field in a Cursor and writes it to a Map.
- Parameters:
cursor
- The cursor to read fromfield
- The REAL field to readvalues
- The ContentValues to put the value intokey
- The key to store the value with in the map
-
cursorRowToContentValues
static void cursorRowToContentValues(Cursor cursor, ContentValues values)
Read the entire contents of a cursor row and store them in a ContentValues.
- Parameters:
cursor
- the cursor to read from.values
- the ContentValues to put the row into.
-
cursorPickFillWindowStartPosition
static int cursorPickFillWindowStartPosition(int cursorPosition, int cursorWindowCapacity)
Picks a start position for fillWindow such that thewindow will contain the requested row and a useful range of rowsaround it.When the data set is too large to fit in a cursor window, seeking thecursor can become a very expensive operation since we have to run thequery again when we move outside the bounds of the current window.We try to choose a start position for the cursor window such that1/3 of the window's capacity is used to hold rows before the requestedposition and 2/3 of the window's capacity is used to hold rows after therequested position.
- Parameters:
cursorPosition
- The row index of the row we want to get.cursorWindowCapacity
- The estimated number of rows that can fit ina cursor window, or 0 if unknown.
-
queryNumEntries
static long queryNumEntries(SQLiteDatabase db, String table)
Query the table for the number of rows in the table.
- Parameters:
db
- the database the table is intable
- the name of the table to query
-
queryNumEntries
static long queryNumEntries(SQLiteDatabase db, String table, String selection)
Query the table for the number of rows in the table.
- Parameters:
db
- the database the table is intable
- the name of the table to queryselection
- A filter declaring which rows to return,formatted as an SQL WHERE clause (excluding the WHERE itself).
-
queryNumEntries
static long queryNumEntries(SQLiteDatabase db, String table, String selection, Array<String> selectionArgs)
Query the table for the number of rows in the table.
- Parameters:
db
- the database the table is intable
- the name of the table to queryselection
- A filter declaring which rows to return,formatted as an SQL WHERE clause (excluding the WHERE itself).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.
-
queryIsEmpty
static boolean queryIsEmpty(SQLiteDatabase db, String table)
Query the table to check whether a table is empty or not
- Parameters:
db
- the database the table is intable
- the name of the table to query
-
longForQuery
static long longForQuery(SQLiteDatabase db, String query, Array<String> selectionArgs)
Utility method to run the query on the db and return the value in thefirst column of the first row.
-
longForQuery
static long longForQuery(SQLiteStatement prog, Array<String> selectionArgs)
Utility method to run the pre-compiled query and return the value in thefirst column of the first row.
-
stringForQuery
static String stringForQuery(SQLiteDatabase db, String query, Array<String> selectionArgs)
Utility method to run the query on the db and return the value in thefirst column of the first row.
-
stringForQuery
static String stringForQuery(SQLiteStatement prog, Array<String> selectionArgs)
Utility method to run the pre-compiled query and return the value in thefirst column of the first row.
-
blobFileDescriptorForQuery
static ParcelFileDescriptor blobFileDescriptorForQuery(SQLiteDatabase db, String query, Array<String> selectionArgs)
Utility method to run the query on the db and return the blob value in thefirst column of the first row.
-
blobFileDescriptorForQuery
static ParcelFileDescriptor blobFileDescriptorForQuery(SQLiteStatement prog, Array<String> selectionArgs)
Utility method to run the pre-compiled query and return the blob value in thefirst column of the first row.
-
cursorStringToContentValuesIfPresent
static void cursorStringToContentValuesIfPresent(Cursor cursor, ContentValues values, String column)
Reads a String out of a column in a Cursor and writes it to a ContentValues.Adds nothing to the ContentValues if the column isn't present or if its value is null.
- Parameters:
cursor
- The cursor to read fromvalues
- The ContentValues to put the value intocolumn
- The column to read
-
cursorLongToContentValuesIfPresent
static void cursorLongToContentValuesIfPresent(Cursor cursor, ContentValues values, String column)
Reads a Long out of a column in a Cursor and writes it to a ContentValues.Adds nothing to the ContentValues if the column isn't present or if its value is null.
- Parameters:
cursor
- The cursor to read fromvalues
- The ContentValues to put the value intocolumn
- The column to read
-
cursorShortToContentValuesIfPresent
static void cursorShortToContentValuesIfPresent(Cursor cursor, ContentValues values, String column)
Reads a Short out of a column in a Cursor and writes it to a ContentValues.Adds nothing to the ContentValues if the column isn't present or if its value is null.
- Parameters:
cursor
- The cursor to read fromvalues
- The ContentValues to put the value intocolumn
- The column to read
-
cursorIntToContentValuesIfPresent
static void cursorIntToContentValuesIfPresent(Cursor cursor, ContentValues values, String column)
Reads a Integer out of a column in a Cursor and writes it to a ContentValues.Adds nothing to the ContentValues if the column isn't present or if its value is null.
- Parameters:
cursor
- The cursor to read fromvalues
- The ContentValues to put the value intocolumn
- The column to read
-
cursorFloatToContentValuesIfPresent
static void cursorFloatToContentValuesIfPresent(Cursor cursor, ContentValues values, String column)
Reads a Float out of a column in a Cursor and writes it to a ContentValues.Adds nothing to the ContentValues if the column isn't present or if its value is null.
- Parameters:
cursor
- The cursor to read fromvalues
- The ContentValues to put the value intocolumn
- The column to read
-
cursorDoubleToContentValuesIfPresent
static void cursorDoubleToContentValuesIfPresent(Cursor cursor, ContentValues values, String column)
Reads a Double out of a column in a Cursor and writes it to a ContentValues.Adds nothing to the ContentValues if the column isn't present or if its value is null.
- Parameters:
cursor
- The cursor to read fromvalues
- The ContentValues to put the value intocolumn
- The column to read
-
createDbFromSqlStatements
static void createDbFromSqlStatements(Context context, String dbName, int dbVersion, String sqlStatements)
Creates a db and populates it with the sql statements in sqlStatements.
- Parameters:
context
- the context to use to create the dbdbName
- the name of the db to createdbVersion
- the version to set on the dbsqlStatements
- the statements to use to populate the db.
-
getSqlStatementType
static int getSqlStatementType(String sql)
Returns one of the following which represent the type of the given SQL statement.
- Parameters:
sql
- the SQL statement whose type is returned by this method
-
appendSelectionArgs
static Array<String> appendSelectionArgs(Array<String> originalValues, Array<String> newValues)
Appends one set of selection args to another. This is useful when adding a selectionargument to a user provided set.
-
findRowIdColumnIndex
static int findRowIdColumnIndex(Array<String> columnNames)
Returns column index of "_id" column, or -1 if not found.
-
-
-
-