Reputation: 1944
I have a working app which is already published to google play store. I am using sqllite database in my app to store data. I am having a problem as following.
Currently my app version for eg 1.1 it has only one table. which i create inside onCreate() method of my data helper class.
In update 1.2 i added one more table to my database which i created inside onUpgrade() method. like following
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
{
db.execSQL(ShortCode.TABLE_CREATE_STATEMENT);
onCreate(db);
}
now what i have observer if a user has previously installed 1.1 in his mobile and he updates to 1.2, both table is created in user can use app without any problem. but if user install fresh copy or directly 1.2 app gets crashed. What can be done to avoid this. Any help will be appreciated. Thanks in advance.
Upvotes: 1
Views: 1258
Reputation: 91
`@Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { db.execSQL("DROP TABLE IF EXISTS " + TABLE_Old1_TABLE); db.execSQL("DROP TABLE IF EXISTS " + TABLE_Old2_TABLE); onCreate(db); }
And in your onCreate() method you create a new table like this-
`String CREATE_New_Table = "CREATE TABLE IF NOT EXISTS " + TABLE_New_TABLE + "(" + KEY_ID
+ " INTEGER PRIMARY KEY," + Id + " TEXT, " + Address + " TEXT, " + AppName + " TEXT " + ")";
db.execSQL(CREATE_Village_Table);`
Upvotes: 0
Reputation: 180080
onCreate
is used for newly installed apps (or after the user has deleted your app's data).
onUpgrade
is used when your app is updated, i.e., when the data of some older version already exists on the device.
The onCreate
method of your 1.2 app must create all the tables needed for that version.
Upvotes: 0
Reputation: 758
In onUpgrade method you need to drop the existing table taking the backup of the data. Then in onCreate you need to create both the table and insert the old data if any....
Upvotes: 2