开发者

How do I know when to update the database - Android

Hey I'm building an Android app that contains a database. My question is how can I know whether the app was updated? I mean, I know the onUpgrade method is called when the DATABASE_VERSION from -

public DataBaseHel开发者_JAVA百科per(Context context) {        
    super(context, DB_NAME, null, DATABASE_VERSION);
    this.myContext = context;
}

is lower than the version of the app, but how do I increment it after the update, so the app won't update itself all the time?


You don't need to take care of keeping track of the version number. After onUpgrade() has been called, android takes care of all this stuff automatically. onUpgrade() will automatically be called when the next update is due (i.e. you increased DATABASE_VERSION once again).

To be even more clear: Just keep a static final int DATABASE_VERSION field, which you increase at development time, everytime you change something essential on the database structure.

You create a class that extends SQLLiteOpenHelper which basically looks like this:

public class ContentDatabase extends SQLiteOpenHelper {

  // Whenever you change the DB structure, increment DATABASE_VERSION (it starts from 1, so  your first upgrade should be 2)
  // - note it's only used for upgrades; if it's a new install, onUpgrade won't be called and everything is done by onCreate instead
  private static final int DATABASE_VERSION = 6;

  public ContentDatabase(Context context) {
    super(context, DATABASE_NAME, null, DATABASE_VERSION);
  }

  public void onCreate(SQLiteDatabase db) {
    // Code to create the most recent version of your database 
    // i.e. CREATE TABLE xxx (....) 
  }

  @Override
  public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    // Code to update an existing database structure to the most recent 
    // version. 

    if (oldVersion < 2) {
      // do something to upgrade the DB to version 2
    }

    if (oldVersion < 3) {
      // do something to upgrade the DB to version 3
    }

    if (oldVersion < 6) {
      // Let's assume you added a new table in v4, and then added a field to that new table in v6
      if (oldVersion < 4) {
        // add the v6 version of the new table to the DB
      }
      else {
        // add the new field to the existing v4 table in the DB
      }
    }

  }
}

Everytime you need to change the structure of the table (i.e. add aditional columns or tables) you increase the DATABASE_VERSION variable by one and write code accordingly for the onCreate() and onUpdate() methods. These methods are called automatically by android.


Consider using SharedPreferences in the application context, you can keep track of lastVersion in there and your application will check it onCreate() to see if lastVersion matches the version of this application.

It is persistent between updates but not between installs.

you can make it so version 0 equates to not having it which you require you to setup the new database. By using SharedPreference mPref = getSharedPreference(String,int) You can then have code like

if(mPref.getInt("DB_VERSION", 0) == 0 ) { 
  no db yet so create one
} else {  db is there, 
    if (mPref.getInt("DB_VERSION",0) != DATABASE_VERSION) { 
             do some special update stuff here
    } 
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