备份室数据库 [英] Backup Room database

查看:109
本文介绍了备份室数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试以编程方式备份​​会议室数据库.

为此,我只是复制包含整个数据库的.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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