简体   繁体   English

在我的应用程序中升级SQLite数据库架构

[英]Upgrading SQLite database schema in my app

If I have 3 different database schemas, is it possible for a user to upgrade to version 2? 如果我有3种不同的数据库架构,用户是否可以升级到版本2? In other words, do I need to check for every possible combination of oldVersion and newVersion in the onUpgrade() method of my SQLiteOpenHelper subclass? 换句话说,我是否需要在SQLiteOpenHelper子类的onUpgrade()方法中检查oldVersionnewVersion每种可能组合?

Clarification: 澄清:

After I release an update with a 3rd database schema is it possible that onUpgrade() will be called with a newVersion of 2 (where the original database schema is 1 and the newest one is 3)? 在发布具有第三个数据库架构的更新后,是否可能会使用newVersion 2(其中原始数据库架构为1而最新的数据库架构为3)调用onUpgrade() )?

It is possible for them to skip a version, but this doesn't mean you have to check for every possible combination. 他们可能会跳过版本,但这并不意味着您必须检查所有可能的组合。 What I usually do is put the upgrades in their unique methods and call those methods from a switch : 我通常要做的是将升级放入其独特的方法中,并从switch调用这些方法:

private void updateTo1(){
   //Code to update schema to version 1.
}

private void updateTo2(){
   //Code to update schema to version 2.
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion){
   switch(oldVersion){
      case 0:
         updateTo1();
      case 1:
         updateTo2();
   }
}

This will ensure that even if they upgrade from version 0 straight to version 2, the schema will first go through version 1 making sure that it's compatible with and ready for the version 2 update. 这将确保即使他们从版本0直接升级到版本2,该架构也将首先通过版本1,以确保它与版本2兼容并准备进行版本2更新。

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

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