1

I've used this tutorial in order to create and populate my own SQLite database for android. http://www.reigndesign.com/blog/using-your-own-sqlite-database-in-android-applications

However, using these exact methods mean that the database doesnt upgrade with the following code

private static final int DB_Version = 2;

    public DbConnector(Context context){
        super(context, DB_Name, null, DB_Version);
        this.context = context;
    }

In fact the method onUpgrade() is never called :(

Grateful for any help

Stevanicus
  • 7,561
  • 9
  • 49
  • 70

1 Answers1

4

Here is the answer thanks to Joe Masilotti on http://www.reigndesign.com/blog/using-your-own-sqlite-database-in-android-applications/comment-page-2/

private static final int DATABASE_VERSION = 1;

Change the constructor to:

public DataBaseHelper(Context context) {
    super(context, DB_NAME, null, DATABASE_VERSION);
    this.myContext = context;
}

Change the createDataBase to (thanks @kondortek):

public void createDataBase() throws IOException {
    boolean dbExist = checkDataBase();
    if (dbExist) {
        Log.v(“DB Exists”, “db exists”);
        // By calling this method here onUpgrade will be called on a
        // writeable database, but only if the version number has been
        // bumped
        this.getWritableDatabase();
    }
    dbExist = checkDataBase();
    if (!dbExist) {
        // By calling this method and empty database will be created into
        // the default system path of your application so we are gonna be
        // able to overwrite that database with our database.
        this.getReadableDatabase();
        try {
            copyDataBase();
        } catch (IOException e) {
            throw new Error(“Error copying database”);
        }
    }
}

Change onUpgrade to:

public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    if (newVersion > oldVersion)
        Log.v(“Database Upgrade”, “Database version higher than old.”);
    myContext.deleteDatabase(DB_NAME);
}
mkjeldsen
  • 2,053
  • 3
  • 23
  • 28
Stevanicus
  • 7,561
  • 9
  • 49
  • 70
  • 1
    why dbExist is called twice ? By calling boolean dbExist = checkDataBase(); we already know exist database or not. why we dont youse else instead of call same method again -> dbExist = checkDataBase(); ? –  Oct 25 '12 at 12:00
  • call copyDataBase(); in onUpgrade(...) method (after deleting the old db) so that you need not check dbExist = checkDataBase(); second time. – selva Feb 13 '13 at 05:36