7

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?

Paul
  • 1,714
  • 7
  • 22
  • 45
  • Executing `pragma wal_checkpoint;` will return a single row with three columns. You can check those column values to determine exactly what the result of the call is. For example, from the doc *The third column is the number of pages in the write-ahead log file that have been successfully moved back into the database file at the conclusion of the checkpoint.* – pathfinderelite May 16 '15 at 14:28
  • Running the command via sql command prompt works as expected but that command from inside my app does not. – Paul May 16 '15 at 16:34

2 Answers2

5

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();
SK. Fuad
  • 371
  • 4
  • 9
3

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.

CL.
  • 173,858
  • 17
  • 217
  • 259