Insert, Query & Update Rows
Run CRUD operations from shared code.
Insert, Query & Update Rows is a free Kotlin Multiplatform Academy lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Kotlin Multiplatform Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Reach the Generated Queries
Your database exposes a queries object named after the .sq file. Every labeled statement becomes a method on it.
val queries = database.playerQueriesWrite an Insert Query
Add an insert label in your .sq file. SQLDelight turns its placeholders into typed Kotlin parameters.
insertPlayer:
INSERT INTO player(id, name) VALUES (?, ?);Call Insert from Kotlin
Now you call the generated function with real values. The types are checked, so a wrong argument fails to compile.
queries.insertPlayer(id = 1, name = "Ada")Select All Rows
Run a select query and ask for executeAsList to get every matching row as a typed Kotlin list.
val players = queries.selectAll().executeAsList()Mapped to Data Classes
SQLDelight maps each row to a generated data class with named, typed properties, so you avoid string-keyed cursors.
players.forEach { println(it.name) }Select a Single Row
For a one-row query use executeAsOneOrNull, which returns null instead of crashing when nothing matches.
val player = queries.selectById(1).executeAsOneOrNull()Write an Update Query
An update label changes existing rows. Put the new value first and the matching key last as placeholders.
updateName:
UPDATE player SET name = ? WHERE id = ?;Call Update from Kotlin
Invoke the generated update like any function. SQLDelight runs the SQL and applies it to every matching row.
queries.updateName(name = "Grace", id = 1)Delete Rows
A delete query removes rows by condition. Without a WHERE clause it clears the whole table, so be careful.
deleteById:
DELETE FROM player WHERE id = ?;Group with Transactions
Wrap several writes in a transaction so they all succeed or all roll back together, keeping your data consistent.
queries.transaction {
queries.insertPlayer(2, "Linus")
queries.insertPlayer(3, "Margaret")
}Runs the Same Everywhere
Because the queries are shared, this exact CRUD code behaves identically on Android and iOS. Write it once, trust it twice.
Quick Check
You query for one player by id but it may not exist. Which call is safest?
Recap: Full CRUD
You inserted, selected, updated and deleted rows with type-safe generated queries and grouped writes in a transaction. Next, make it reactive. ✅
Frequently asked questions
Is the “Insert, Query & Update Rows” lesson free?
Yes — the full text of “Insert, Query & Update Rows” is free to read here on the web, and the Kotlin Multiplatform Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Kotlin Multiplatform Academy course, upgrade to CoddyKit PRO.
What will I learn in “Insert, Query & Update Rows”?
Run CRUD operations from shared code. You practise Kotlin Multiplatform Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Kotlin Multiplatform Academy?
No prior experience is required. Kotlin Multiplatform Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Insert, Query & Update Rows” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Kotlin Multiplatform Academy lesson?
Yes. Every Kotlin Multiplatform Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Add SQLDelight & Write .sq Schemas
- Platform SqlDriver Setup
- Insert, Query & Update Rows
- Observe Tables as Flow