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.
Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss
Asked: 2022-12-09 11:00:00 +0000
Seen: 14 times
Last updated: Nov 14 '21
Why can't IntelliJ IDEA detect the Kotlin multiplatform library?
What is the correlation between coroutines and the Kotlin Gradle Plugin?
What are the typical approaches found in a Gradle build script?
Why isn't the date picker dialog displayed in an Android fragment using Kotlin in Android Studio?
What is the meaning of the error message "Task :react-native-gradle-plugin:compileKotlin FAILED"?
Is it necessary to have an updated Kotlin Gradle plugin for your project?
How can we avoid the duplication of Kotlin class jetified-kotlin-stdlib-1.8.10 in Android (Java)?
How can build variables be accessed in code in Kotlin multiplatform?
Can a transitive dependency in gradle be made exclusively for runtime purposes?