0Pricing
Kotlin Multiplatform Academy · درس

إضافة SQLDelight وكتابة مخططات .sq

تعريف الجداول وإنشاء واجهات Kotlin آمنة الأنواع

إضافة SQLDelight وكتابة مخططات .sq درس مجاني في Kotlin Multiplatform Academy على CoddyKit. هذا هو الدرس 1 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 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» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة Kotlin Multiplatform Academy، انتقل إلى CoddyKit PRO. تتضمن دورة Kotlin Multiplatform Academy 4 دروس في المجموع.

ماذا ستتعلم في «إضافة SQLDelight وكتابة مخططات .sq»؟

تعريف الجداول وإنشاء واجهات Kotlin آمنة الأنواع تتمرن على Kotlin Multiplatform Academy مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.

هل أحتاج إلى خبرة سابقة لأبدأ Kotlin Multiplatform Academy؟

لا تُشترط خبرة سابقة. Kotlin Multiplatform Academy على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 1 من أصل 4.

كم من الوقت يستغرق درس «إضافة SQLDelight وكتابة مخططات .sq»؟

معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.

هل يمكنني كتابة وتشغيل أكواد في درس Kotlin Multiplatform Academy هذا؟

نعم. كل درس في Kotlin Multiplatform Academy يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.

جميع الدروس في هذه الدورة

  1. إضافة SQLDelight وكتابة مخططات .sq
  2. إعداد SqlDriver للمنصة
  3. إدراج الصفوف والاستعلام عنها وتحديثها
  4. مراقبة الجداول باعتبارها Flow
← العودة إلى Kotlin Multiplatform Academy