Android SQLite database and app update

青春壹個敷衍的年華 提交于 2019-11-29 21:44:13

You should put all changes in your onUpgrade method you can use this code:

@Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    String sql = "ALTER TABLE " + TABLE_SECRET + " ADD COLUMN " +
     "name_of_column_to_be_added" + " INTEGER";
    db.execSQL(sql);        
}        

this adds a column in your current database. Your database will not lose data. Reminder: onUpgrade will be called when getWriteableDatabase or getReadableDatabase is executed AND the version of your database is different from your older version.

Change your db version and add extra two table creation methods in your onUpgrade(SQLiteDatabase db, int oldVersion, int newVesion) method. Now the the previous data will not get lost.

I hope this will help you.

You can do some on SQLiteOpenHelper#onUpgrade, get some old data. and insert into new table

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!