如何在 SQLite android 中手动执行检查点? [英] How to manually perform checkpoint in SQLite android?

查看:23
本文介绍了如何在 SQLite android 中手动执行检查点?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建我的 sqlite 数据库的备份,我想先刷新数据库中的 WAL 文件的内容.

I'm trying to create a backup of my sqlite database and I want to flush the content of the WAL file in the db first.

这是我的 SQLiteOpenHelper:

Here is my SQLiteOpenHelper:

public class MyDBHelper extends SQLiteOpenHelper {

private Context mContext;
private static MyDBHelper mInstance = null;

private MyDBHelper(final Context context, String databaseName) {
    super(new MYDB(context), databaseName, null, DATABASE_VERSION);
    this.mContext = context;
}

@Override
public void onCreate(SQLiteDatabase db) {

}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

}
   public static MyDBHelper getInstance(Context context) {

    if (mInstance == null) {
        mInstance = new MyDBHelper(context, DATABASE_NAME);
    }
    return mInstance;
}

  private void closeDataBase(Context context) {
    getInstance(context).close();
}

}

现在,我的理解是在一个检查点完成后,mydb.db-wal 文件应该是空的.对吗?

Now, my understanding is that after a checkpoint is completed, the mydb.db-wal file should be empty. Is that correct?

这是我迄今为止尝试过的:1.

Here is what I've tried so far: 1.

    public Completable flushWalInDB() {
    return Completable.fromAction(new Action() {
        @Override
        public void run() throws Exception {
            getInstance(mContext).getReadableDatabase().rawQuery("pragma wal_checkpoint;", null);
        }
    });
}

这不会引发错误,但似乎没有做任何事情.运行此程序后,我物理检查了 mydb.db-wal 文件并具有相同的大小.我还检查了设备上的数据库,数据库中没有添加任何内容

This doesn't throw an error but doesn't seem to do anything. After running this, I physically checked my mydb.db-wal file and had the same size. I also checked the db on the device and nothing was added in the database

经过一番挖掘,我发现了这个[https://stackoverflow.com/a/30278485/2610933][1]并尝试了这个:

After some digging around I found this [https://stackoverflow.com/a/30278485/2610933][1] and tried this:

2.

    public Completable flushWalInDB() {
    return Completable.fromAction(new Action() {
        @Override
        public void run() throws Exception {
            getInstance(mContext).getReadableDatabase().execSQL("pragma wal_checkpoint;");
        }
    });
}

运行时报错:

unknown error (code 0): Queries can be performed using SQLiteDatabase query or rawQuery methods only.

并基于此答案 [https://stackoverflow.com/a/19574341/2610933][1] ,我也尝试 VACUUM 数据库,但似乎什么也没发生.

And based on this answer [https://stackoverflow.com/a/19574341/2610933][1] , I also tried to VACUUM the DB but nothing seems to happen.

 public Completable vacuumDb() {
    return Completable.fromAction(new Action() {
        @Override
        public void run() throws Exception {
            getInstance(mContext).getReadableDatabase().execSQL("VACUUM");
        }
    });
} 
}

在创建备份之前刷新数据库中的 WAL 文件的正确方法是什么?

Whats is the correct way of flushing the WAL file in the DB before creating a backup?

谢谢.

推荐答案

PRAGMA wal_checkpoint(2) 确实将 WAL 中的所有数据复制到实际数据库文件中,但它不会删除 -wal 文件,任何并发连接都可以在之后立即进行新的更改.

PRAGMA wal_checkpoint(2) does copy all data from the WAL into the actual database file, but it does not remove the -wal file, and any concurrent connections can make new changes right afterwards.

如果您想真正确定没有 WAL 干扰您的备份,请运行 PRAGMA journal_mode = 删除.(您可以稍后将其切换回来.)

If you want to be really sure that there is no WAL to interfere with your backup, run PRAGMA journal_mode = DELETE. (You can switch it back afterwards.)

这篇关于如何在 SQLite android 中手动执行检查点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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