Backup Room database

后端 未结 7 823
夕颜
夕颜 2020-12-08 01:41

I\'m trying to backup a room database programmatically.

For that, I\'m simply copying the .sqlite file that contains the whole database

But, bef

7条回答
  •  猫巷女王i
    2020-12-08 01:54

    How can I properly re-open room db after I close it?

    I am sorry that that this doesn't answer that question.

    But if moving everything to the original database file is what you want to do, then you don't have to close the database in the first place. You can force a checkpoint using the wal_checkpoint pragma instead.

    Query the following statement against the database. We use raw queries here as pragma is not yet supported by Room (it will trigger a UNKNOWN query type error). Have this query inside of your DAO:

    @RawQuery
    int checkpoint(SupportSQLiteQuery supportSQLiteQuery);
    

    And then when you call the checkpoint method, use the query then:

    myDAO.checkpoint(new SimpleSQLiteQuery("pragma wal_checkpoint(full)"));
    

    This link may shed some light on what wal_checkpoint does.

提交回复
热议问题