Ask Your Question
0

What is the process to integrate SQLiter in KMM for both Android and IOS?

asked 2022-12-09 11:00:00 +0000

lalupa gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
3

answered 2021-11-14 17:00:00 +0000

lakamha gravatar image

The process to integrate SQLiter in KMM for both Android and IOS is as follows:

  1. Add the SQLiter dependency in the shared module's build.gradle file:
// 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")
      }
    }
  }
}
  1. Create a schema file for your database in the shared module's resources folder:
// Shared module resources folder 
CREATE TABLE users(
    id INTEGER PRIMARY KEY,
    name TEXT NOT NULL,
    age INTEGER NOT NULL
);
  1. Generate the SQLiter database code:

Run the following command in the terminal to generate the database code:

./gradlew generateSqlDelightInterface
  1. Use the generated code in your Kotlin code:
// 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.

edit flag offensive delete link more

Your Answer

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

Add Answer


Question Tools

Stats

Asked: 2022-12-09 11:00:00 +0000

Seen: 14 times

Last updated: Nov 14 '21