How do I force sqlite to merge the log file into the main database?

I have an application that writes to databases with transactions. My understanding is that sqlite will write to a log file and then merge it as soon as the log file reaches a certain size. I also have functionality that will try to copy the database file to a different location. My problem is that I cannot find a way to get sqlite to merge the log into the main database.

All my queries look something like this.

SQLiteDatabase database =null;
try{
    database = getReadableDatabase();

    //PERFORM SOME INSERT

   }
   catch (Exception e){
            Log.e(TAG, "Error with insert");
   }
   finally {
        if( database != null){
            database.close();
        }
  }

      

After completing these methods, I will close the application and look around the directory where the application is saving its database. I can see the database file and the database log.

I am trying to do something like

public void copyDatabase(){
    //FORCE MERGE

    //DO OTHER OPERATIONS
}

      

Does anyone have any ideas on how to merge the log file when needed?

+3


source to share


1 answer


The log contains only information about uncommitted transactions. If you are sure the database is not open anywhere, the log file is not used and you can simply copy the main database file.

More details

Sqlite has several log modes including DELETE

one that deletes the log when the last transaction completes.

However, the default log mode under android PERSIST

means that the log file is not automatically deleted by the database engine, even if it is not currently in use.



In android mode, you can change the log mode: Android SQLite - change log_modeg

db.rawQuery("PRAGMA journal_mode=OFF", null).close();

      

Pay attention to the warning :

When using enableWriteAheadLogging (), mod_log is automatically managed by this class. Thus, do not set journal_mode with PRAGMA statement journal_mode if your application uses enableWriteAheadLogging ()

+3


source







All Articles