title: Is it possible to disable WAL on Android SQLite database?
body: <p>I do have a lot of problems with database backup/restore on these few Android devices that do have WAL (write ahead logging) enabled. Is it possible to switch that off after such a database has been created?</p>
  
  <p>My idea is:</p>
  
  <p>1.) A wrapper that makes this work on older Android devices:</p>
  
  <pre><code>public class SQLiteDatabaseWalWrapper {
  
      private boolean        available;
      private Method         disableWriteAheadLogging;
      private Method         enableWriteAheadLogging;
      private Method         isWriteAheadLoggingEnabled;
      private SQLiteDatabase sqliteDatabase;
  
      public SQLiteDatabaseWalWrapper(final SQLiteDatabase sqliteDatabase) {
          this.sqliteDatabase = sqliteDatabase;
  
          available = false;
  
          try {
              disableWriteAheadLogging = SQLiteDatabase.class.getMethod("disableWriteAheadLogging");
              enableWriteAheadLogging = SQLiteDatabase.class.getMethod("enableWriteAheadLogging");
              isWriteAheadLoggingEnabled = SQLiteDatabase.class.getMethod("isWriteAheadLoggingEnabled");
  
              available = true;
          } catch (NoSuchMethodException noSuchMethodException) {
          }
      }
  
      public boolean checkAvailable() {
          return available;
      }
  
      public void disableWriteAheadLogging() {
          if (disableWriteAheadLogging != null) {
              try {
                  disableWriteAheadLogging.invoke(sqliteDatabase);
              } catch (IllegalAccessException illegalAccessException) {
              } catch (IllegalArgumentException illegalArgumentException) {
              } catch (InvocationTargetException invocationTargetException) {
              }
          }
      }
  
      public boolean enableWriteAheadLogging() {
          boolean result = false;
  
          if (enableWriteAheadLogging != null) {
              try {
                  result = (Boolean) enableWriteAheadLogging.invoke(sqliteDatabase);
              } catch (IllegalAccessException illegalAccessException) {
              } catch (IllegalArgumentException illegalArgumentException) {
              } catch (InvocationTargetException invocationTargetException) {
              }
          }
  
          return result;
      }
  
      public boolean isWriteAheadLoggingEnabled() {
          boolean result = false;
  
          if (isWriteAheadLoggingEnabled != null) {
              try {
                  result = (Boolean) isWriteAheadLoggingEnabled.invoke(sqliteDatabase);
              } catch (IllegalAccessException illegalAccessException) {
              } catch (IllegalArgumentException illegalArgumentException) {
              } catch (InvocationTargetException invocationTargetException) {
              }
          }
  
          return result;
      }
  }
  </code></pre>
  
  <p>2.) Within an extended Application with AsyncTask I will issue a FULL checkpoint and switch WAL off:</p>
  
  <pre><code>public class MyApplication extends Application {
  
      private class MyAsyncTask extends AsyncTask&lt;Void, Void, Boolean&gt; {
  
          @Override
          protected Boolean doInBackground(final Void... voids) {
              // Make a full checkpoint
              sqliteDatabase.execSQL("PRAGMA wal_checkpoint(FULL);");
  
              // switch WAL off
              sqliteDatabaseWalWrapper.disableWriteAheadLogging();
          }
  
          @Override
          protected void onPostExecute(final Boolean result) {
          }
      }
  
      private Context                  context;
      private SQLiteDatabase           sqliteDatabase;
      private SQLiteDatabaseWalWrapper sqliteDatabaseWalWrapper;
      private MySQLiteOpenHelper       sqliteOpenHelper;
      private MyAsyncTask              task;
  
      @Override
      public void onCreate() {
          super.onCreate();
  
          context = getApplicationContext();
  
          sqliteOpenHelper = new MySQLiteOpenHelper(context);
          if (sqliteOpenHelper != null) {
              sqliteDatabase = sqliteOpenHelper.getWritableDatabase();
              if (sqliteDatabase != null) {
  
                  if (Build.VERSION.SDK_INT &gt;= 16) {
                      sqliteDatabaseWalWrapper = new SQLiteDatabaseWalWrapper(sqliteDatabase);
                      if (sqliteDatabaseWalWrapper != null &amp;&amp; sqliteDatabaseWalWrapper.checkAvailable() &amp;&amp; sqliteDatabaseWalWrapper.isWriteAheadLoggingEnabled()) {
                          task = new MyAsyncTask();
                          task.execute();
                      }
                  }
              }
          }
      }
  
      @Override
      public void onTerminate() {
          if (sqliteDatabase != null) {
              sqliteDatabase.close();
          }
  
          if (sqliteOpenHelper != null) {
              sqliteOpenHelper.close();
          }
  
          super.onTerminate();
      }
  }
  </code></pre>
  
  <p>Will this work? Will this delete the various additional files that SQLite does create on WAL-enabled databases? Will this lead back to that single database file that's described in the Android docs?</p>