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.
Asked
Active
Viewed 1,516 times
6
-
1look up sqlite migrations. You can update the table structure while keeping data. Can be difficult if you have to move around data. – Zun Jun 01 '18 at 09:43
-
2If my answer was useful to use, I'll greatly appreciate if you can accept an answer. Thanks. – Harsh4789 Jun 01 '18 at 13:54
2 Answers
8
(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.

Harsh4789
- 677
- 7
- 17
-
thanks,but i have another table i have to write anything in onupgrade method for that table – sai android Jun 01 '18 at 14:17
-
1As you haven't mentioned about the table and all, this is a generic answer which exactly matches to your question. You simply need to replace your query based on your requirements in the onUpgrade() method. I have also mentioned a blog that does the same. – Harsh4789 Jun 01 '18 at 14:22
-
Didn't get your question. You want to add column to the existing table right? For this, you don't need to write the onCreate again in the onUpgrade. Simply use the query in the onUPgrade to update your table - ALTER TABLE your_table_name ADD COLUMN your_new_column_name TEXT - I have simply considered the new column type TEXT. You should change it according to your requirements. – Harsh4789 Jun 02 '18 at 07:47
-
You are receiving the SQLiteDatabase instance (db), So simply write this line - db.execSQL("ALTER TABLE your_table_name ADD COLUMN your_new_column_name TEXT) – Harsh4789 Jun 02 '18 at 08:18
-
thanks for your responce bro my qst is i have four tables in my database i want to add three coloums to only one table – sai android Jun 02 '18 at 13:27
-
thanks for your responce bro my qst is i have four tables in my database i want to add three coloums to only one table for that the above code is okay but for remaining tables i have to write any code in on upgrade like ( db.execSQL("DROP TABLE IF EXISTS " + TABLE_ProductSales);) – sai android Jun 02 '18 at 13:34
-
With SQlite, you cannot add three columns with a single query. Please visit this for more information - https://stackoverflow.com/a/6172889/6306365. Simply Call the db.execSQL three time in the onUpgrade() – Harsh4789 Jun 02 '18 at 13:35
-
-
Hope, you are satisfied with my previous comments & managed to fix your problem. If not, please tell me what problem are you facing? Otherwise please accept an answer. Thanks. – Harsh4789 Jun 05 '18 at 08:19
-
3
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);
}

Mohsin mithawala
- 66
- 5
-
1Your database will not lose data. Reminder: onUpgrade will be called when getWriteableDatabase or getReadableDatabase is executed AND the version of your database is different from your older version. – Mohsin mithawala Jun 01 '18 at 09:47
-
do one thing uninstall application and re-install.from then open Device file explorer option menu in android studio push db file and match column name – Mohsin mithawala Jun 01 '18 at 10:26