onCreate() method calling instead of onUpgrade() method while updating database

1k Views Asked by At

I have an app with a database version = 1. Now I want to upgrade my database to version = 2.

However my problem is when I am running updated application on previous version with changed database version it is calling onCreate() method instead of onUpgrade().

I know onCreate() is called when database is created for first time and onUpgrade() is called when we change the database version.

I went through a lot of stack overflow questions but couldn't able to get a solution

Note : I have a preloaded database created using SqliteManager stored in asset folder. What I have done till now is as below

Database :

private static String DB_PATH = "/data/data/context.getPackageName()/databases/";
private static String DB_NAME = "db_name";
private static final int DATABASE_VERSION = 2;

// reference of database
private SQLiteDatabase mySqliteDb;
private Context context;

public DBController(Context context) {

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

/****************** onCreate() ***********************/
@Override
public void onCreate(SQLiteDatabase database) {
    Log.v("On create method", "On create method");   // This log is printing
}

/********************* Upgrade ************************/
@Override
public void onUpgrade(SQLiteDatabase database, int version_old,
        int current_version) {

    Log.v("On upgrade method", version_old +"");

}

/************* Create Database *********************/
public void createDataBase() throws IOException {
    boolean dbExist = checkDataBase();

    if (dbExist) {
        Log.v("DATABASE call ", "DATABASE exists");   // This log is printing
        this.getWritableDatabase();

    } else {
        Log.v("DATABASE call ", "DATABASE not exist");
        this.getWritableDatabase();

        try {

            copyDataBase();

        } catch (IOException e) {
            // Log.e("Error", e.toString());
            throw new Error("Error copying database");
        }
    }

}

/***************** Check whether database exist or not *******************/
private boolean checkDataBase() {

    File dbFile = new File(DB_PATH + DB_NAME);
    return dbFile.exists();
}

private void copyDataBase() throws IOException {
    // Open your local db as the input stream
    InputStream myInput = context.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[5120];
    int length;
    while ((length = myInput.read(buffer)) > 0) {
        myOutput.write(buffer, 0, length);
    }
    // Close the streams
    myOutput.flush();
    myOutput.close();
    myInput.close();
}

/************************************ database open *************************************/
public void openDataBase() throws SQLException {

    String myPath = DB_PATH + DB_NAME;
    mySqliteDb = SQLiteDatabase.openDatabase(myPath, null,
            SQLiteDatabase.OPEN_READWRITE);

}

And I am calling my database on my first activity

DBController dbController = new DBController(Login.this); 
try {
    dbController.createDataBase();
} catch (IOException e) {
    e.printStackTrace();
}

And after running my code

Log.v("On create method", "On create method");
Log.v("DATABASE call ", "DATABASE exists");

this two logs are printing. So I am unable to upgrade my database as it is not going to onUpgrade() block. Please help me if I am missing something here.

I found a similar type of question with my problem in stack overflow. Here is the link.

1

There are 1 best solutions below

3
On

It seems the database version is not correctly recorded unless you call getReadableDatabase or getWritableDatabase at some stage on creation of the db. According to user Vovkab, you should call either of these things in the constructor of your db to trigger the upgrade.

EDIT: I just wanted to add a couple things to this answer - most notably, this answer is the best description of upgrading databases I have seen. Next up, it would seem the key difference between this code and all the other similar SO problems out there is the fact that your database resides in the assets folder. I think this question needs a combination of onUpgrade outlined in the previous link, and a CopyNewDatabaseFromAsset() call outlined by sandeep_jagtap here.

@Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // TODO Auto-generated method stub
          CopyNewDatabaseFromAsset();
    }