0Pricing
Kotlin Multiplatform Academy · Lesson

Add SQLDelight & Write .sq Schemas

Define tables and generate type-safe Kotlin APIs.

Add SQLDelight & Write .sq Schemas is a free Kotlin Multiplatform Academy lesson on CoddyKit — lesson 1 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.

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.sq

Define 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. ✅

Frequently asked questions

Is the “Add SQLDelight & Write .sq Schemas” lesson free?

Yes — the full text of “Add SQLDelight & Write .sq Schemas” 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 “Add SQLDelight & Write .sq Schemas”?

Define tables and generate type-safe Kotlin APIs. 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 1 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Add SQLDelight & Write .sq Schemas” 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

  1. Add SQLDelight & Write .sq Schemas
  2. Platform SqlDriver Setup
  3. Insert, Query & Update Rows
  4. Observe Tables as Flow
← Back to Kotlin Multiplatform Academy