EDIT: sorry, wrong channel. I moved this to the ro...
# random
p
EDIT: sorry, wrong channel. I moved this to the room channel. I didn't delete this because it already haves a thread started: I'm having a problem with my room database, it is creating 4 files:
bus.db, bus.db.lck, bus.db-shm and bus.db-wal
If I open the
.db
file with a sqlite browser on that folder (with these other 3 files present) it is correct and has all the data, but if I copy the
.db
alone into another folder, then, when I open it with an sqlite browser, it doesn't have all the data, a lot of rows are missing on a table with 26.000 rows. If I copy the other 3 files too, and open the
.db
file with the sqlite browser, then the sqlite browser sees all the data. Can someone explain me why this happens and how to solve it? I need to export my
.db
file so I need that when I copy it it haves all the data without depending on other 3 files. How I create the database:
Copy code
fun createDatabaseBuilder(): RoomDatabase.Builder<BusStopsDatabase> {
    val dbFile = File(Util.getAppDataPath()+"data/database/bus.db")
    return Room.databaseBuilder<BusStopsDatabase>(name = dbFile.absolutePath)
}
fun RoomDatabase.Builder<BusStopsDatabase>.getRoomDatabase(): BusStopsDatabase {
    return this
        .fallbackToDestructiveMigration(dropAllTables = true)
        .setDriver(BundledSQLiteDriver())
        .setQueryCoroutineContext(Dispatchers.IO)
        .build()
}
e
but your export should be done with https://www.sqlite.org/lang_vacuum.html#vacuuminto anyway, which works completely fine with wal
p
can you tell me how to do that in my Room database?
e
just send the sql command to the underlying database
Room doesn't hide it, it's right there on the RoomDatabase type
Copy code
database.openHelper.writableDatabase.execSql("VACUUM INTO ?", arrayOf(file.path))
p
@ephemient I don't have any openHelper, with room we only have the DAO. I noticed I posted this on the wrong channel. I missread random with room.
I doublechecked it, seems that long before did exist a getOpenHelper but that was deprecated and removed from RoomDatabase today
p
I don't know what is that and where to get it
I only know what documentation says and what I see on my android studio ide
The *android.arch* Architecture Components packages are no longer maintained. They have been superseded by the corresponding androidx.* packages. See androidx.room.RoomDatabase instead.
if you go to the new version open helper does not exist: https://developer.android.com/reference/androidx/room/RoomDatabase
I don't see any get method for it
e
openhelper is android-only and the docs are missing multi-platform-specific bits
anyhow it's not a Kotlin issue with #C7KS458SJ
p
so, there is no solution?
is not possible to merge all the content of the database into one simple .db?
e
sure there is. use openHelper on Android, or useWritableConnection if you are on the 2.7 multiplatform alphas
p
I'm on compose desktop, multiplatform
Copy code
androidx-room = "2.7.0-alpha11"
that is my room version
how can I use that
useWritableConnection
to solve this?
e
the docs are there…
Copy code
database.useWriterConnection { connection ->
    connection.usePrepared("VACUUM INTO ?") { statement ->
        statement.bindText(1, "path")
        while (statement.step()) {}
    }
}
p
thank you
that while is empty
what should I write there?
e
nothing. vacuum doesn't return anything, you just want to step the cursor until it's done
p
it seems to be like a hacking patch
does not exist another way to do this?
more similar to the first you proposed?
database.openHelper.writableDatabase.execSql("VACUUM INTO ?", arrayOf(file.path))
that empty while seems to be the typical thing will change very soon, it's like a ugly patch
well, it also gives me error
Exception in thread "DefaultDispatcher-worker-10" androidx.sqlite.SQLiteException: Error code: 1, message: output file already exists
I put the path of my app .db database
is not the correct thing to do?
e
don't overwrite your app db database. use it to make a copy for export.
p
it seems to work perfectly, thank you very much, it's a shame that room doesn't offer a common way to do this... that method is very ugly and strange
probably will be deleted or changed soon
do you know another way to achieve the same without using this trick?