Platform SqlDriverの設定
expect/actualでAndroidとiOSのdriverを提供します
「Platform SqlDriverの設定」はCoddyKit上の無料Kotlin Multiplatform Academyレッスンです。 これはレッスン2/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはKotlin Multiplatform Academy学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 Kotlin Multiplatform Academyコースには全4レッスンが含まれています。
このレッスンの一部はまだ翻訳されておらず、英語で表示されています。
What a Driver Does
SQLDelight generates queries, but it needs a SqlDriver to actually talk to SQLite. Each platform ships its own driver.
Why Drivers Differ
Android and iOS open SQLite differently, so there is no single shared driver. This is exactly the job for expect/actual.
Declare the expect Factory
In commonMain you declare an expect function that promises a SqlDriver, without saying how each platform builds it.
expect fun driverFactory(): SqlDriverAdd Platform Driver Deps
Each source set pulls in its own driver artifact: an Android driver in androidMain and a native driver in iosMain.
androidMain: "app.cash.sqldelight:android-driver:2.0.2"
iosMain: "app.cash.sqldelight:native-driver:2.0.2"Android actual Driver
On Android you build an AndroidSqliteDriver, passing the generated schema and a Context to open the file.
actual fun driverFactory(): SqlDriver =
AndroidSqliteDriver(AppDatabase.Schema, context, "app.db")Context on Android
The Android driver needs a Context. Pass the application context in so the database survives the whole app lifecycle.
iOS actual Driver
On iOS you build a NativeSqliteDriver from the same schema. No Context needed; it stores the file in the app sandbox.
actual fun driverFactory(): SqlDriver =
NativeSqliteDriver(AppDatabase.Schema, "app.db")The Schema Object
Both drivers take AppDatabase.Schema. SQLDelight generated it from your .sq file, and it creates the tables on first launch.
Build the Database
With a driver in hand you create the database instance. This AppDatabase is your typed entry point to every query.
val database = AppDatabase(driverFactory())Share One Instance
Creating many databases wastes file handles. Build the driver once and reuse a single AppDatabase across the app.
Inject the Driver
Pass the driver in from each platform rather than hardcoding it. This keeps shared code testable with a fake or in-memory driver.
Quick Check
How does SQLDelight get a platform-specific database driver into shared code?
Recap: Wire the Engine
You declared an expect driver factory, supplied AndroidSqliteDriver and NativeSqliteDriver, and built one AppDatabase. Now you can run queries. ✅
よくある質問
「Platform SqlDriverの設定」レッスンは無料ですか?
はい。「Platform SqlDriverの設定」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Kotlin Multiplatform Academyコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Kotlin Multiplatform Academyコースには全4レッスンが含まれています。
「Platform SqlDriverの設定」で何を学びますか?
expect/actualでAndroidとiOSのdriverを提供します ブラウザで直接実行するハンズオンコードでKotlin Multiplatform Academyを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。
Kotlin Multiplatform Academyを始めるのに経験は必要ですか?
事前経験は必要ありません。CoddyKitのKotlin Multiplatform Academyは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン2/4です。
「Platform SqlDriverの設定」レッスンにはどのくらい時間がかかりますか?
ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。
このKotlin Multiplatform Academyレッスンでコードを書いて実行できますか?
はい。すべてのKotlin Multiplatform Academyレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。
このコースのすべてのレッスン
- SQLDelightを追加して.sqスキーマを書く
- Platform SqlDriverの設定
- 行のInsert、Query、Update
- テーブルをFlowとして監視する