Added in API level 1

DatabaseUtils


open class DatabaseUtils
kotlin.Any
   ↳ android.database.DatabaseUtils

Static utility methods for dealing with databases and Cursors.

Summary

Nested classes
open

This class allows users to do multiple inserts into a table using the same statement.

Constants
static Int

One of the values returned by getSqlStatementType(java.lang.String).

static Int

One of the values returned by getSqlStatementType(java.lang.String).

static Int

One of the values returned by getSqlStatementType(java.lang.String).

static Int

One of the values returned by getSqlStatementType(java.lang.String).

static Int

One of the values returned by getSqlStatementType(java.lang.String).

static Int

One of the values returned by getSqlStatementType(java.lang.String).

static Int

One of the values returned by getSqlStatementType(java.lang.String).

static Int

One of the values returned by getSqlStatementType(java.lang.String).

static Int

One of the values returned by getSqlStatementType(java.lang.String).

static Int

One of the values returned by getSqlStatementType(java.lang.String).

Public constructors

Public methods
open static Unit

Appends an SQL string to the given StringBuilder, including the opening and closing single quotes.

open static Array<String!>!
appendSelectionArgs(originalValues: Array<String!>!, newValues: Array<String!>!)

Appends one set of selection args to another.

static Unit

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

open static Unit
bindObjectToProgram(prog: SQLiteProgram!, index: Int, value: Any!)

Binds the given Object to the given SQLiteProgram using the proper typing.

open static ParcelFileDescriptor!
blobFileDescriptorForQuery(db: SQLiteDatabase!, query: String!, selectionArgs: Array<String!>!)

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

open static ParcelFileDescriptor!

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

open static String!

Concatenates two SQL WHERE clauses, handling empty or null values.

open static Unit
createDbFromSqlStatements(context: Context!, dbName: String!, dbVersion: Int, sqlStatements: String!)

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

open static Unit
cursorDoubleToContentValues(cursor: Cursor!, field: String!, values: ContentValues!, key: String!)

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

open static Unit

Reads a Double out of a column in a Cursor and writes it to a ContentValues.

open static Unit
cursorDoubleToCursorValues(cursor: Cursor!, field: String!, values: ContentValues!)

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

open static Unit

Reads a Float out of a column in a Cursor and writes it to a ContentValues.

open static Unit
cursorIntToContentValues(cursor: Cursor!, field: String!, values: ContentValues!)

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

open static Unit
cursorIntToContentValues(cursor: Cursor!, field: String!, values: ContentValues!, key: String!)

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

open static Unit

Reads a Integer out of a column in a Cursor and writes it to a ContentValues.

open static Unit
cursorLongToContentValues(cursor: Cursor!, field: String!, values: ContentValues!)

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

open static Unit
cursorLongToContentValues(cursor: Cursor!, field: String!, values: ContentValues!, key: String!)

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

open static Unit

Reads a Long out of a column in a Cursor and writes it to a ContentValues.

open static Unit

Read the entire contents of a cursor row and store them in a ContentValues.

open static Unit

Reads a Short out of a column in a Cursor and writes it to a ContentValues.

open static Unit
cursorStringToContentValues(cursor: Cursor!, field: String!, values: ContentValues!)

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

open static Unit
cursorStringToContentValues(cursor: Cursor!, field: String!, values: ContentValues!, key: String!)

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

open static Unit

Reads a String out of a column in a Cursor and writes it to a ContentValues.

open static Unit
cursorStringToInsertHelper(cursor: Cursor!, field: String!, inserter: DatabaseUtils.InsertHelper!, index: Int)

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

open static Unit

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

open static Unit
dumpCurrentRow(cursor: Cursor!, stream: PrintStream!)

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

open static Unit

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

open static String!

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

open static Unit
dumpCursor(cursor: Cursor!)

Prints the contents of a Cursor to System.

open static Unit
dumpCursor(cursor: Cursor!, stream: PrintStream!)

Prints the contents of a Cursor to a PrintSteam.

open static Unit
dumpCursor(cursor: Cursor!, sb: StringBuilder!)

Prints the contents of a Cursor to a StringBuilder.

open static String!

Prints the contents of a Cursor to a String.

open static String!

return the collation key

open static String!

return the collation key in hex format

open static Int

Returns one of the following which represent the type of the given SQL statement.

open static Long
longForQuery(db: SQLiteDatabase!, query: String!, selectionArgs: Array<String!>!)

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

open static Long
longForQuery(prog: SQLiteStatement!, selectionArgs: Array<String!>!)

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

open static Long

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

open static Long
queryNumEntries(db: SQLiteDatabase!, table: String!, selection: String!)

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

open static Long
queryNumEntries(db: SQLiteDatabase!, table: String!, selection: String!, selectionArgs: Array<String!>!)

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

static Unit

