6

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.

2
  • 1
    look up sqlite migrations. You can update the table structure while keeping data. Can be difficult if you have to move around data. Commented Jun 1, 2018 at 9:43
  • 2
    If my answer was useful to use, I'll greatly appreciate if you can accept an answer. Thanks. Commented Jun 1, 2018 at 13:54

2 Answers 2

9

(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.

Sign up to request clarification or add additional context in comments.

10 Comments

thanks,but i have another table i have to write anything in onupgrade method for that table
As 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.
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.
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)
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
|
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);        
}     

2 Comments

Your 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.
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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.