Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

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.