KQLite Cursor
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
Tis 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 the4thcolumn of the first row if the cursor is not empty.For nullable columns, the return type
Twill be nullable (e.g.,String?). For columns defined asnotNull()or as aprimaryKey(), the return type will be non-nullable (e.g.,String,Int).It provides
getXXXornulloperation for nullable columns, a convenience method overif(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
useblock to ensure that the underlying resources are properly released.For
SELECTqueries, 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 automaticallyExample (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
Converts the KQLiteCursor into a cold Flow that emits the cursor whenever the underlying database tables are updated.
Returns the value of the requested column at the given zero-based index as a Boolean.
Returns the zero-based index for the given KQLiteColumn or -1 if not found.
Returns a list of all column names in the result set.
Returns the data type of the column at the specified zero-based index as androidx.sqlite.DataType.
Eagerly maps all rows from this KQLiteCursor into a List using mapper.
Maps the first row of this KQLiteCursor into a single object of type T using mapper.
Maps the first row of this KQLiteCursor into an object of type T using mapper, or returns null if the cursor is empty.