开发者

SQLite updation upon android app's update

I have an app released on the android market which uses sqlite and displays data. I want to know if I am sending an update for the app by adding features, should I upload a new database? What happens when users already having my app, click on update? Will the database update on its own? If the users are downloading my app for the first time, they will certainly get the new db content...but开发者_开发问答 what about the existing users?? I want to know if I have to explicitly update the database in the program


When you create your new version... If you change the version of the database... The onUpgrade function will run on all the existing users:

public static final int dbVersion = 2;
protected static class DatabaseHelper extends SQLiteOpenHelper { 
    public DatabaseHelper(Context context) {
        super(context, dbName, null, dbVersion);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        //create tables
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        //what do you want to do to existing users? maybe recreate?
    }
}


Why not create the sqlite database through code? That way your updates can update the SQL on the database (alter columns, add rows) without affecting the users existing data.


If you are changing database's attributes then on updation it will create problem and if the database's attributes are same then it will not have any effect...


You may use android.database.sqlite.SQLiteOpenHelper class it supports versions and migration mechanism

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