lundi 15 février 2016

Android force database update to

I have a class

public class DbHelper extends SQLiteOpenHelper {

    private static DbHelper mDbHelper = null;
    private final static String TAG = DbHelper.class.getClass().getSimpleName();

    public DbHelper(Context context) {
        super(context, DBManifest.DB_NAME, null, DBManifest.DB_VERSION);
    }

    public static DbHelper getInstance() {
        if (mDbHelper == null)
            mDbHelper = new DbHelper(CommonApplication.mainContext);

        return mDbHelper;
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        Log.d(TAG, "onCreate called. Current db version = " + db.getVersion());
        for (String query : DBManifest.CREATE_TABLE_QUERY_LIST)
            db.execSQL(query);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        Log.d(TAG, "onUpgrade called from " + oldVersion + " to " + newVersion + " version.");
        executeMigration(db, oldVersion);
    }


 private void executeMigration(SQLiteDatabase db, int oldVersion) {
        switch (oldVersion) {
            case 0:
            case 1:
            case 2:
                upgradeV2(db);
            case 3:
                upgradeV3(db);
            case 4:
                upgradeV4(db);
            case 5:
                upgradeV5(db);
            case 6:
                upgradeV6(db);
                break;
        }
        db.setVersion(DBManifest.DB_VERSION);
    }

  // migration methods
  // ...
}

My problem is when I install my application at the first time I need to force update my database to 6 version. Is it possible and how to do that? Code above is not working. Database version is stay 0.

Aucun commentaire:

Enregistrer un commentaire