1 | initial version |
The process to integrate SQLiter in KMM for both Android and IOS is as follows:
// Shared module build.gradle
kotlin {
targets {
android()
iosX64("ios")
}
sourceSets {
val commonMain by getting {
dependencies {
implementation("com.squareup.sqldelight:runtime:$sqldelight_version")
}
}
val androidMain by getting {
dependencies {
implementation("com.squareup.sqldelight:android-driver:$sqldelight_version")
}
}
val iosMain by getting {
dependencies {
implementation("com.squareup.sqldelight:native-driver:$sqldelight_version")
}
}
}
}
// Shared module resources folder
CREATE TABLE users(
id INTEGER PRIMARY KEY,
name TEXT NOT NULL,
age INTEGER NOT NULL
);
Run the following command in the terminal to generate the database code:
./gradlew generateSqlDelightInterface
// Shared module Kotlin code
import com.sample.db.Database
val driver = NativeSqliteDriver(Database.Schema, "my.db")
val database = Database(driver)
database.userQueries.insertOrUpdateUser(1, "John Doe", 30)
val user = database.userQueries.getUserById(1).executeAsOne()
println(user.name) // Output: John Doe
That's it! Now you can use SQLiter in your KMM project for both Android and iOS platforms.