使用SQLiteOpenHelper的onUpgrade实现数据库版本升级

   

我这里说的数   

                     From http://www.cnblogs.com/vir56k/archive/2013/04/18/3028688.html

                        据库版本指的是:我们的应用的程序的数据库的用户版本(user_version).比如说下面的情形:

2013年4月,我们第一次 发布了 我们的应用,数据库版本是1。

2013年5月,我们第二次 发布了 我们的应用,数据库版本是2。由于业务需要,我们更改了数据库里的某个表的表结构。

这时候就有这样的难题出现:

有些用户已经下载了4月份的版本1,并且已经使用了,很多数据存储在数据库了,这个时候,他想安装新的版本2,怎么办? 怎么才能让数据不丢失?

有的用户直接装了5月份的版本,那这些用户就直接使用了新的表结构格式。

可能以后还有版本3,4,N,怎么保证“数据不丢失的情况下“让用户手机里的数据库跟着升级?

------

我们记得SQLiteOpenHelper的onUpgrade方法,那么它是如何工作呢?我们该如何使用他?下面先说说使用它的方式。

解决方案:

我们在4月份数据库建立时,使用下面的方式

public  class  MyDbHelper1 extends  SQLiteOpenHelper{
     public  final  static  int  <span style= "color: #ff0000;" >DB_VERSION = 1 </span>;
     public  final  static  String DB_NAME = "mydb.db" ;
     public  final  String TABLE_NAME = "tbl_data" ;
     public  final  String COL_UID = "uid" ;
     public  final  String COL_ITSVALUE = "itsvalue" ;
     public  final  String COL_CREATEDDATE = "createddate" ;
     
     public  MyDbHelper1(Context context) {
         super (context, DB_NAME, null , <span style= "color: #ff0000;" >DB_VERSION</span>);
     }
 
     @Override
     public  void  onCreate(SQLiteDatabase db) {
         String sql = "create table [" +TABLE_NAME+ "] ( [uid] int identity primary key,[itsvalue] nvarchar(200),createddate TIMESTAMP default (datetime('now', 'localtime')) )" ;
         db.execSQL(sql);
     }
 
     @Override
     public  void  onUpgrade(SQLiteDatabase db, int  oldVersion, int  newVersion) {
         // TODO Auto-generated method stub
         
     }
}

  我们看到,在这里,我们的onCreate方法里,写个建表的语句,这个表有3个字段。

 

我们注意看下数据库的版本

..............................

于是到了五月份,由于业务需要,我们想添加新的字段到这个表里。我们这样写代码:

public  class  MyDbHelper2 extends  SQLiteOpenHelper{
     public  final  static  int  <span style= "color: #ff0000;" >DB_VERSION = 2 </span>;
     public  final  static  String DB_NAME = "mydb.db" ;
     public  final  String TABLE_NAME = "tbl_data" ;
     public  final  String COL_UID = "uid" ;
     public  final  String COL_ITSVALUE = "itsvalue" ;
     public  final  String COL_CREATEDDATE = "createddate" ;
     <span style= "color: #ff0000;" > public  final  String COL_DESC = "desc" ;</span>
     
     
     public  MyDbHelper2(Context context) {
         super (context, DB_NAME, null , <span style= "color: #ff0000;" >DB_VERSION</span>);
     }
     
     @Override
     public  void  onCreate(SQLiteDatabase db) {
         //创建新的数据库时。已经 有新列desc了。
         String sql = "create table [" +TABLE_NAME+ "] ( [uid] int identity primary key,[itsvalue] nvarchar(200),createddate TIMESTAMP default (datetime('now', 'localtime')),[desc] nvarchar(300) )" ;
         db.execSQL(sql);
     }
 
     @Override
     public  void  onUpgrade(SQLiteDatabase db, int  oldVersion, int  newVersion) {
         <span style= "color: #ff0000;" > if (oldVersion == 1  && newVersion == 2 ){
             //从版本1到版本2时,增加了一个字段 desc
             String sql = "alter table [" +TABLE_NAME+ "] add [desc] nvarchar(300)" ;
             db.execSQL(sql);
         }</span>
         
     }

  这个时候,onCreate方法里,是新的建立表的方式。而不同的是 onUpgrade方法里我们检测了这样的变化,如果 是从版本1到版本2,我们执行了一段”添加列的sql语句“。这段代码,仅仅在符合这样的场景里执行。

通过上面的方式,我们就完成了一次的数据库升级的操作。android会判断 数据库的版本号,并自动的调用onUpgrade方法。

看看数据库的版本

------------------

下面是一些扩展内容?

我们通过SQLite Expert看到的这个user_version是什么东西?经过在网络上反复查到资料,这个其实是sqlite数据库的"PRAGMA ".

看看描述:

<span style= "font-size: 15px;" ><strong>PRAGMA schema_version;
PRAGMA schema_version = integer ;
PRAGMA user_version;
PRAGMA user_version = integer ;</strong></span>
The pragmas schema_version and user_version are used to set or get the value of the schema-version and user-version, respectively. The schema-version and the user-version are big-endian 32 -bit signed integers stored in the database header at offsets 40  and 60 , respectively.
The schema-version is usually only manipulated internally by SQLite. It is incremented by SQLite whenever the database schema is modified (by creating or dropping a table or index). The schema version is used by SQLite each time a query is executed to ensure that the internal cache of the schema used when compiling the SQL query matches the schema of the database against which the compiled query is actually executed. Subverting this  mechanism by using "PRAGMA schema_version"  to modify the schema-version is potentially dangerous and may lead to program crashes or database corruption. Use with caution!
The user-version is not used internally by SQLite. It may be used by applications for  any purpose.

在数据库中,我们可以使用这样写 sql语句来查询它:

    PRAGMA main.user_version 

或者来设置它的值

   PRAGMA main.user_version = 1

更多内容参考sqlite的官方描述。

 

参考:http://www.sqlite.org/pragma.html

http://dev.10086.cn/cmdn/bbs/thread-25063-1-1.html

http://blog.sina.com.cn/s/blog_6ffbcfdb0100vjhs.html

http://stackoverflow.com/questions/12778551/how-to-check-sqlite-database-file-and-get-my-defined-database-user-version

 

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值