备份室数据库 [英] Backup Room database
问题描述
我正在尝试以编程方式备份会议室数据库.
为此,我只是复制包含整个数据库的.sqlite
文件
但是,在复制之前,由于房间启用了预写日志记录,我们必须关闭数据库,以便将-shm
文件和-wal
文件合并为一个.sqlite
文件. 此处指出的
我在 RoomDatabase
对象上运行.close()
:
一切正常,但备份,但是稍后,当我尝试执行INSERT
查询时,出现此错误:
android.database.sqlite.SQLiteException: no such table: room_table_modification_log (code 1)
关闭会议室数据库后如何正确重新打开它?
PS:RoomDatabase
对象上的.isOpen()
在INSERT
true
房间版本:1.1.1-rc1
关闭后如何正确重新打开Room db?
很抱歉,这不能回答该问题.
但是,如果要将所有内容移到原始数据库文件中,则不必首先关闭数据库.您可以改为使用wal_checkpoint
编译指示强制检查点.
针对数据库查询以下语句.我们在这里使用原始查询,因为Room尚不支持pragma
(它将触发UNKNOWN query type
错误).在您的DAO中包含以下查询:
@RawQuery
int checkpoint(SupportSQLiteQuery supportSQLiteQuery);
然后,当您调用checkpoint方法时,请使用查询:
myDAO.checkpoint(new SimpleSQLiteQuery("pragma wal_checkpoint(full)"));
此链接可能会阐明wal_checkpoint
的功能. >
I'm trying to backup a room database programmatically.
For that, I'm simply copying the .sqlite
file that contains the whole database
But, before copying, due to the fact that room has write ahead logging enabled, we must close the database so that -shm
file and -wal
file merge into a single .sqlite
file. As pointed out here
I run .close()
on RoomDatabase
object:
Everything works fine with the backup, BUT, later on, when I try to execute an INSERT
query, I get this error:
android.database.sqlite.SQLiteException: no such table: room_table_modification_log (code 1)
How can I properly re-open room db after I close it?
PS: .isOpen()
on RoomDatabase
object returns true
before INSERT
Room version: 1.1.1-rc1
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.
这篇关于备份室数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!