Special function for reading an exception result from the header of a parcel, to be used after receiving the result of a transaction.

open static Unit

open static Unit

open static String!

SQL-escape a string.

open static String!
stringForQuery(db: SQLiteDatabase!, query: String!, selectionArgs: Array<String!>!)

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

open static String!
stringForQuery(prog: SQLiteStatement!, selectionArgs: Array<String!>!)

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

static Unit

Special function for writing an exception result at the header of a parcel, to be used when returning an exception from a transaction.

Constants

STATEMENT_ABORT

Added in API level 11
static val STATEMENT_ABORT: Int

One of the values returned by getSqlStatementType(java.lang.String).

Value: 6

STATEMENT_ATTACH

Added in API level 11
static val STATEMENT_ATTACH: Int

One of the values returned by getSqlStatementType(java.lang.String).

Value: 3

STATEMENT_BEGIN

Added in API level 11
static val STATEMENT_BEGIN: Int

One of the values returned by getSqlStatementType(java.lang.String).

Value: 4

STATEMENT_COMMIT

Added in API level 11
static val STATEMENT_COMMIT: Int

One of the values returned by getSqlStatementType(java.lang.String).

Value: 5

STATEMENT_DDL

Added in API level 11
static val STATEMENT_DDL: Int

One of the values returned by getSqlStatementType(java.lang.String).

Value: 8

STATEMENT_OTHER

Added in API level 11
static val STATEMENT_OTHER: Int

One of the values returned by getSqlStatementType(java.lang.String).

Value: 99

STATEMENT_PRAGMA

Added in API level 11
static val STATEMENT_PRAGMA: Int

One of the values returned by getSqlStatementType(java.lang.String).

Value: 7

STATEMENT_SELECT

Added in API level 11
static val STATEMENT_SELECT: Int

One of the values returned by getSqlStatementType(java.lang.String).

Value: 1

STATEMENT_UNPREPARED

Added in API level 11
static val STATEMENT_UNPREPARED: Int

One of the values returned by getSqlStatementType(java.lang.String).

Value: 9

STATEMENT_UPDATE

Added in API level 11
static val STATEMENT_UPDATE: Int

One of the values returned by getSqlStatementType(java.lang.String).

Value: 2

Public constructors

DatabaseUtils

DatabaseUtils()

Public methods

appendEscapedSQLString

Added in API level 1
open static fun appendEscapedSQLString(
    sb: StringBuilder!,
    sqlString: String!
): Unit

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 StringBuilder!: the StringBuilder that the SQL string will be appended to
sqlString String!: the raw string to be appended, which may contain single quotes

appendSelectionArgs

Added in API level 11
open static fun appendSelectionArgs(
    originalValues: Array<String!>!,
    newValues: Array<String!>!
): Array<String!>!

Appends one set of selection args to another. This is useful when adding a selection argument to a user provided set.

appendValueToSql

Added in API level 1
static fun appendValueToSql(
    sql: StringBuilder!,
    value: Any!
): Unit

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

bindObjectToProgram

Added in API level 1
open static fun bindObjectToProgram(
    prog: SQLiteProgram!,
    index: Int,
    value: Any!
): Unit

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 SQLiteProgram!: the program to bind the object to
index Int: the 1-based index to bind at
value Any!: the value to bind

blobFileDescriptorForQuery

Added in API level 11
open static fun blobFileDescriptorForQuery(
    db: SQLiteDatabase!,
    query: String!,
    selectionArgs: Array<String!>!
): ParcelFileDescriptor!

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

Return
ParcelFileDescriptor! A read-only file descriptor for a copy of the blob value.

blobFileDescriptorForQuery

Added in API level 11
open static fun blobFileDescriptorForQuery(
    prog: SQLiteStatement!,
    selectionArgs: Array<String!>!
): ParcelFileDescriptor!

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

Return
ParcelFileDescriptor! A read-only file descriptor for a copy of the blob value.

concatenateWhere

Added in API level 11
open static fun concatenateWhere(
    a: String!,
    b: String!
): String!

Concatenates two SQL WHERE clauses, handling empty or null values.

createDbFromSqlStatements

Added in API level 1
open static fun createDbFromSqlStatements(
    context: Context!,
    dbName: String!,
    dbVersion: Int,
    sqlStatements: String!
): Unit

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

Parameters
context Context!: the context to use to create the db
dbName String!: the name of the db to create
dbVersion Int: the version to set on the db
sqlStatements String!: 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)

cursorDoubleToContentValues

Added in API level 1
open static fun cursorDoubleToContentValues(
    cursor: Cursor!,
    field: String!,
    values: ContentValues!,
    key: String!
): Unit

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

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

cursorDoubleToContentValuesIfPresent

