0Pricing
Kotlin Multiplatform Academy · บทเรียน

เพิ่ม SQLDelight และเขียนสคีมา .sq

กำหนดตารางและสร้าง API Kotlin ที่ปลอดภัยด้านชนิดข้อมูล

เพิ่ม SQLDelight และเขียนสคีมา .sq เป็นบทเรียน Kotlin Multiplatform Academy ฟรีบน CoddyKit นี่คือบทเรียนที่ 1 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน 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.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. ✅

คำถามที่พบบ่อย

บทเรียน “เพิ่ม SQLDelight และเขียนสคีมา .sq” ฟรีหรือไม่

ใช่ — ข้อความเต็มของ “เพิ่ม SQLDelight และเขียนสคีมา .sq” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Kotlin Multiplatform Academy ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Kotlin Multiplatform Academy มีบทเรียนทั้งหมด 4 บทเรียน

คุณจะเรียนรู้อะไรในบทเรียน “เพิ่ม SQLDelight และเขียนสคีมา .sq”

กำหนดตารางและสร้าง API Kotlin ที่ปลอดภัยด้านชนิดข้อมูล คุณปฏิบัติ Kotlin Multiplatform Academy ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน

คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Kotlin Multiplatform Academy หรือไม่

ไม่จำเป็นต้องมีประสบการณ์มาก่อน Kotlin Multiplatform Academy บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 1 จากทั้งหมด 4 บทเรียน

บทเรียน “เพิ่ม SQLDelight และเขียนสคีมา .sq” ใช้เวลานานแค่ไหน

บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย

ฉันเขียนและรันโค้ดในบทเรียน Kotlin Multiplatform Academy นี้ได้ไหม

ได้ บทเรียน Kotlin Multiplatform Academy ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ

บทเรียนทั้งหมดในหลักสูตรนี้

  1. เพิ่ม SQLDelight และเขียนสคีมา .sq
  2. การตั้งค่า SqlDriver ตามแพลตฟอร์ม
  3. แทรก คิวรี และอัปเดตแถว
  4. สังเกตตารางเป็น Flow
← กลับไปที่ Kotlin Multiplatform Academy