0

I want to pre-populate the database in my KMM project. I’m using SQLDelight. I have this implementation for the Android side.

actual fun Scope.createDriver(): SqlDriver {

    val context = androidContext()
    val fileName = "dbFileName"
    val database: File = context.getDatabasePath(fileName)

    if (!database.exists()) {
        val inputStream = context.assets.open(fileName)
        val outputStream = FileOutputStream(database.absolutePath)

        inputStream.use { input ->
            outputStream.use {
                input.copyTo(it)
            }
        }
    }

    return AndroidSqliteDriver(NoteDatabase.Schema, context, fileName)
}

I tried this code on iOS but it’s not working.

val path = "test.db"
val fileManager = NSFileManager.defaultManager

if (!fileManager.fileExistsAtPath(path)) {
    val bundlePath = MR.assets.balagh_ul_quran.url.path.toString()
    fileManager.copyItemAtPath(bundlePath, toPath = path, error = null)

}

return NativeSqliteDriver(NoteDatabase.Schema, path)

Can anyone suggest something?

thanks!

1 Answers1

0
val path = "test.db"
val dbFile = File(DatabaseFileContext.databasePath(path, null))

Try that. DatabaseFileContext is a sqliter classe, just FYI.

Kevin Galligan
  • 16,159
  • 5
  • 42
  • 62
  • Thank you for the input, can you please explain a little bit.. – AhmadHassan Jun 06 '23 at 18:53
  • "dbFile" is the path. You need to copy your db files to that. Your question seemed to be about finding where the db should go. – Kevin Galligan Jun 07 '23 at 20:34
  • There’s no any File class which accepts parameter like this but instead FILE which accepts object of NativePtr. Actually I’m able to pre-populate db on Android side but nothing found regarding iOS. – AhmadHassan Jun 21 '23 at 06:27