行のInsert、Query、Update
共有コードからCRUD操作を実行します
「行のInsert、Query、Update」はCoddyKit上の無料Kotlin Multiplatform Academyレッスンです。 これはレッスン3/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはKotlin Multiplatform Academy学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 Kotlin Multiplatform Academyコースには全4レッスンが含まれています。
このレッスンの一部はまだ翻訳されておらず、英語で表示されています。
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. ✅
よくある質問
「行のInsert、Query、Update」レッスンは無料ですか?
はい。「行のInsert、Query、Update」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Kotlin Multiplatform Academyコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Kotlin Multiplatform Academyコースには全4レッスンが含まれています。
「行のInsert、Query、Update」で何を学びますか?
共有コードからCRUD操作を実行します ブラウザで直接実行するハンズオンコードでKotlin Multiplatform Academyを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。
Kotlin Multiplatform Academyを始めるのに経験は必要ですか?
事前経験は必要ありません。CoddyKitのKotlin Multiplatform Academyは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン3/4です。
「行のInsert、Query、Update」レッスンにはどのくらい時間がかかりますか?
ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。
このKotlin Multiplatform Academyレッスンでコードを書いて実行できますか?
はい。すべてのKotlin Multiplatform Academyレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。
このコースのすべてのレッスン
- SQLDelightを追加して.sqスキーマを書く
- Platform SqlDriverの設定
- 行のInsert、Query、Update
- テーブルをFlowとして監視する