Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android sqlite flush WAL file contents into main database file

I'm trying to create a backup of my apps database contents and for most devices it works fine but a few have wal mode enabled by default which causes an issue. From everything I've read calling "pragma wal_checkpoint" should flush the contents of a -wal file into the main database file which is what I'm after. Flush the contents to main db file and then copy the db file for backup. I'm calling

db.rawQuery("pragma wal_checkpoint;", null);

but it doesn't seem to be working. Any ideas?

like image 471
Paul Avatar asked May 16 '15 14:05

Paul


2 Answers

rawQuery() returns a cursor; the query is not actually executed until you try to read from the cursor.

To execute an SQL statement that does not return a cursor, use execSQL() instead.

like image 56
CL. Avatar answered Nov 20 '22 15:11

CL.


It's worked for me to flush wal file to main database.

        String query = "pragma wal_checkpoint(full)";
 
        Cursor cursor = db.rawQuery(query, null);
        if (cursor != null && cursor.moveToFirst()) {
            int a = cursor.getInt(0);
            int b = cursor.getInt(1);
            int c = cursor.getInt(2);

        }
        if (cursor != null) {
            cursor.close();
        }
        db.close();
like image 3
SK. Fuad Avatar answered Nov 20 '22 15:11

SK. Fuad