jeudi 30 juillet 2015

SQLite need Upgrade on FirstRun

im using SQLite OpenHelper to handle database version and it worked fine, my problem is, if i set some updates and new users install my app, the will not get the UPGRADES, because it will run the onCreate using the last DB_VERSION

explaining:

  private static final int DB_VERSION = 3;

Context context;

public DB(Context c) {

    super(c, DB_NAME, null, DB_VERSION);
    context = c;
}
    public void onCreate(SQLiteDatabase db) {
    db.execSQL("CREATE TABLE table1...");
    }

   @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        Log.d("DBUpgrade", "Atualizando DATABASE");
        switch (oldVersion) {
            case 2: {

            execSQLFromFile(db, "sql2");
            }
  case 3: {

            execSQLFromFile(db, "sql3");
            }


        }

scenarios:

User 1 has installed since first app release -> he will get the all the upgrades

User 2 installed for the first time in his device the last release of my apk that has the db_version 3 -> he will not get the upgrades, just the onCreate

ofc i know why this happens but i dont know how to solve it by a simple way, the only i way i throught was to call the onUpgrade(db, 0, DB_VERSION); this enter in the onUpgrade but dont call the cases inside the switch i dont know why, since i send 0, it should call case: 2, because i dont have the break isnt it?

BUT doing this i loose the OnUpgrade for the next version, it will always check the oldVersion, and redo the same code so it will crash because Unique column, alter columns anything

so i still ended without way to onfirst install do the upgrades too =(

Aucun commentaire:

Enregistrer un commentaire