SQLDelightを追加して.sqスキーマを書く
テーブルを定義し、型安全なKotlin APIを生成します
「SQLDelightを追加して.sqスキーマを書く」はCoddyKit上の無料Kotlin Multiplatform Academyレッスンです。 これはレッスン1/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはKotlin Multiplatform Academy学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 Kotlin Multiplatform Academyコースには全4レッスンが含まれています。
このレッスンの一部はまだ翻訳されておらず、英語で表示されています。
Meet SQLDelight
SQLDelight turns your SQL into type-safe Kotlin you can call from shared code. You write real SQL, it writes the boilerplate. 🗄️
Why It Fits KMP
Because it generates plain Kotlin from SQL, the same database code runs on Android and iOS. The queries are shared, the engine is native per platform.
Add the Gradle Plugin
You enable SQLDelight by adding its Gradle plugin to your shared module so it can scan and process your schema files.
plugins {
id("app.cash.sqldelight") version "2.0.2"
}Add the Runtime
The plugin generates code, but you also need the runtime dependency in commonMain so the generated queries actually execute.
commonMain.dependencies {
implementation("app.cash.sqldelight:runtime:2.0.2")
}Declare a Database
You tell SQLDelight the database name and where it lives. This databases block names the generated class you will use later.
sqldelight {
databases {
create("AppDatabase") {
packageName.set("com.example.db")
}
}
}What Is a .sq File
Your schema lives in .sq files: plain text holding SQL statements plus named queries. SQLDelight reads them and emits Kotlin.
Where .sq Files Go
Place .sq files under commonMain/sqldelight in the package you declared. The plugin only looks there for your schema.
commonMain/sqldelight/com/example/db/Player.sqDefine a Table
Inside a .sq file you write a normal CREATE TABLE statement. SQLDelight uses it to type-check every query you write.
CREATE TABLE player (
id INTEGER NOT NULL PRIMARY KEY,
name TEXT NOT NULL
);Name Your Queries
Each query starts with a label ending in a colon. SQLDelight turns that label into a Kotlin function you can call.
selectAll:
SELECT * FROM player;Parameters Become Arguments
A ? placeholder in SQL becomes a typed Kotlin function parameter, so the compiler checks your inputs for you.
selectById:
SELECT * FROM player WHERE id = ?;Generated Type Safety
SQLDelight maps your columns to Kotlin types. A wrong column or type is caught at compile time, not at runtime in production.
Quick Check
Where do your SQLDelight schema files belong in a KMP project?
Recap: Schema First
You added the plugin and runtime, declared a database, and wrote a .sq schema with named queries. Next you give each platform a driver. ✅
よくある質問
「SQLDelightを追加して.sqスキーマを書く」レッスンは無料ですか?
はい。「SQLDelightを追加して.sqスキーマを書く」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Kotlin Multiplatform Academyコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Kotlin Multiplatform Academyコースには全4レッスンが含まれています。
「SQLDelightを追加して.sqスキーマを書く」で何を学びますか?
テーブルを定義し、型安全なKotlin APIを生成します ブラウザで直接実行するハンズオンコードでKotlin Multiplatform Academyを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。
Kotlin Multiplatform Academyを始めるのに経験は必要ですか?
事前経験は必要ありません。CoddyKitのKotlin Multiplatform Academyは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン1/4です。
「SQLDelightを追加して.sqスキーマを書く」レッスンにはどのくらい時間がかかりますか?
ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。
このKotlin Multiplatform Academyレッスンでコードを書いて実行できますか?
はい。すべてのKotlin Multiplatform Academyレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。
このコースのすべてのレッスン
- SQLDelightを追加して.sqスキーマを書く
- Platform SqlDriverの設定
- 行のInsert、Query、Update
- テーブルをFlowとして監視する