The Problem:
A developer is working on a Jetpack Compose Desktop application and is facing difficulties in finding reliable and up-to-date information about database storage libraries compatible with the framework. While Room, a popular option for Android development, is not suitable for desktop applications, the developer has heard of sqldelight as a potential solution. However, they are unable to locate comprehensive resources or documentation for setting up and using sqldelight in a Jetpack Compose Desktop environment. This lack of information hinders their progress and prevents them from implementing database storage effectively in their application.
The Solutions:
Solution 1: Using SqlDelight
SqlDelight is a multiplatform SQL database engine for Kotlin, Swift, and Java. It provides a simple, type-safe API for working with databases. To use SqlDelight in a Jetpack Compose Desktop application, follow these steps:
- Add the SqlDelight dependencies to your build.gradle.kts file.
“`kotlin
kotlin {
jvm {
jvmToolchain(Deps.JDK)
withJava()
}sourceSets { val commonMain by getting { dependencies { implementation(Deps.Koin.CORE) //Database with(Deps.SQLDelight) { implementation(DRIVER) api(RUNTIME) api(COROUTINE_EXT) } } } val jvmMain by getting }
}
sqldelight {
databases {
create("AppDatabase") {
packageName.set("com.mobiledevpro.database")
}
}
}</li> <li><b>Create a database schema.</b> ```kotlin object SQLDelight { const val DRIVER = "app.cash.sqldelight:sqlite-driver:${Versions.SQL_DELIGHT}" const val RUNTIME = "app.cash.sqldelight:runtime:${Versions.SQL_DELIGHT}" const val COROUTINE_EXT = "app.cash.sqldelight:coroutines-extensions:${Versions.SQL_DELIGHT}" }
- Generate the database classes.
Run the following command from the command line:
“`
./gradlew sqldelightRun
“` - Create a database instance.
“`kotlin
val database = AppDatabase.invoke(driverFactory = AndroidSqliteDriverFactory(connectionSource))
“` - Use the database.
You can now use the database to perform CRUD operations. For example, to insert a new row into the database, you can use the following code:
“`kotlin
database.personQueries.insert(“John”, 30)
“`
Q&A
Resource for SQLDelight with Jetpack Compose Desktop?
This link might be helpful: https://cashapp.github.io/sqldelight/
How to use SQLDelight in Jetpack Compose Desktop?
Build.gradle.kts setup with SQLDelight and Kotlin.
How to setup SQLDelight in Jetpack Compose Desktop?
Add SQLDelight dependencies and configure build.gradle.kts.
Video Explanation:
The following video, titled "#kotlin #kmm Multiplatform Live Coding w/ Android ...", provides additional insights and in-depth exploration related to the topics discussed in this post.
#kotlin #kmm Multiplatform Live Coding w/ Android, #JetpackCompose: Desktop SqlDelight Database. 1.5K views · Streamed 2 years ago ...more ...
The following video, titled "#kotlin #kmm Multiplatform Live Coding w/ Android ...", provides additional insights and in-depth exploration related to the topics discussed in this post.
#kotlin #kmm Multiplatform Live Coding w/ Android, #JetpackCompose: Desktop SqlDelight Database. 1.5K views · Streamed 2 years ago ...more ...