SQLite onUpgrade具有4个数据库版本 [英] SQLite onUpgrade with 4 database versions

查看:60
本文介绍了SQLite onUpgrade具有4个数据库版本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我们有4个数据库版本并且每个版本的用户表中都添加了一个新字段时,如何正确执行SQLiteOpenHelper的onUpgrade方法?

How is the proper way to execute an SQLiteOpenHelper's onUpgrade method, when we have 4 database versions and we have added a new field to the user table in each version?

变体A://在每种情况下都没有中断",它是否在情况2和3中继续运行?

Variant A: // No "break" after each case, does it keep running for case 2 and 3?

public void onUpgrade(SQLiteDatabase database, int version_old, int current_version) {
    switch (version_old) {
    case 1:
        database.execSQL(addPostcodeFieldToUserTable);
    case 2:
        database.execSQL(addGenderFieldToUserTable);
    case 3:
        database.execSQL(addEmailSubscriptionFieldToUserTable);
        break;
    }
}

变种B:

public void onUpgrade(SQLiteDatabase database, int version_old, int current_version) {
    switch (version_old) {
    case 1:
        database.execSQL(addPostcodeFieldToUserTable);
        break;
    case 2:
        database.execSQL(addPostcodeFieldToUserTable);
        database.execSQL(addGenderFieldToUserTable);
        break;
    case 3:
        database.execSQL(addPostcodeFieldToUserTable);
        database.execSQL(addGenderFieldToUserTable);
        database.execSQL(addEmailSubscriptionFieldToUserTable);
        break;
    }

但是,如果用户拥有数据库的版本1,然后错过了版本2,并使用版本3升级了应用程序,我们该怎么办?

But what do we do in the case when a user had version 1 of the DB, then missed version 2 and upgraded the app with version 3?

变体3:

public void onUpgrade(SQLiteDatabase database, int version_old, int current_version) {
    if(version_old==1 && current_version==2) {
        database.execSQL(addPostcodeFieldToUserTable);

    } else if(version_old==2 && current_version==3) {
        database.execSQL(addGenderFieldToUserTable);

    } else if(version_old==3 && current_version==4) {
        database.execSQL(addEmailSubscriptionFieldToUserTable);

    } else if(version_old==1 && current_version==3) {
        database.execSQL(addPostcodeFieldToUserTable);
        database.execSQL(addGenderFieldToUserTable);

    } else if(version_old==1 && current_version==4) {
        database.execSQL(addPostcodeFieldToUserTable);
        database.execSQL(addGenderFieldToUserTable);
        database.execSQL(addEmailSubscriptionFieldToUserTable);

    } else if(version_old==2 && current_version==4) {
        database.execSQL(addGenderFieldToUserTable);
        database.execSQL(addEmailSubscriptionFieldToUserTable);

    }
}

推荐答案

在每种情况下都没有中断",它是否在情况2和3中继续运行?

No "break" after each case, does it keep running for case 2 and 3?

是的.一种常见的做法是添加类似

Yes. A common practice is to add a comment like

// fallthrough

表示丢失的 break 是有意的.

但是,如果用户拥有数据库的版本1,然后错过了版本2,并使用版本3升级了应用程序,我们该怎么办?

But what do we do in the case when a user had version 1 of the DB, then missed version 2 and upgraded the app with version 3?

onUprade()将使用 oldVersion 1和 newVersion 3进行调用.该代码应将数据库更新为架构的版本3.

onUprade() would be called with oldVersion 1 and newVersion 3. The code should update the database to version 3 of your schema.

要使用哪种变体,取决于哪个最适合您维护.我个人会选择类似变体A的代码,因为它的代码最少.

Which variant to use depends on which is the most comfortable for you to maintain. I'd personally go with something like variant A since it has the least code.

这篇关于SQLite onUpgrade具有4个数据库版本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