I want to add new columns to a SQLite database, but I already released my app on the Play Store so, if I edit it, users need to uninstall and reinstall the app, but I don't want that. Please, help, I am new to Android.
-
1look up sqlite migrations. You can update the table structure while keeping data. Can be difficult if you have to move around data.Zun– Zun2018-06-01 09:43:39 +00:00Commented Jun 1, 2018 at 9:43
-
2If my answer was useful to use, I'll greatly appreciate if you can accept an answer. Thanks.Harsh4789– Harsh47892018-06-01 13:54:15 +00:00Commented Jun 1, 2018 at 13:54
2 Answers
(1) Increment (or simply change) your database version
(2) It would lead to onUpgrade() method call
(3) Execute your query(for adding a new column) in the onUpgrade() method.
The Right Way of doing is mentioned here in this blog.
Sometimes user may update the version 1.5 from the version 1.1.In other words, They may skip the other versions between the 1.1 to 1.5. You might changed database couple of time between 1.1 to 1.5. So in order to give the user a benefit of all the database changes, you need to take of onUpgrade() method as below.
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
if (oldVersion < 2) {
db.execSQL(DATABASE_ALTER_TEAM_1);
}
if (oldVersion < 3) {
db.execSQL(DATABASE_ALTER_TEAM_2);
}
}
Hope it helps.
10 Comments
You need to changes in following method of Database Class
@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);
}