Search code examples
androidandroid-sqliteandroid-roomandroid-sql

Android room database won't export all the data


I'm trying to setup Room database backup functionality. Problem is that sql database file doesn't contain latest set of data in the app once downloaded. It always misses some most recent records. Is there a proper way to export room database? P.S. I didn't face similar problems when handled my db with sqliteHelper, so I suppose it must have something to do with Room.

Way I'm doing it:

@Throws(IOException::class)
private fun copyAppDbToDownloadFolder(address: String) {
    val backupDB = File(address, "studioDb.db") 
    val currentDB = applicationContext.getDatabasePath(StudioDatabase.DB_NAME)
    if (currentDB.exists()) {
        val src = FileInputStream(currentDB).channel
        val dst = FileOutputStream(backupDB).channel
        dst.transferFrom(src, 0, src.size())
        src.close()
        dst.close()
    }
}

Solution

  • I've solved it. When exporting (saving) sql database which you handle with Room, you have to export(and later import) both - your_database.bd and your_database.wal files. Later is a journal and afaiu keeps latest records.