2012-08-10 110 views
0

在確實啓用了WAL(預寫日誌記錄)的這些少數Android設備上,我確實有數據庫備份/恢復方面的問題。在創建這樣的數據庫之後是否可以關閉它?是否可以在Android SQLite數據庫上禁用WAL?

我的想法是:

1)的包裝,使得在舊版Android裝置這項工作:在擴展應用)

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; 
    } 
} 

2.我的AsyncTask將問題進行了充分的檢查點和開關WAL關閉:

public class MyApplication extends Application { 

    private class MyAsyncTask extends AsyncTask<Void, Void, Boolean> { 

     @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 >= 16) { 
        sqliteDatabaseWalWrapper = new SQLiteDatabaseWalWrapper(sqliteDatabase); 
        if (sqliteDatabaseWalWrapper != null && sqliteDatabaseWalWrapper.checkAvailable() && sqliteDatabaseWalWrapper.isWriteAheadLoggingEnabled()) { 
         task = new MyAsyncTask(); 
         task.execute(); 
        } 
       } 
      } 
     } 
    } 

    @Override 
    public void onTerminate() { 
     if (sqliteDatabase != null) { 
      sqliteDatabase.close(); 
     } 

     if (sqliteOpenHelper != null) { 
      sqliteOpenHelper.close(); 
     } 

     super.onTerminate(); 
    } 
} 

這項工作?這會刪除SQLite在啓用WAL的數據庫上創建的各種附加文件嗎?這是否會導致Android文檔中描述的單個數據庫文件?

回答

相關問題