KQLite INSERT
KQLite provides following ways to insert data into KQLiteTable.
- Quick insert convenience API
- Insert by prepared statement
- Insert without specifying columns
- Insert any type by VALUES
- Fetching newly inserted ROWID by RETURNING
- Fetching multiple columns by RETURNING
- Fetching newly inserted row by RETURNING *
- Insert by SELECT from another table
- Insert binding functions
- Insert DEFAULT values
Quickly inserting a row into KQLiteTable
KQLiteTable provides the quickInsert extension method to simplify inserting a single row into a table avoiding complex query execution manually. The method does not return anything for operations chaining. Database must be open before performing any operation.
val myDatabase = MyDatabase()
myDatabase.open()
Artists.quickInsert { it: Artists
it.artistId.bind(1)
it.artistName.bind("SuperCoolMan")
}
myDatabase.close()
Content copied to clipboard
Inserting multiple rows using prepared statement
val prepared = Artists.insert(Artists.artistName)
// Bind and execute to insert multiple rows
for (i in 2..5) {
prepared.bind { it: Artists
it.artistName.bind("SuperCoolMan $i")
}.execute()
}
prepared.close() // Close once done
Content copied to clipboard
Inserting into all columns without specifying column names
val prepared = Artists.insert()
// Insert multiple rows
for (i in 6..8) {
prepared.bind { it: Artists
it.artistId.bind(i)
it.artistName.bind("SuperCoolMan $i")
}.execute()
}
prepared.close() // Close once done
Content copied to clipboard
Inserting multiple rows by VALUES
val prepared = Artists.insert()
prepared.values(9, "SuperCoolMan 9").execute()
prepared.values(10, "SuperCoolMan 10").execute()
prepared.close()
Content copied to clipboard
Fetching newly inserted ROWID by RETURNING
val prepared = Artists.insert()
val id: Long = prepared.bind { it: Artists
it.artistName.bind("SuperCoolMan 11")
}.executeReturning(Artists.rowId)
println("Inserted artist with rowId: $id")
prepared.close()
Content copied to clipboard
Fetching multiple columns by RETURNING
val prepared = Artists.insert()
val cursor: KQLiteCursor = prepared.bind { it: Artists
it.artistName.bind("SuperCoolMan 12")
}.executeReturning(Artists.artistId, Artists.artistName)
println(
"Artist inserted: id=${cursor[Artists.artistId]}, name=${cursor[Artists.artistName]}"
)
prepared.close()
Content copied to clipboard
Fetching newly inserted row by RETURNING *
val prepared = Artists.insert()
val cursor: KQLiteCursor = prepared.bind { it: Artists
it.artistName.bind("SuperCoolMan 13")
}.executeReturning() // Empty returning means *
println(
"Artist inserted: id=${cursor[Artists.artistId]}, name=${cursor[Artists.artistName]}"
)
prepared.close()
Content copied to clipboard
Insert by SELECT from another table
Artists
.insert(Artists.artistName)
.select {
Employees
.select(
Employees.firstName CONCAT " " CONCAT Employees.lastName
)
.where { it: Employees
it.title LIKE "%artist%"
}
}.execute()
Content copied to clipboard
Insert binding functions
Artists.quickInsert { it: Artists
it.artistName.bind(UPPER(StringLiteral("SuperCoolMan")))
}
Content copied to clipboard
Inserting DEFAULT values
Artists.insertDefaultValues()
Content copied to clipboard