Platform SqlDriver Setup
Provide Android and iOS drivers via expect/actual.
Platform SqlDriver Setup is a free Kotlin Multiplatform Academy lesson on CoddyKit — lesson 2 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.
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. ✅
Frequently asked questions
Is the “Platform SqlDriver Setup” lesson free?
Yes — the full text of “Platform SqlDriver Setup” 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 “Platform SqlDriver Setup”?
Provide Android and iOS drivers via expect/actual. 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 2 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Platform SqlDriver Setup” 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