Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android SQLite: Replace old database with a new one or use migration scripts

I have an Android app that uses a SQLite database and Active Android as ORM. On each app update I need to ship my database with new/updated data. This is what i've been doing

  1. I have a my_app.db database
  2. I make modifications on the rows, tables, etc. of the my_app.db
  3. I save the modified my_app.db as my_app_v2.db ( and so on )
  4. I replace the my_app.db file of the assets folder with the my_app_v2.db and set it as the default database
  5. I compile and run the program using the newly created my_app_v2.db

So when the user gets the app, it will be using the my_app_v2.db with new contents.

I know that Active Android supports migration scripts, but on each database update I need to add/update about 2000+ records. So for each database update I would need a migration script with 2000+ insert/update statements, it means that for 3+ consecutive upgrades the app would have to execute about 6000+ statements.

I want to know if my approach of replace the whole database with a new one is a bad practice and the migrations scripts should be prefered.

like image 296
regmoraes Avatar asked Oct 10 '16 20:10

regmoraes


1 Answers

You dont need to do that (renaming stuff or anything)

You just need to change your database version and write a sql command to alter your previous table to migrate from version A to B.

Look at this link:

Android: upgrading DB version and adding new table

like image 72
Amir Ziarati Avatar answered Oct 24 '22 16:10

Amir Ziarati