seekingStillness
seekingStillness

Reputation: 5093

Android SQLite DB version 2 error

I realize there are many answers here for SQLite versioning, but I couldn't find an answer for my specific issue. I was doing fine with Version 1 for quite a while. I then needed to add another column "TAG" to my table and implemented the following code in onUpgrade.

@Override
public void onCreate(SQLiteDatabase db) {
    db.execSQL(DATABASE_CREATE);
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    if (oldVersion < 2) db.execSQL(DATABASE_ALTER_TAG);
}

It worked great. No issues. The TAG column was working as expected reading and writing -- until I reinstalled after a full uninstall. The DB now won't work using code that worked before the uninstall / reinstall.

I suspect it is because after uninstall / reinstall the "old version" is null (which is not < 2) and the TAG column is not being added. I'm not entirely sure although. Is my onUpgrade code the problem?

I suppose I could rewrite my my database as Version 1 with the TAG column, but I'd like to know what is wrong with my code for future reference.

EDIT QUESTION

OK, thanks for the responses so far. I understand how to get the DB to work for someone who is installing the app fresh. I also understand how to get the DB to work for someone who is going from Version 1 to Version 2. I don't yet understand how to get it to work for both users (fresh install vs. someone who is just upgrading).

Upvotes: 0

Views: 113

Answers (1)

Ramandeep Nanda
Ramandeep Nanda

Reputation: 519

Your assumption is definitely not correct. Primitive data types are not object and hence are not null.

Just make sure your create statement i.e. db.execSQL(DATABASE_CREATE); reflects the new database table structure and not the old one i.e. with the new TAG column.

Upvotes: 1

Related Questions