Upgrading database version automatically

帅比萌擦擦* 提交于 2019-12-07 16:04:36

Changing a row in the database should not affect the schema version. This should be executed as proper queries to manipulate the database entries.

You might find use of this method:

public void clearDatabase(Context context) {
   DatabaseHelper helper = new DBHelper(context);
   SQLiteDatabase database = helper.getWritableDatabase();
   database.delete(DATABASE_TABLE, null, null); //erases everything in the table.
   database.close();
}

First of all here is how you can change the database version programmatically and hence force the SQLiteOpenHelper to call onUpgrade() on the next call to open the database:

db.execSQL("PRAGMA user_version = " + NEW_VERSION);

Second, I don't recommend using this technique. I only wrote this answer cause you asked about how to do it this way. It's your choice. For me I'll do it the way @Boris Strandjev recommends in his answer.

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