Added in API level 8
open static fun cursorDoubleToContentValuesIfPresent(
    cursor: Cursor!,
    values: ContentValues!,
    column: String!
): Unit

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 Cursor!: The cursor to read from
column String!: The column to read
values ContentValues!: The ContentValues to put the value into

cursorDoubleToCursorValues

Added in API level 1
open static fun cursorDoubleToCursorValues(
    cursor: Cursor!,
    field: String!,
    values: ContentValues!
): Unit

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

Parameters
cursor Cursor!: The cursor to read from
field String!: The REAL field to read
values ContentValues!: The ContentValues to put the value into

cursorFloatToContentValuesIfPresent

Added in API level 8
open static fun cursorFloatToContentValuesIfPresent(
    cursor: Cursor!,
    values: ContentValues!,
    column: String!
): Unit

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 Cursor!: The cursor to read from
column String!: The column to read
values ContentValues!: The ContentValues to put the value into

cursorIntToContentValues

Added in API level 1
open static fun cursorIntToContentValues(
    cursor: Cursor!,
    field: String!,
    values: ContentValues!
): Unit

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

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

cursorIntToContentValues

Added in API level 1
open static fun cursorIntToContentValues(
    cursor: Cursor!,
    field: String!,
    values: ContentValues!,
    key: String!
): Unit

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

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

cursorIntToContentValuesIfPresent

Added in API level 8
open static fun cursorIntToContentValuesIfPresent(
    cursor: Cursor!,
    values: ContentValues!,
    column: String!
): Unit

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 Cursor!: The cursor to read from
column String!: The column to read
values ContentValues!: The ContentValues to put the value into

cursorLongToContentValues

Added in API level 1
open static fun cursorLongToContentValues(
    cursor: Cursor!,
    field: String!,
    values: ContentValues!
): Unit

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

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

cursorLongToContentValues

Added in API level 1
open static fun cursorLongToContentValues(
    cursor: Cursor!,
    field: String!,
    values: ContentValues!,
    key: String!
): Unit

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

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

cursorLongToContentValuesIfPresent

Added in API level 8
open static fun cursorLongToContentValuesIfPresent(
    cursor: Cursor!,
    values: ContentValues!,
    column: String!
): Unit

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 Cursor!: The cursor to read from
column String!: The column to read
values ContentValues!: The ContentValues to put the value into

cursorRowToContentValues

Added in API level 1
open static fun cursorRowToContentValues(
    cursor: Cursor!,
    values: ContentValues!
): Unit

Read the entire contents of a cursor row and store them in a ContentValues.

Parameters
cursor Cursor!: the cursor to read from.
values ContentValues!: the ContentValues to put the row into.

cursorShortToContentValuesIfPresent

Added in API level 8
open static fun cursorShortToContentValuesIfPresent(
    cursor: Cursor!,
    values: ContentValues!,
    column: String!
): Unit

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 Cursor!: The cursor to read from
column String!: The column to read
values ContentValues!: The ContentValues to put the value into

cursorStringToContentValues

Added in API level 1
open static fun cursorStringToContentValues(
    cursor: Cursor!,
    field: String!,
    values: ContentValues!
): Unit

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

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

cursorStringToContentValues

Added in API level 1
open static fun cursorStringToContentValues(
    cursor: Cursor!,
    field: String!,
    values: ContentValues!,
    key: String!
): Unit

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

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

cursorStringToContentValuesIfPresent

Added in API level 8
open static fun cursorStringToContentValuesIfPresent(
    cursor: Cursor!,
    values: ContentValues!,
    column: String!
): Unit

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 Cursor!: The cursor to read from
column String!: The column to read
values ContentValues!: The ContentValues to put the value into

cursorStringToInsertHelper

Added in API level 1
open static fun cursorStringToInsertHelper(
    cursor: Cursor!,
    field: String!,
    inserter: DatabaseUtils.InsertHelper!,
    index: Int
): Unit

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

Parameters
cursor Cursor!: The cursor to read from
field String!: The TEXT field to read
inserter DatabaseUtils.InsertHelper!: The InsertHelper to bind into
index Int: the index of the bind entry in the InsertHelper

dumpCurrentRow

Added in API level 1
open static fun dumpCurrentRow(cursor: Cursor!): Unit

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

Parameters
cursor Cursor!: the cursor to print from

dumpCurrentRow

Added in API level 1
open static fun dumpCurrentRow(
    cursor: Cursor!,
    stream: PrintStream!
): Unit

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

Parameters
cursor Cursor!: the cursor to print
stream PrintStream!: the stream to print to

dumpCurrentRow

Added in API level 1
open static fun dumpCurrentRow(
    cursor: Cursor!,
    sb: StringBuilder!
): Unit

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

Parameters
cursor Cursor!: the cursor to print
sb StringBuilder!: the StringBuilder to print to

dumpCurrentRowToString

