public class

DatabaseUtils

extends Object
java.lang.Object
   ↳ android.database.DatabaseUtils

Class Overview

Static utility methods for dealing with databases and Cursors.

Summary

Nested Classes
class DatabaseUtils.InsertHelper This class allows users to do multiple inserts into a table but compile the SQL insert statement only once, which may increase performance. 
Public Constructors
DatabaseUtils()
Public Methods
static void appendEscapedSQLString(StringBuilder sb, String sqlString)
Appends an SQL string to the given StringBuilder, including the opening and closing single quotes.
final static void appendValueToSql(StringBuilder sql, Object value)
Appends an Object to an SQL string with the proper escaping, etc.
static void bindObjectToProgram(SQLiteProgram prog, int index, Object value)
Binds the given Object to the given SQLiteProgram using the proper typing.
static void createDbFromSqlStatements(Context context, String dbName, int dbVersion, String sqlStatements)
Creates a db and populates it with the sql statements in sqlStatements.
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 cursorDoubleToContentValuesIfPresent(Cursor cursor, ContentValues values, String column)
Reads a Double out of a column in a Cursor and writes it to a ContentValues.
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 cursorFloatToContentValuesIfPresent(Cursor cursor, ContentValues values, String column)
Reads a Float out of a column in a Cursor and writes it to a ContentValues.
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 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 cursorIntToContentValuesIfPresent(Cursor cursor, ContentValues values, String column)
Reads a Integer out of a column in a Cursor and writes it to a ContentValues.
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 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 cursorLongToContentValuesIfPresent(Cursor cursor, ContentValues values, String column)
Reads a Long out of a column in a Cursor and writes it to a ContentValues.
static void cursorRowToContentValues(Cursor cursor, ContentValues values)
Read the entire contents of a cursor row and store them in a ContentValues.
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.
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 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 cursorStringToContentValuesIfPresent(Cursor cursor, ContentValues values, String column)
Reads a String out of a column in a Cursor and writes it to a ContentValues.
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 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 void dumpCurrentRow(Cursor cursor)
Prints the contents of a Cursor's current row to System.out.
static String dumpCurrentRowToString(Cursor cursor)
Dump the contents of a Cursor's current row to a String.
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 void dumpCursor(Cursor cursor)
Prints the contents of a Cursor to System.out.
static String dumpCursorToString(Cursor cursor)
Prints the contents of a Cursor to a String.
static String getCollationKey(String name)
return the collation key
static String getHexCollationKey(String name)
return the collation key in hex format
static long longForQuery(SQLiteDatabase db, String query, String[] selectionArgs)
Utility method to run the query on the db and return the value in the first column of the first row.
static long longForQuery(SQLiteStatement prog, String[] selectionArgs)
Utility method to run the pre-compiled query and return the value in the first column of the first row.
static long queryNumEntries(SQLiteDatabase db, String table)
Query the table for the number of rows in the table.
final static void readExceptionFromParcel(Parcel reply)
Special function for reading an exception result from the header of a parcel, to be used after receiving the result of a transaction.
static void readExceptionWithFileNotFoundExceptionFromParcel(Parcel reply)
static void readExceptionWithOperationApplicationExceptionFromParcel(Parcel reply)
static String sqlEscapeString(String value)
SQL-escape a string.
static String stringForQuery(SQLiteDatabase db, String query, String[] selectionArgs)
Utility method to run the query on the db and return the value in the first column of the first row.
static String stringForQuery(SQLiteStatement prog, String[] selectionArgs)
Utility method to run the pre-compiled query and return the value in the first column of the first row.
final static void writeExceptionToParcel(Parcel reply, Exception e)
Special function for writing an exception result at the header of a parcel, to be used when returning an exception from a transaction.
[Expand]
Inherited Methods
From class java.lang.Object

Public Constructors

public DatabaseUtils ()

Since: API Level 1

Public Methods

public static void appendEscapedSQLString (StringBuilder sb, String sqlString)

Since: API Level 1

Appends an SQL string to the given StringBuilder, including the opening and closing single quotes. Any single quotes internal to sqlString will be escaped. This method is deprecated because we want to encourage everyone to use the "?" binding form. However, when implementing a ContentProvider, one may want to add WHERE clauses that were not provided by the caller. Since "?" is a positional form, using it in this case could break the caller because the indexes would be shifted to accomodate the ContentProvider's internal bindings. In that case, it may be necessary to construct a WHERE clause manually. This method is useful for those cases.

