tags:

views:

42

answers:

3

I have released an app (World Time) with initial database. Now i want to update the app with a database upgrade.

I have put in the upgrade code in OnUpgrade() and checking for the newVersion. But it was not being called in my local testing...

So i put in the debug statement to get the database version and it is zero .

Any idea why it is not being versioned ?

Following is the code to copy the database from my Assets folder ...

    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();

Following is my constructor, OnCreate() & OnUpgrade()

public DatabaseHelper(Context context) {

    super(context, DB_NAME, null, DB_VERSION);
    this.myContext = context;
}
@Override
public void onCreate(SQLiteDatabase db) {

}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    if(oldVersion == 1 && newVersion == 2){
        String sql = "update statement...."; // i have a correct update statement here
        db.execSQL(sql);
    }

}

--
Mahesh
http://android.maheshdixit.com

A: 

You should have a class that is your database adapter. It will have lines similar to the following:

private static final String  DATABASE_NAME = "sr4.db";
private static final int    DATABASE_VERSION = 1;

All you need to do to get the app to recognize that the database needs updating is to change the number DATABASE_VERSION is set to.

David
Thanks for your reply... but I already have the following private static final String DB_NAME = "WorldClock"; private static final int DB_VERSION = 2; public DatabaseHelper(Context context) { super(context, DB_NAME, null, DB_VERSION); this.myContext = context; } I also tried explicitly setting the version to 1, but the next time is upgraded it does not call the OnUpgrade()
mahesh
+1  A: 

You should NOT have to manually check for the DB Version, SQLite does this for you if done correctly.

In your SQLiteOpenHelper inherited class, in the constructor, you should pass the DB_VERSION, something like this:

public class DBHelper extends SQLiteOpenHelper {
    public static final String DB_NAME = "your_db_name";
    public static final int DB_VERSION = 2;

    public DBHelper(Context context) {
        super(context, DB_NAME, null, DB_VERSION);
                ...
        }
  ...
}

Then in your onCreate() execute the create table and onUpgrade() do whatever you need to do to upgrade without checking the version, this method will only get called when SQLite has already checked this for you. If you need to run the code in onUpgrade() just increase your DB_VERSION variable.

Ricardo Villamil
A: 

I found the problem... It seems that the OnCreate() and OnUpgrade are called only when you call getReadableDatabases or getWriteableDatabases(). And i was not calling either of them if the DB exists. I was instead call SQLLiteDatabase.openDatabase(), which does not call the OnCreate or OnUpgrade().

Now that i call explicitly getWriteableDatabases(), it calls the OnUpgrade().

Thanks for all you responses though...

mahesh