KQLiteCursor

Provides a read-only, forward-only, type-safe cursor as Iterator over the result set of underlying database cursor. Helpful to use with Kotlin forEach.

  • This operator allows for convenient, array-like access to column data using a KQLiteColumn instance. The return type T is inferred from the column definition, ensuring type safety at compile time.

  • Each iteration step advances the underlying cursor and exposes the current row through the same instance.

  • If any value is read from the cursor without any iteration or loops, cursor reads from the first row of the result. Hence, cursor.getString(3) without the loop will read the 4th column of the first row if the cursor is not empty.

  • For nullable columns, the return type T will be nullable (e.g., String?). For columns defined as notNull() or as a primaryKey(), the return type will be non-nullable (e.g., String, Int).

  • It provides getXXX or null operation for nullable columns, a convenience method over if(isNull(XXX)) ... else getXXX. For compatibility, index based data access is also possible. For example: cursor.getInt(0)

  • As this interface extends AutoCloseable, it is recommended to use it within a use block to ensure that the underlying resources are properly released.

  • For SELECT queries, the cursor is closed automatically after the last row is processed. Use kotlin.sequences.asSequence to convert the cursor into a Sequence to for advanced use-cases like kotlin.sequences.map cursor results to other types.

Example (Type-safe data access)

// SELECT * FROM employee_table;
val cursor = Employee.select().execute()
cursor.forEach {
// returns non-nullable id due to primaryKey() constraint
val id = it[Employee.id]

// returns non-nullable `String` name due to notNull() constraint
val name = it[Employee.name]

// returns nullable `String?` jobTitle
val jobTitle = it[Employee.jobTitle]

// Reading data by index
val value = it.getDouble(3)
}
// here cursor is closed automatically

Example (Cursor results mapping)

// Mapping results to other types
val pairs = cursor
.asSequence()
.map {
it[Employee.id] to it[Employee.name]
}.toList()

Example (Cursor object mapping)

val empList = Employee.quickSelect().mapToList(Employee::mapper)

Example (Cursor mapping as Flow)

val flow = Employee.quickSelect().asCallbackFlow().mapToList(mapper = Employee::mapper)

Example (Read first row)

// Reading from the first row immediately (unsafe)
val empName: String = Employee.select().execute().use { it[Employee.name] }

Author

MOHAMMAD AZIM ANSARI

See also

Functions

Link copied to clipboard

Converts the KQLiteCursor into a cold Flow that emits the cursor whenever the underlying database tables are updated.

Link copied to clipboard
abstract operator fun <T> get(column: KQLiteColumn<T>): T

Returns the value of the requested column from the current row in a type-safe manner.

Link copied to clipboard
abstract fun getBlob(@IntRange(from = 0) index: Int): ByteArray

Returns the value of the requested column at the given zero-based index as a ByteArray.

Link copied to clipboard
abstract fun getBoolean(@IntRange(from = 0) index: Int): Boolean

Returns the value of the requested column at the given zero-based index as a Boolean.

Link copied to clipboard
abstract fun getColumnIndex(column: KQLiteColumn<*>): Int

Returns the zero-based index for the given KQLiteColumn or -1 if not found.

Link copied to clipboard
abstract fun getColumnNames(): List<String>

Returns a list of all column names in the result set.

Link copied to clipboard
abstract fun getColumnType(@IntRange(from = 0) index: Int): Int

Returns the data type of the column at the specified zero-based index as androidx.sqlite.DataType.

Link copied to clipboard
abstract fun getDouble(@IntRange(from = 0) index: Int): Double

Returns the value of the requested column at the given zero-based index as a Double.

Link copied to clipboard
abstract fun getFloat(@IntRange(from = 0) index: Int): Float

Returns the value of the requested column at the given zero-based index as a Float.

Link copied to clipboard
abstract fun getInt(@IntRange(from = 0) index: Int): Int

Returns the value of the requested column at the given zero-based index as an Int.

Link copied to clipboard
abstract fun getLong(@IntRange(from = 0) index: Int): Long

Returns the value of the requested column at the given zero-based index as a Long.

Link copied to clipboard
abstract fun getString(@IntRange(from = 0) index: Int): String

This method is just an alias for getText, added for convenience.

Link copied to clipboard
abstract fun getText(@IntRange(from = 0) index: Int): String

Returns the value of the requested column at the given zero-based index as an String.

Link copied to clipboard
abstract fun isNull(column: KQLiteColumn<*>): Boolean

Checks if the value of the requested column is NULL in the current row.

abstract fun isNull(@IntRange(from = 0) index: Int): Boolean

Checks if the value of the requested column at the given zero-based index is NULL in the current row.

Link copied to clipboard
fun <T> KQLiteCursor.mapToList(mapper: (KQLiteCursor) -> T): List<T>

Eagerly maps all rows from this KQLiteCursor into a List using mapper.

Link copied to clipboard
fun <T> KQLiteCursor.mapToSingle(mapper: (KQLiteCursor) -> T): T

Maps the first row of this KQLiteCursor into a single object of type T using mapper.

Link copied to clipboard

Maps the first row of this KQLiteCursor into an object of type T using mapper, or returns null if the cursor is empty.