0

I'm having issues after releasing a new version of my android app in android studio.

Here is the problem: I have a quiz app with an SQlite database in which questions and answers have been populated with SQLite database browser. Now when I add new questions to the database tables and create a new database version, database dont seem to reflect in the updated app. The tutorials I find online on database upgrades enphasizes only on database that is created as application starts. like this

Question: How do I handle database upgrade only when I add new records to an already existing table in SQLite?

What I have tried: (1) in DatabaseOpenHelper OnUpgrade() method, I dropped the tables in which new records were added.

result : App crashed activity starts

(2) Leave the OnUpdrade() method empty.

result : The newly added records did not reflect

Extra data: DatabaseOpenHelper OnCreate() method is also empty

Code

@Override
public void onCreate(SQLiteDatabase db) {

}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

    db.execSQL("drop table if exists "+ DatabaseDescription.LectureNotes.TABLE_NAME);
    db.execSQL("drop table if exists "+ DatabaseDescription.QuestionAnswer.TABLE_NAME);
    db.execSQL("drop table if exists "+ DatabaseDescription.Test.TABLE_NAME);
    db.execSQL("drop table if exists "+ DatabaseDescription.Subjects.TABLE_NAME);
    db.execSQL("drop table if exists "+ DatabaseDescription.Topics.TABLE_NAME);
    db.execSQL("drop table if exists "+ DatabaseDescription.Year.TABLE_NAME);
}
Urchboy
  • 725
  • 2
  • 12
  • 26

1 Answers1

1

The pre-populated database will only be copied from the assets folder once.

To update with a new version of the pre-populated database you would need to open a copy of the newer updated database and then copy the relevant rows or tables into the existing database if the existing database has data that needs to be kept (e.g. answered questions).

here's an example that does the above Update DB. Sqlite-asset-helper library

MikeT
  • 51,415
  • 16
  • 49
  • 68