0

I'm having a bit of trouble understand what to do if I want to upgrade a database with an app update, though my case is a bit unique. The starting database (in the assets folder) has a few tables that have records in them already, so it just directly copies the entire database to the data/data/package/databases/ folder. When I want to do an update I want some some tables to keep their data, but I want other tables to be dropped and added from the updated database.

I'm assuming I put my code in OnUpgrade, but I'm not sure what to put...I can back up the data in the table, but I'm not sure how copy it back.

Here's some snippets of my code.

public class DataBaseHelper extends SQLiteOpenHelper{

//The Android's default system path of your application database.
private static String DB_PATH = "/data/data/package/databases/";

private static String DB_NAME = "drink.db";
private static final int DATABASE_VERSION = 2;
SQLiteDatabase myDataBase;
private final Context myContext;

public DataBaseHelper(Context context) {

    super(context, DB_NAME, null, DATABASE_VERSION);
    this.myContext = context;
}   

public void createDataBase() throws IOException{

    boolean dbExist = checkDataBase();
    if(dbExist){
            //do nothing database already exists
    }else{
            try {
            copyDataBase();

        } catch (IOException e) {

            throw new RuntimeException(e.getMessage());

        }
    }

}

private boolean checkDataBase(){
    SQLiteDatabase checkDB = null;

    try{
        String myPath = DB_PATH + DB_NAME;
        checkDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);

    }catch(SQLiteException e){
        //database does't exist yet.
    }

    if(checkDB != null){

        checkDB.close();

    }

    return checkDB != null ? true : false;
}

private void copyDataBase() throws IOException{

    //Open your local db as the input stream
    InputStream myInput = myContext.getAssets().open(DB_NAME);

    // Path to the just created empty db
    String outFileName = DB_PATH + DB_NAME;

    //Open the empty db as the output stream
    OutputStream myOutput = new FileOutputStream(outFileName);

    //transfer bytes from the inputfile to the outputfile
    byte[] buffer = new byte[1024];
    int length;
    while ((length = myInput.read(buffer))>0){
        myOutput.write(buffer, 0, length);
    }

    //Close the streams
    myOutput.flush();
    myOutput.close();
    myInput.close();
}

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

    if (newVersion > oldVersion) {
            }
}
}

Thanks again!

1 Answer 1

1

You are right with regards to putting your upgrade code in onUpgrade. I can see you are using the widely used solution to ship a pre-constructed database with your application, with this, you need to consider some scenarios that would otherwise be unimportant. For example, what if your user has your app installed, never used it though, and suddenly decides to update it to a new version? Then your code would try to "update" a non-existing database. So, just a heads up in that direction :)

When writing code in onUpgrade, you are doing operations on the provided SQLiteDatabase object, which is your old database. To fetch the new database, from the assets folder, take a look at this in your checkDataBase() method:

checkDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);

To grab a SQLiteDatabase object with your new database(assets folder) you use this function, change myPath to the path of your database in the assets folder, it will return a SQLiteDatabase object, you might want to change the open method to writable, depending on your needs. Now you can perform operations back and forth between the two databases to fit your needs.

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

4 Comments

Thanks so much! I'll mess around with it for a bit. I'll let you know how it goes!
Glad to help, let me know if you run into any problems.
Hm...I'm not sure how to get the path for the database in the assets folders...any idea?
Ah, I got it now. I backed up the old database, backed up the old data, copied the new database over, then copied the new data over.

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.