zoukankan      html  css  js  c++  java
  • SQLiteOpenHelper 升级onUpgrade 的调用问题

    onUpgrade 的调用次数问题

    比如说现在数据库版本是1,然后此时我修改代码定数据库版本为5。 那么系统在调用onUpgrade的时候是只调用一次(oldVersion == 1, newVersion == 5)还是从1到5调用5次?

    只会调用一次。不管是从1到2,还是1到5

    onUpgrade 调用还是 onCreate调用还是一起调用的问题

    一、软件v1.0  (只有一个account表)

    安装v1.0,假设v1.0版本,这时走继承SQLiteOpenHelper的onCreate,不走onUpgrade。

    1、v1.0(直接安装v1.0)

    二、软件v2.0   (需要新增一个member表)

    有2种安装软件情况:

    1、v1.0   -->  v2.0            

     不走onCreate,走onUpgrade

    这时不会走继承SQLiteOpenHelper的onCreate,而是直接走onUpgrade,这时就要在onUpgrade添加member表的代码了,在onCreate加了也没用,因为这种情况都不走onCreate。

    2、v2.0(直接安装v2.0)

    走onCreate,不走onUpgrade

    走继承SQLiteOpenHelper的onCreate,不走onUpgrade,所以要在onCreate添加member表的代码

    三、软件v3.0   (需要新增一个news表)

    假设v3.0又新增一个news表,这里有三种情况:

    1、v1.0   -->  v3.0              不走onCreate,走onUpgrade

    2、v2.0   -->  v3.0              不走onCreate,走onUpgrade

    3、v3.0(直接安装v3.0)          走onCreate,不走onUpgrade

    那数据库添加表语句在那里写呢?数据库有一个版本号用DATABASE_VERSION表示

    其实想一下,就知道不是onCreate写就是onUpgrade写,就是要兼容各种情况下安装app,都能把数据库表添加进去就好了。这里很巧妙:

    1、v1.0     DATABASE_VERSION=1000    onCreate      --添加--  account

    2、v2.0     DATABASE_VERSION=1001    onCreate      --添加--  account  (v1.0代码不变)  onUpgrade(DATABASE_VERSION>1000)

                                                           onUpgrade   --添加--  member 

    3、v3.0     DATABASE_VERSION=1002    onCreate      --添加--  account  (v1.0代码不变)  onUpgrade(DATABASE_VERSION>1001)

                                                           onUpgrade   --添加--  member (v2.0代码不变)

                                                           onUpgrade   --添加--  news

    这样就可以解决问题了,第一版本的都在onCreate,其他版本新增的在onUpgrade,而且在onCreate执行onUpgrade。做判断是否执行onUpgrade该怎么判断呢,所以有了数据库版本的概念了,DATABASE_VERSION保存当前的数据库版本,只要当前的数据库版本比已经安装的数据库版本大时,就进入onUpgrade,这时还会把上一个数据库版本号(oldVersion)跟安装的数据库版本号(newVersion)做比较,不同的DATABASE_VERSION添加自己所需要的表(跨版本升级数据库)。

    @Override
        public void onCreate(SQLiteDatabase db) {
            createTable(db, Account.class);
            createTable(db, Member.class);
            createTable(db, News.class);
        }
         @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
           switch (oldVersion) {
            case 1:
        createTable(db, Member.class);  
            case 2:
            createTable(db, News.class);
            }
            return;
        }
  • 相关阅读:
    java的(PO,VO,TO,BO,DAO,POJO)解释
    java中的几种对象(PO,VO,DAO,BO,POJO)
    mybatis如何根据mapper接口生成其实现类
    MyBatis接口的简单实现原理
    Fast-settling synchronous-PWM-DAC filter has almost no ripple
    CMOS DACs act as digitally controlled voltage dividers
    Programmable current source requires no power supply
    Add margining capability to a dc/dc converter
    Use an LM317 as 0 to 3V adjustable regulator
    Simple microcontroller-temperature measurement uses only a diode and a capacitor
  • 原文地址:https://www.cnblogs.com/mingfeng002/p/6439235.html
Copyright © 2011-2022 走看看