KQLite Quick API
KQLite provides simple convenience APIs to perform CRUD operations quickly. These are used to perform quick one time operations like inserting single item into table, performing simple select query, updating single item or deleting quickly using where.
Quick INSERT
Executes SQLite INSERT and inserts a single row into the table. This is a convenience function for a simple insert operation. It creates, binds, executes, and closes an `InsertStatement` in a single call.
// INSERT INTO contacts (first_name, last_name, phone, type, birth_date)
// VALUES (?, ?, JSON_ARRAY('1234567890'), ?, DATE('2000-01-01'));
TblContact.quickInsert { it: TblContact
it.firstName.bind("John")
it.lastName.bind("Doe")
it.phone.bind(JSON_ARRAY("1234567890"))
it.type.bind(ContactType.Family)
it.birthDate.bind(DATE("2000-01-01"))
}
Conflict resolution algorithm can also be specified using onConflict action. For more details check quickInsert docs.
// INSERT OR REPLACE INTO contacts (first_name, last_name, phone, type, birth_date)
// VALUES (?, ?, JSON_ARRAY('111111111'), ?, DATE('1999-01-01'));
TblContact.quickInsert(onConflict = Action.REPLACE) {
it.firstName.bind("John")
it.lastName.bind("Conner")
it.phone.bind(JSON_ARRAY("111111111"))
it.type.bind(ContactType.Family)
it.birthDate.bind(DATE("1999-01-01"))
}
Quick SELECT
Executes SQLite SELECT and returns database cursor which is auto closed when fully iterated. This is a convenience function for simple queries of most common types.
// SELECT * FROM contacts;
TblContact.quickSelect().forEach {
println(it[TblContact.firstName] + " " + it[TblContact.type])
}
Specific columns can be passed to arguements in select.
// SELECT first_name, type FROM contacts;
TblContact
.quickSelect(TblContact.firstName, TblContact.type)
.forEach {
println(it[TblContact.firstName] + " " + it[TblContact.type])
}
WHERE clause can be defined to filter results.
// SELECT first_name, type FROM contacts WHERE type = ?;
TblContact
.quickSelect(TblContact.firstName, TblContact.type) {
it.type EQ ContactType.Family
}
.forEach {
println(it[TblContact.firstName] + " " + it[TblContact.type])
}
Quick UPDATE
Executes an SQLite UPDATE statement with the provided binding values and WHERE clause. The WHERE parameter is required; to update all rows, pass null, which executes the statement without a WHERE clause.
// UPDATE contacts SET phone = JSON_ARRAY('1234567890','0987654321') WHERE id = ?;
TblContact.quickUpdate(
binding = {
it.phone.bind(JSON_ARRAY("1234567890", "0987654321"))
},
where = {
it.id EQ 1
}
)
Quick DELETE
Executes an SQLite DELETE statement with the provided WHERE clause. The WHERE parameter is required; to delete all rows, pass null, which executes the statement without a WHERE clause.
// DELETE FROM contacts WHERE id = ?;
TblContact.quickDelete {
it.id EQ 1
}