Added in API level 1
open static fun dumpCurrentRowToString(cursor: Cursor!): String!

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

Parameters
cursor Cursor!: the cursor to print
Return
String! a String that contains the dumped cursor row

dumpCursor

Added in API level 1
open static fun dumpCursor(cursor: Cursor!): Unit

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

Parameters
cursor Cursor!: the cursor to print

dumpCursor

Added in API level 1
open static fun dumpCursor(
    cursor: Cursor!,
    stream: PrintStream!
): Unit

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

Parameters
cursor Cursor!: the cursor to print
stream PrintStream!: the stream to print to

dumpCursor

Added in API level 1
open static fun dumpCursor(
    cursor: Cursor!,
    sb: StringBuilder!
): Unit

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

Parameters
cursor Cursor!: the cursor to print
sb StringBuilder!: the StringBuilder to print to

dumpCursorToString

Added in API level 1
open static fun dumpCursorToString(cursor: Cursor!): String!

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

Parameters
cursor Cursor!: the cursor to print
Return
String! a String that contains the dumped cursor

getCollationKey

Added in API level 1
open static fun getCollationKey(name: String!): String!

return the collation key

Parameters
name String!:
Return
String! the collation key

getHexCollationKey

Added in API level 1
open static fun getHexCollationKey(name: String!): String!

return the collation key in hex format

Parameters
name String!:
Return
String! the collation key in hex format

getSqlStatementType

Added in API level 11
open static fun getSqlStatementType(sql: String!): Int

Returns one of the following which represent the type of the given SQL statement.

  1. STATEMENT_SELECT
  2. STATEMENT_UPDATE
  3. STATEMENT_ATTACH
  4. STATEMENT_BEGIN
  5. STATEMENT_COMMIT
  6. STATEMENT_ABORT
  7. STATEMENT_PRAGMA
  8. STATEMENT_DDL
  9. STATEMENT_UNPREPARED
  10. STATEMENT_OTHER

Parameters
sql String!: the SQL statement whose type is returned by this method
Return
Int one of the values listed above

longForQuery

Added in API level 1
open static fun longForQuery(
    db: SQLiteDatabase!,
    query: String!,
    selectionArgs: Array<String!>!
): Long

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

longForQuery

Added in API level 1
open static fun longForQuery(
    prog: SQLiteStatement!,
    selectionArgs: Array<String!>!
): Long

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

queryNumEntries

Added in API level 1
open static fun queryNumEntries(
    db: SQLiteDatabase!,
    table: String!
): Long

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

Parameters
db SQLiteDatabase!: the database the table is in
table String!: the name of the table to query
Return
Long the number of rows in the table

queryNumEntries

Added in API level 11
open static fun queryNumEntries(
    db: SQLiteDatabase!,
    table: String!,
    selection: String!
): Long

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

Parameters
db SQLiteDatabase!: the database the table is in
table String!: the name of the table to query
selection String!: A filter declaring which rows to return, formatted as an SQL WHERE clause (excluding the WHERE itself). Passing null will count all rows for the given table
Return
Long the number of rows in the table filtered by the selection

queryNumEntries

Added in API level 11
open static fun queryNumEntries(
    db: SQLiteDatabase!,
    table: String!,
    selection: String!,
    selectionArgs: Array<String!>!
): Long

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

Parameters
db SQLiteDatabase!: the database the table is in
table String!: the name of the table to query
selection String!: A filter declaring which rows to return, formatted as an SQL WHERE clause (excluding the WHERE itself). Passing null will count all rows for the given table
selectionArgs Array<String!>!: 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.
Return
Long the number of rows in the table filtered by the selection

readExceptionFromParcel

Added in API level 1
static fun readExceptionFromParcel(reply: Parcel!): Unit

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!: Parcel to read from

readExceptionWithFileNotFoundExceptionFromParcel

Added in API level 1
open static fun readExceptionWithFileNotFoundExceptionFromParcel(reply: Parcel!): Unit

readExceptionWithOperationApplicationExceptionFromParcel

Added in API level 5
open static fun readExceptionWithOperationApplicationExceptionFromParcel(reply: Parcel!): Unit

sqlEscapeString

Added in API level 1
open static fun sqlEscapeString(value: String!): String!

SQL-escape a string.

stringForQuery

Added in API level 1
open static fun stringForQuery(
    db: SQLiteDatabase!,
    query: String!,
    selectionArgs: Array<String!>!
): String!

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

stringForQuery

Added in API level 1
open static fun stringForQuery(
    prog: SQLiteStatement!,
    selectionArgs: Array<String!>!
): String!

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

writeExceptionToParcel

Added in API level 1
static fun writeExceptionToParcel(
    reply: Parcel!,
    e: Exception!
): Unit

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!: Parcel to write to
e Exception!: The Exception to be written.