简体   繁体   中英

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).

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM