DJhon
DJhon

Reputation: 1518

SQLite database size getting increase

I am trying to run "Vacuum" command for SqLite database

     String sql = "VACUUM;" ;
    if(!IsSyncTriggered()){
        databaseInstance.execSQL(sql);
        databaseInstance.setTransactionSuccessful();
    }

My complete class

        public class AppServiceDBClean extends WakefulIntentService {   
int FIVE_MINUTES = 5*60*1000;
public AppServiceDBClean() {
    super("AppService");     
}       

@Override
protected void doWakefulWork(Intent intent) {
    // Toast.makeText(getApplicationContext(), "App services", 50000).show();

    try {
        doCleanUPDB();
    }
    catch (Exception e) {
        Log.e("AppService", "Exception appending to log file", e);
    }  
}
public boolean IsSyncTriggered() {

    boolean isActive = false;
    AccountManager am = AccountManager.get(getBaseContext());
    Account[] ac = am.getAccountsByType(Constants.ACCOUNT_TYPE);
    if (ac.length > 0) {
        isActive = ContentResolver.isSyncActive(ac[0], Constants.AUTHORITY);
    }
    return isActive;
}
public void doCleanUPDB(){

    /**
     * Create some basic instance of database interaction
     */
    DaoSession daoSessionUni = TAndroidApplicationContext.getInstance().getDaoSession();
    TAndroidApplicationContext tAndroidApplicationContext = TAndroidApplicationContext.getInstance();
    TDatabaseLibrary tDB = TDatabaseLibrary.getInstance(tAndroidApplicationContext.getApplicationContext());
    SQLiteDatabaseWrapper databaseInstance = tDB.getDatabase();
    String sql = "VACUUM;" ;
    if(!IsSyncTriggered()){
        databaseInstance.execSQL(sql);
        databaseInstance.setTransactionSuccessful();
        //databaseInstance.endTransaction();
    }else{
        /**
         * If the sync is in progress
         * wait for 10 minutes and try again
         * */
        try {
            Thread.sleep(FIVE_MINUTES);
        } catch (InterruptedException e) {
            Log.d("DataCleaningService","SleepException ",e);
            e.printStackTrace();     

        }
    }




}

}

IsSyncTriggered() is User-define method. I am trying to achieve this by a services which will run once in every 24 hour.But my database size is not decreasing . It remain goes increasing even some rows form table deleted. what i missed here Please help me . Thanks in advance to all.

Upvotes: 1

Views: 825

Answers (1)

reixa
reixa

Reputation: 7011

You can enable auto_vacuum and upgrade the bbdd. If you sync your data with a webservice or something like that you shouldn't have any problem. If you can't lose the data then try this:

Remove the last ";" from the command. I know it might sound stupid but all the info I´ve recovered about vacuum on sqlite appears without it, so maybe isn't even firing the command.

if(!IsSyncTriggered()){
    databaseInstance.execSQL("VACUUM");
    databaseInstance.setTransactionSuccessful();
    //databaseInstance.endTransaction();
}else{

There´s also a bug report for vacuum command on previous android versions such 2.3. If you're having this issue on that version or previous ones you should search more about that bug.

Bug report

Hope it helps.

Upvotes: 1

Related Questions