Parameters
sb the StringBuilder that the SQL string will be appended to
sqlString the raw string to be appended, which may contain single quotes

public static final void appendValueToSql (StringBuilder sql, Object value)

Since: API Level 1

Appends an Object to an SQL string with the proper escaping, etc.

public static void bindObjectToProgram (SQLiteProgram prog, int index, Object value)

Since: API Level 1

Binds the given Object to the given SQLiteProgram using the proper typing. For example, bind numbers as longs/doubles, and everything else as a string by call toString() on it.

Parameters
prog the program to bind the object to
index the 1-based index to bind at
value the value to bind

public static void createDbFromSqlStatements (Context context, String dbName, int dbVersion, String sqlStatements)

Since: API Level 1

Creates a db and populates it with the sql statements in sqlStatements.

Parameters
context the context to use to create the db
dbName the name of the db to create
dbVersion the version to set on the db
sqlStatements the statements to use to populate the db. This should be a single string of the form returned by sqlite3's .dump command (statements separated by semicolons)

public static void cursorDoubleToContentValues (Cursor cursor, String field, ContentValues values, String key)

Since: API Level 1

Reads a Double out of a field in a Cursor and writes it to a Map.

Parameters
cursor The cursor to read from
field The REAL field to read
values The ContentValues to put the value into
key The key to store the value with in the map

public static void cursorDoubleToContentValuesIfPresent (Cursor cursor, ContentValues values, String column)

Since: API Level 8

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 from
values The ContentValues to put the value into
column The column to read

public static void cursorDoubleToCursorValues (Cursor cursor, String field, ContentValues values)

Since: API Level 1

Reads a Double out of a field in a Cursor and writes it to a Map.

Parameters
cursor The cursor to read from
field The REAL field to read
values The ContentValues to put the value into

public static void cursorFloatToContentValuesIfPresent (Cursor cursor, ContentValues values, String column)

Since: API Level 8

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 from
values The ContentValues to put the value into
column The column to read

public static void cursorIntToContentValues (Cursor cursor, String field, ContentValues values, String key)

Since: API Level 1

Reads a Integer out of a field in a Cursor and writes it to a Map.

Parameters
cursor The cursor to read from
field The INTEGER field to read
values The ContentValues to put the value into, with the field as the key
key The key to store the value with in the map

public static void cursorIntToContentValues (Cursor cursor, String field, ContentValues values)

Since: API Level 1

Reads an Integer out of a field in a Cursor and writes it to a Map.

Parameters
cursor The cursor to read from
field The INTEGER field to read
values The ContentValues to put the value into, with the field as the key

public static void cursorIntToContentValuesIfPresent (Cursor cursor, ContentValues values, String column)

Since: API Level 8

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 from
values The ContentValues to put the value into
column The column to read

public static void cursorLongToContentValues (Cursor cursor, String field, ContentValues values, String key)

Since: API Level 1

Reads a Long out of a field in a Cursor and writes it to a Map.

Parameters
cursor The cursor to read from
field The INTEGER field to read
values The ContentValues to put the value into
key The key to store the value with in the map

public static void cursorLongToContentValues (Cursor cursor, String field, ContentValues values)

Since: API Level 1

Reads a Long out of a field in a Cursor and writes it to a Map.

Parameters
cursor The cursor to read from
field The INTEGER field to read
values The ContentValues to put the value into, with the field as the key

public static void cursorLongToContentValuesIfPresent (Cursor cursor, ContentValues values, String column)

Since: API Level 8

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 from
values The ContentValues to put the value into
column The column to read

public static void cursorRowToContentValues (Cursor cursor, ContentValues values)

Since: API Level 1

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.

public static void cursorShortToContentValuesIfPresent (Cursor cursor, ContentValues values, String column)

Since: API Level 8

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 from
values The ContentValues to put the value into
column The column to read

public static void cursorStringToContentValues (Cursor cursor, String field, ContentValues values, String key)

Since: API Level 1

Reads a String out of a field in a Cursor and writes it to a Map.

Parameters
cursor The cursor to read from
field The TEXT field to read
values The ContentValues to put the value into, with the field as the key
key The key to store the value with in the map

public static void cursorStringToContentValues (Cursor cursor, String field, ContentValues values)

Since: API Level 1

