We will cover some general and not so general workarounds and practices, which can be put to good use depending on the situation. For instance, in some cases, we need to have a prepopulated database of values that we will make use of in our Android application or upgrading a database, which seems trivial but can break our application.
In Chapter 2, Connecting the Dots, we used onUpgrade()
to show how a database is updated. If we go back to the example, you will notice that it executes a Drop Table
command. What will happen here is that the original table will be dropped and a new table will be created by the call, onCreate()
. This will lead to a loss of the existing data and hence is not suitable if we need to alter our database. The onUpgrade()
function can be defined as follows:
public void onUpgrade(SQLiteDatabase db, int oldVersion,int newVersion) { String DROP_TABLE = "DROP TABLE IF EXISTS " + TABLE_NAME; db.execSQL(DROP_TABLE); onCreate...