Reputation: 602
I found many tutorials on how to deploy a SQLite Database together with an android app. I also found out, that I cannot delete files from the assets folder during runtime, because the .apk is signed.
Now I want to offer an updated database for my app every week. My plan was to update the apk, and publish it at the marketplace. I obviously don't want to copy the database with every program start from the asset folder into the application's database folder. I tried comparing the filesize to find out if the new database is bigger than the old one. But it seems if you don't add or change much data, the size stays the same.
So my question is: What is a smart way to copy the database only after the first install or after an application update?
(I'm pretty new to android development so forgive me if I've overseen the maybe terribly obvious solution. Usually I try to find my answers before I ask questions. But here I just couldn't find a solution.)
Upvotes: 1
Views: 1377
Reputation: 27411
Answer is, you don't have to. Suppose you use the SQLiteOpenHelper to implement your database, you have two overrides:
onCreate(SQLiteDatabase db);
onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion);
These pretty much enough to handle all the issue. When you create your openhelper, you need to supply the 'required' database version to it. It will auto detect the Device, whether it previously has your app's db or not. So, there will be three possible situation:
In case of you update your app, but the structure of db need not to be changed, (2) will happens and the db and data will remains.
Upvotes: 3
Reputation: 10254
Take a look at the NotePadProvider.java example from the Android Developers website. Basically anytime you update the database version the onUpgrade method is called in your SQLOpenHelper class. Insert the logic you are looking for into that method.
Upvotes: 0