Reads a String out of a field in a Cursor and writes it to a Map.

Parameters
cursor The cursor to read from
field The TEXT field to read
values The ContentValues to put the value into, with the field as the key

public static void cursorStringToContentValuesIfPresent (Cursor cursor, ContentValues values, String column)

Since: API Level 8

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 from
values The ContentValues to put the value into
column The column to read

public static void cursorStringToInsertHelper (Cursor cursor, String field, DatabaseUtils.InsertHelper inserter, int index)

Since: API Level 1

Reads a String out of a field in a Cursor and writes it to an InsertHelper.

Parameters
cursor The cursor to read from
field The TEXT field to read
inserter The InsertHelper to bind into
index the index of the bind entry in the InsertHelper

public static void dumpCurrentRow (Cursor cursor, PrintStream stream)

Since: API Level 1

Prints the contents of a Cursor's current row to a PrintSteam.

Parameters
cursor the cursor to print
stream the stream to print to

public static void dumpCurrentRow (Cursor cursor, StringBuilder sb)

Since: API Level 1

Prints the contents of a Cursor's current row to a StringBuilder.

Parameters
cursor the cursor to print
sb the StringBuilder to print to

public static void dumpCurrentRow (Cursor cursor)

Since: API Level 1

Prints the contents of a Cursor's current row to System.out.

Parameters
cursor the cursor to print from

public static String dumpCurrentRowToString (Cursor cursor)

Since: API Level 1

Dump the contents of a Cursor's current row to a String.

Parameters
cursor the cursor to print
Returns
  • a String that contains the dumped cursor row

public static void dumpCursor (Cursor cursor, PrintStream stream)

Since: API Level 1

Prints the contents of a Cursor to a PrintSteam. The position is restored after printing.

Parameters
cursor the cursor to print
stream the stream to print to

public static void dumpCursor (Cursor cursor, StringBuilder sb)

Since: API Level 1

Prints the contents of a Cursor to a StringBuilder. The position is restored after printing.

Parameters
cursor the cursor to print
sb the StringBuilder to print to

public static void dumpCursor (Cursor cursor)

Since: API Level 1

Prints the contents of a Cursor to System.out. The position is restored after printing.

Parameters
cursor the cursor to print

public static String dumpCursorToString (Cursor cursor)

Since: API Level 1

Prints the contents of a Cursor to a String. The position is restored after printing.

Parameters
cursor the cursor to print
Returns
  • a String that contains the dumped cursor

public static String getCollationKey (String name)

Since: API Level 1

return the collation key

Returns
  • the collation key

public static String getHexCollationKey (String name)

Since: API Level 1

return the collation key in hex format

Returns
  • the collation key in hex format

public static long longForQuery (SQLiteDatabase db, String query, String[] selectionArgs)

Since: API Level 1

Utility method to run the query on the db and return the value in the first column of the first row.

public static long longForQuery (SQLiteStatement prog, String[] selectionArgs)

Since: API Level 1

Utility method to run the pre-compiled query and return the value in the first column of the first row.

public static long queryNumEntries (SQLiteDatabase db, String table)

Since: API Level 1

Query the table for the number of rows in the table.

Parameters
db the database the table is in
table the name of the table to query
Returns
  • the number of rows in the table

public static final void readExceptionFromParcel (Parcel reply)

Since: API Level 1

Special function for reading an exception result from the header of a parcel, to be used after receiving the result of a transaction. This will throw the exception for you if it had been written to the Parcel, otherwise return and let you read the normal result data from the Parcel.

Parameters
reply Parcel to read from

public static void readExceptionWithFileNotFoundExceptionFromParcel (Parcel reply)

Since: API Level 1

public static void readExceptionWithOperationApplicationExceptionFromParcel (Parcel reply)

Since: API Level 5

public static String sqlEscapeString (String value)

Since: API Level 1

SQL-escape a string.

public static String stringForQuery (SQLiteDatabase db, String query, String[] selectionArgs)

Since: API Level 1

Utility method to run the query on the db and return the value in the first column of the first row.

public static String stringForQuery (SQLiteStatement prog, String[] selectionArgs)

Since: API Level 1

Utility method to run the pre-compiled query and return the value in the first column of the first row.

public static final void writeExceptionToParcel (Parcel reply, Exception e)

Since: API Level 1

Special function for writing an exception result at the header of a 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 to
e The Exception to be written.