SQLDelight for Kotlin: Complete Guide with Examples
\n\n1. What It Is and Why You Need It
\nSQLDelight is a library for Kotlin that generates type-safe Kotlin code from SQL queries. It was developed by Cash App (Square) and supports Kotlin Multiplatform (KMP), allowing you to use the same code for working with databases on Android, iOS, JVM, JS, and Native platforms.
\nThe main idea of SQLDelight is that you write SQL queries in .sq files, and the library automatically generates Kotlin classes, interfaces, and functions that fully correspond to your queries. This eliminates the need to write boilerplate code for mapping query results to objects and handles SQL injections at the compilation level.
Why use SQLDelight? First, it provides full type checking at compile time: if you change the table structure, the compiler will immediately point out all places where queries need to be updated. Second, it supports migrations, transactions, asynchronous queries, and multi-platform work. Third, it integrates with popular frameworks such as Ktor, Koin, Dagger/Hilt, and others.
\n2. Installation
\nSQLDelight is installed via the Gradle build system. For Kotlin Multiplatform, add the following plugin to the root build.gradle.kts:
// Root build.gradle.kts\nplugins {\n id("app.cash.sqldelight") version "2.0.2" apply false\n}\n\n// In the module (e.g., :shared)\nplugins {\n id("app.cash.sqldelight")\n}\n\nsqldelight {\n databases {\n create("MyDatabase") {\n packageName.set("com.example.db")\n }\n }\n}\n\ndependencies {\n implementation("app.cash.sqldelight:coroutines-extensions:2.0.2")\n implementation("app.cash.sqldelight:runtime:2.0.2")\n // For Android\n implementation("app.cash.sqldelight:android-driver:2.0.2")\n // For iOS\n implementation("app.cash.sqldelight:native-driver:2.0.2")\n // For JVM\n implementation("app.cash.sqldelight:sqlite-driver:2.0.2")\n}\n\nAfter Gradle sync, create the file src/commonMain/sqldelight/com/example/db/MyDatabase.sq (the path must match the packageName).
3. Quick Start — Minimal Working Example
\nCreate the file MyDatabase.sq:
-- MyDatabase.sq\nCREATE TABLE Person (\n id INTEGER PRIMARY KEY AUTOINCREMENT,\n name TEXT NOT NULL,\n age INTEGER NOT NULL\n);\n\nselectAll:\nSELECT * FROM Person;\n\ninsertPerson:\nINSERT INTO Person(name, age) VALUES (?, ?);\n\nNow in Kotlin code:
\nimport com.example.db.MyDatabase\nimport app.cash.sqldelight.db.SqlDriver\nimport app.cash.sqldelight.driver.jdbc.sqlite.JdbcSqliteDriver\n\nfun main() {\n val driver: SqlDriver = JdbcSqliteDriver("jdbc:sqlite:test.db")\n MyDatabase.Schema.create(driver)\n \n val database = MyDatabase(driver)\n val queries = database.myDatabaseQueries\n \n // Insert\n queries.insertPerson("Alice", 30)\n queries.insertPerson("Bob", 25)\n \n // Select\n val people = queries.selectAll().executeAsList()\n people.forEach { person ->\n println("${person.name} is ${person.age} years old")\n }\n}\n\nThis code will create the table, insert two records, and display them.
\n4. Main Methods/Functions/Classes
\n\n4.1 executeAsList()
\nSignature: fun <T> executeAsList(): List<T>
What it does: Executes a SELECT query and returns all result rows as a list of objects of the generated type.
\nval allPeople: List<Person> = querie Also in library
Sending SMS in PHP: API libraries of sms.ru and other services
Tower for Rust: Complete Guide with Examples
Generating PDF Reports in C# (.NET): iTextSharp, QuestPDF, PDFsharp
Working with Dates in Java: Overview of java.time, Joda-Time, and Apache Commons Libraries