defiant
defiant

Reputation: 3341

Replace the database file in an app in the next update

My app has a database file in the assests directory and the app imports it when the app is run for the first time or the database doesn't exist. I would like to replace the database with a database which I have, when the user updates the app for the next time. It should only run when the user updates it, so that no data is lost.

Can somebody please help me?

This is my code: http://pastie.org/private/bni5iwbob1oklyxx5atolq#66

Upvotes: 0

Views: 79

Answers (1)

Its not blank
Its not blank

Reputation: 3095

You have to update your Db version that you have used in constructor

public DbImportHelper(Context context) {
    super(context, DB_NAME, null, 1);
    this.myContext = context;
    // TODO Auto-generated constructor stub
}

This way onUpgrade will be called. When you initialize your DB the System checks the current version of DB present on Device. If the version is not matched then your onUpgrade or onDownGrade method is called.

so you can change constructor to something like

public DbImportHelper(Context context) {
    super(context, DB_NAME, null, 2);
    this.myContext = context;
    // TODO Auto-generated constructor stub
}

One Suggestion avoid using magic numbers like 1,2,55 etc you can assign a constant to them.this way you wont have to search your code all the time.

Upvotes: 1

Related Questions