Hello I want to pre-populate the database in my KM...
# squarelibraries
a
Hello I want to pre-populate the database in my KMM project. I’m using SQLDelight. I have this implementation for the Android side.
Copy code
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.
Copy code
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)
then tried this implementation:
Copy code
actual fun Scope.createDriver(): SqlDriver {

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

    val documentsDirectory = fileManager.URLsForDirectory(NSDocumentDirectory, inDomains = NSUserDomainMask).first() as NSURL
    val destinationURL = documentsDirectory.URLByAppendingPathComponent(databaseName)

    val bundlePath = MR.assets.balagh_ul_quran.url

    if (!fileManager.fileExistsAtPath(destinationURL?.path.toString())) {
        val response = try {
            if (destinationURL != null) {
                fileManager.copyItemAtURL(bundlePath, toURL = destinationURL, error = null)
            } else {

            }
        } catch (e: Exception) {
            showLog("catch:${e.message}")
        }

        println("myLog: $response")
    }

    return NativeSqliteDriver(NoteDatabase.Schema, destinationURL?.path.toString())
}
But nothing worked. Can anyone provide any sample app or docs something? thanks!