Updating SQLite database android

陌路散爱 提交于 2019-12-06 17:32:26

Instead of uninstalling and installing the application again, simply increment the version number of your database to call onUpgrade method.

public Cook_tab_snacks_data(Context context) {
    super(context, DATABASE_NAME, null, 2);  //previously 1, now 2
}

as in your onUpgrade method, first it will delete the existing table and then will call onCreate method to recreate the table(s)

Your onUpgrade function is not getting called because the version of the database is not changing. If you increment the version number every time you make a change to the database, the onUpgrade function will drop it and recreate it.

The last input parameter to the super constructor is the database version number :

public Cook_tab_snacks_data(Context context) {
    super(context, DATABASE_NAME, null, 1);
}

onCreate of the SQLiteOpenHelper is called only when the database is created.

If you want to add values to the database, you can do so in an Activity or Service by instantiating your subclass, calling getWritableDatabase() and then calling insert in the same way you are doing in onCreate.

If you want to change the schema, you need to do that in onUpgrade.

The notepad example is a good place to learn about using a SQLiteDatabase in an app if you need some practice.

You have too because the database is created just once and if you want to change anything about it you have to uninstall it.

this is the way things works

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