23

I have developed an android app and released version 1.0. The app contains SQLite local database with 5 tables.

Now we planned to release version 2.0 also update the version 1.0 users. In version 2.0 we have included extra two tables with previous 5 table, so 7 tables now.

Now my question is, The version 1.0 users all have some data in the local database, If he update to the version 2.0 the previous data will get lost? If it so. then what is the alternate method?

3
  • if you change the DB architecture, the old DB cannot stay Commented Jun 3, 2013 at 6:05
  • @thepoosh Thank you for your reply. If I didn't change the DB architecture, but updated the version with new feature. In that case? Commented Jun 3, 2013 at 6:07
  • 2
    try reading this answer: stackoverflow.com/a/8627242/1056359 Commented Jun 3, 2013 at 6:13

3 Answers 3

26

You should put all changes in your onUpgrade method you can use this code:

@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);        
}        

this adds a column in your current database. 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.

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

1 Comment

Just to clarify, TABLE_SECRET is the table name.
2

Change your db version and add extra two table creation methods in your onUpgrade(SQLiteDatabase db, int oldVersion, int newVesion) method. Now the the previous data will not get lost.

I hope this will help you.

Comments

1

You can do some on SQLiteOpenHelper#onUpgrade, get some old data. and insert into new table

Comments

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.