8

What is the best practice for updating the DB schema? I could send a text file with the SQL commands. The app could check the text file and execute the commands needed. I am guessing then I will have a flag to indicate that the update has been done. I havent found a way to delete a file in the asset folder from the app, which would be the best thing to do after the DB was updated.

Any thoughts on this?

4 Answers 4

13

You will probably want to override

onUpdate(SQLiteDatabase db,int old Version,int newVerison)

The following tutorial can walk you through the process: http://www.codeproject.com/KB/android/AndroidSQLite.aspx

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

3 Comments

this looks good but what happens if a user doesnt update and skips a few versions. So supposing they have version 1. All is good. I use the onUpdate() on the version 2. All is good here and I can put in the SQL commands in the code. Then version 3 comes along, I wipe out the last updates and put in new ones, I assume. This is all good except what happens to the person that never got version 2?
@Mark Worsnop: Your onUpdate() has to handle all scenarios. So, if you have schema revisions 1, 2, and 3, and you need to go from 1 to 3, one solution is to apply the 1->2 change, then the 2->3 change.
As @CommonsWare sir mentioned, in onUpdate() method we should consider all conditions i.e. 1 -> 2 , 1 -> 3 , 2 -> 3 because we should not assume that all users have latest version
4

What is the best practice for updating the DB schema?

Use SQLiteOpenHelper. The only time you will be updating your schema is when you update the application. Whether you have the SQL commands in a file that you read in or just in your Java code is up to you.

I am guessing then I will have a flag to indicate that the update has been done.

That is part of what SQLiteOpenHelper gives you.

I havent found a way to delete a file in the asset folder from the app, which would be the best thing to do after the DB was updated.

That is not possible, sorry.

Comments

1

As mentioned from others the SQLiteOpenHelper is the thing you are starting with.

Depending on your environment and requirements, Liquibase may be worth a look. It allows writing your update statements in a more structural way and is easier to use than plain SQL operations. I haven't used it for android, though.

Comments

0

If you want to update the db schema use the following code in your SQLiteOpenHelper extended class. This will update the table structure and never lose your saved data from db,

 public void onUpgrade(SQLiteDatabase db, int oldVersion, int 
    newVersion) {
      if (oldVersion < newVersion) 
      {
        db.execSQL("ALTER TABLE " + "TABLE_NAME" + " TO " + 
        "NEW_TABLE_NAME");

        db.execSQL("ALTER TABLE " + "TABLE_NAME" + " ADD COLUMN " + 
        "FIELD_NAME" + " INTEGER;");
      }   
    }

SQLite doesn't support removing or modifying columns.

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.