简体   繁体   English

SQLite 数据库升级

[英]SQLite Database Upgrading

I am a bit confused with the android SQLite database handling.我对 android SQLite 数据库处理有点困惑。 I went through tutorials but I didn't get the exact point.我浏览了教程,但我没有得到确切的意思。

We can have a database class that extends SQLiteOpenHelper , and can override onCreate() method and create a database.我们可以有一个扩展SQLiteOpenHelper的数据库 class ,并且可以覆盖onCreate()方法并创建一个数据库。

Upgrading database part is a bit confusing.升级数据库部分有点混乱。 In the following method, how to handle the verions onUpdate(SQLiteDatabase db,int old Version,int newVerison)在以下方法中,如何处理版本onUpdate(SQLiteDatabase db,int old Version,int newVerison)

Does it mean that when the first time we create database the version is 1. Then, once modified the version become 2. Then if we want to modify again old Version = 2, newVerison = 3是不是说我们第一次创建数据库的时候版本是1。那么修改一次版本就变成2了。那么如果我们要再次修改old Version = 2, newVerison = 3
[ onUpdate(SQLiteDatabase db,int old Version,int newVerison) ] [ onUpdate(SQLiteDatabase db,int old Version,int newVerison) ]

This method will be execute when we pass the constructor version as in the following code (as 2)当我们传递构造函数版本时,将执行此方法,如下代码所示(如 2)

public DatabaseHelper(Context context) {
  super(context, dbName, null,2);
}

I need to know whether when we need to call onUpgrade() method should we pass version as 2 always or we have to increase one every time for the previous version.我需要知道当我们需要调用onUpgrade()方法时,我们应该始终将版本传递为 2,还是每次都必须为以前的版本增加一个。

Increment your database version every time the schema changes.每次架构更改时增加您的数据库版本。

You never need to call onUpgrade directly.你永远不需要直接调用onUpgrade Android will call it when required when you open the database by comparing the version of the database with the version that your code is specifying as the current version. Android 将在您打开数据库时通过将数据库版本与您的代码指定为当前版本的版本进行比较来在需要时调用它。

You just need to handle the upgrade process in onUpgrade - it might go something like this:您只需要在onUpgrade中处理升级过程 - 它可能 go 是这样的:

int curVer = oldVersion;
while ( curVer < newVersion ) {
    curVer++;
    switch ( curVer ) {
        case 2: {
            // Upgrade from V1 to V2
            break;
        }
        case 3: {
            // Upgrade from V2 to V3
            break;
        }
        case 4: {
            // Upgrade from V3 to V4
            break;
        }
    }
}

Lets say your newVersion is 4 and oldVersion is 1 - first iteration will increment curVer to 2 and will run the V2 to V3 upgrade code.假设您的 newVersion 为 4 而 oldVersion 为 1 - 第一次迭代会将 curVer 增加到 2 并将运行 V2 到 V3 升级代码。 Second iteration increments curVer to 3 and runs the V2 to V3 upgrade code, final iteration increments curVer to 4 and runs the V3 to V4 upgrade code.第二次迭代将 curVer 增加到 3 并运行 V2 到 V3 升级代码,最后一次迭代将 curVer 增加到 4 并运行 V3 到 V4 升级代码。

This works for all values of oldVersion that are less than newVersion.这适用于所有小于 newVersion 的 oldVersion 值。 and will sequentially upgrade through the intermediate versions if your users are skipping upgrades of the app.如果您的用户跳过应用程序的升级,它将通过中间版本顺序升级。

I use this approach to make database schema evolution a bit easier, you might find it useful too.我使用这种方法使数据库模式演变更容易一些,您可能会发现它也很有用。

its totally up to you that how you are going to maintain the version of database.如何维护数据库版本完全取决于您。 you can change your version after alteration in table structure or after any modification in content.您可以在更改表结构或修改内容后更改您的版本。 there is no hard and fast rule for maintaining versioning.维护版本控制没有硬性规定。 its depends on the requirement of the project.这取决于项目的要求。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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