How to export room db with sqlcipher from android studio

Hi,

I have a room database encrypted with sqlcipher and would like to ship this encrypted database as an asset to open it on first app start with createFromAsset().
I try to export the encrypted database file from the emulator in android studio via the file explorer.
The problem is, that the encrypted database file is empty. Please see the attached screenshot, it’s only of size 4KB.
It should be around 3MB and the app itself works fine, so the database must contain all the data.
Yet I am unable to export the database file.

This works when I remove sqlcipher. It also worked when I used sqlcipher with dbflow in previous years.

room_version = “2.6.1”
sqlcipher_version = “4.6.1”

Thank you for your help and best regards,
Thomas

Hi @accursedm0,

Historically, the Room API has not supported interfacing with the provided support helper when using createFromAsset. This issue was raised with the Room API team here almost 5 years ago. My recommendation would be to perform the copy from assets directory outside of the Room API itself.

Thank your for your answer!

I’m afraid, there is a misunderstanding though.
My problem is that I don’t yet have the populated AppDatabase.db file which I’d like to place in the assets directory.
Although the app is running fine, so the database should be populated, the AppDatabase.db file under “data/data/myapp/database” is empty with 4KB.
I noticed that the AppDatabase.db-wal file seems to contain the right amount of data with 3.2MB.

Now I followed the advice from here: https://stackoverflow.com/questions/50987119/backup-room-database
And manually created a checkpoint with myDAO.checkpoint(SimpleSQLiteQuery("pragma wal_checkpoint(full)"));
Creating a checkpoint seems to write the data from the AppDatabase.db-wal file to the AppDatabase.db file which I could now export via the device file explorer and then even use with createFromAsset(). So this step does work I’m glad to report.

This is a workaround to create the initial AppDatabase.db file but:

1 I have a line of code, which I should not forget to uncomment for the production version.

2 It looks like without scqlcipher, creating this checkpoint happened automatically pretty quickly and with sclcipher it doesn’t happen at all. Even when I close the app.
Should I be concerned about that?

Hi @accursedm0,

Yes, I did misunderstand your original problem statement, thank you for the clarification. When using WAL mode, SQLite will move the transactions persisted in the WAL file into the back into the database during a checkpoint. By default, a checkpoint occurs when the WAL file reaches 1000 pages, or when the last connection to the database file is closed. Of course you can trigger the process manually as you have done too.