• Android 数据库升级解决方案


    请考虑如下情况:

    在数据库升级时,不同版本的数据库,他们定义的表结构完全可能是不一样的,比如V1.0的表A有10个column,而在V1.1的表A有12个colum,在升级时,表A增加了两列,此时我们应该怎么做呢。

    总体思路

    1,将表A重命名,改了A_temp。

    2,创建新表A。

    3,将表A_temp的数据插入到表A。

    下面代码列出了更新表的实现,upgradeTables,给定表名,更新的列名,就可以实现数据库表的更新。

    [java] view plaincopy
     
    1. /** 
    2.  * Upgrade tables. In this method, the sequence is: 
    3.  * <b> 
    4.  * <p>[1] Rename the specified table as a temporary table. 
    5.  * <p>[2] Create a new table which name is the specified name. 
    6.  * <p>[3] Insert data into the new created table, data from the temporary table. 
    7.  * <p>[4] Drop the temporary table. 
    8.  * </b> 
    9.  * 
    10.  * @param db The database. 
    11.  * @param tableName The table name. 
    12.  * @param columns The columns range, format is "ColA, ColB, ColC, ... ColN"; 
    13.  */  
    14. protected void upgradeTables(SQLiteDatabase db, String tableName, String columns)  
    15. {  
    16.     try  
    17.     {  
    18.         db.beginTransaction();  
    19.   
    20.         // 1, Rename table.  
    21.         String tempTableName = tableName + "_temp";  
    22.         String sql = "ALTER TABLE " + tableName +" RENAME TO " + tempTableName;  
    23.         execSQL(db, sql, null);  
    24.   
    25.         // 2, Create table.  
    26.         onCreateTable(db);  
    27.   
    28.         // 3, Load data  
    29.         sql =   "INSERT INTO " + tableName +  
    30.                 " (" + columns + ") " +  
    31.                 " SELECT " + columns + " FROM " + tempTableName;  
    32.   
    33.         execSQL(db, sql, null);  
    34.   
    35.         // 4, Drop the temporary table.  
    36.         execSQL(db, "DROP TABLE IF EXISTS " + tempTableName, null);  
    37.   
    38.         db.setTransactionSuccessful();  
    39.     }  
    40.     catch (SQLException e)  
    41.     {  
    42.         e.printStackTrace();  
    43.     }  
    44.     catch (Exception e)  
    45.     {  
    46.         e.printStackTrace();  
    47.     }  
    48.     finally  
    49.     {  
    50.         db.endTransaction();  
    51.     }  
    52. }  

    得到数据库表的列名

    我们可以通过SQL表得到表的列名。 这里需要注意的一点,int columnIndex = c.getColumnIndex("name"); 这里根据name去取得index。 

    [java] view plaincopy
     
    1. protected String[] getColumnNames(SQLiteDatabase db, String tableName)  
    2. {  
    3.     String[] columnNames = null;  
    4.     Cursor c = null;  
    5.   
    6.     try  
    7.     {  
    8.         c = db.rawQuery("PRAGMA table_info(" + tableName + ")", null);  
    9.         if (null != c)  
    10.         {  
    11.             int columnIndex = c.getColumnIndex("name");  
    12.             if (-1 == columnIndex)  
    13.             {  
    14.                 return null;  
    15.             }  
    16.   
    17.             int index = 0;  
    18.             columnNames = new String[c.getCount()];  
    19.             for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext())  
    20.             {  
    21.                 columnNames[index] = c.getString(columnIndex);  
    22.                 index++;  
    23.             }  
    24.         }  
    25.     }  
    26.     catch (Exception e)  
    27.     {  
    28.         e.printStackTrace();  
    29.     }  
    30.     finally  
    31.     {  
    32.         closeCursor(c);  
    33.     }  
    34.   
    35.     return columnNames;  
    36. }  

    upgradeTables方法应该是在onUpgrade方法中去调用。

    数据库升级的意义

    在应用程序开发的过程中,数据库的升级是一个很重要的组成部分(如果用到了数据库),因为程序可能会有V1.0,V2.0,当用户安装新版本的程序后,必须要保证用户数据不能丢失,对于数据库设计,如果发生变更(如多添加一张表,表的字段增加或减少等),那么我们必须想好数据库的更新策略。

    1,定义数据库版本

    数据库的版本是一个整型值,在创建SQLiteOpenHelper时,会传入该数据库的版本,如果传入的数据库版本号比数据库文件中存储的版本号大的话,那么SQLiteOpenHelper#onUpgrade()方法就会被调用,我们的升级应该在该方法中完成。

    2,如何写升级逻辑

    假如我们开发的程序已经发布了两个版本:V1.0,V1.2,我们正在开发V1.3。每一版的数据库版本号分别是18,19,20。

    对于这种情况,我们应该如何实现升级?

    用户的选择有:                   

    1) V1.0 -> V1.3  DB 18 -> 20                  

    2) V1.1 -> V1.3  DB 19 -> 20      

    3,注意

    数据库的每一个版本所代表的数据库必须是定义好的,比如说V18的数据库,它可能只有两张表TableA和TableB,如果V19要添加一张表TableC,如果V20要修改TableC,那么每一个版本所对应的数据库结构如下:

    V18  --->  TableA, TableB

    V19  --->  TableA, TableB, TableC

    V20  --->  TableA, TableB, TableC (变更)

    onUpgrade()方法的实现如下:

    [java] view plaincopy
     
    1.        // Pattern for upgrade blocks:  
    2. //  
    3. //    if (upgradeVersion == [the DATABASE_VERSION you set] - 1){  
    4. //        .. your upgrade logic..  
    5. //        upgradeVersion = [the DATABASE_VERSION you set]  
    6. //    }  
    7.   
    8.   
    9. public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)  
    10. {  
    11.     int upgradeVersion  = oldVersion;  
    12.   
    13.     if (18 == upgradeVersion) {  
    14.         // Create table C  
    15.         String sql = "CREATE TABLE ...";  
    16.         db.execSQL(sql);  
    17.         upgradeVersion = 19;  
    18.     }  
    19.   
    20.     if (20 == upgradeVersion) {  
    21.         // Modify table C  
    22.         upgradeVersion = 20;  
    23.     }  
    24.   
    25.     if (upgradeVersion != newVersion) {  
    26.         // Drop tables  
    27.         db.execSQL("DROP TABLE IF EXISTS " + tableName);  
    28.         // Create tables  
    29.         onCreate(db);  
    30.     }  
    31. }  


    从上面的代码可以看到,我们在onUpgrade()方法中,处理了数据库版本从18 -> 20的升级过程,这样做的话,不论用户从18 -> 20,还是从19 -> 20,最终程序的数据库都能升级到V20所对应的数据库结构。  

    4,如何保证数据不丢失

    这是很重要的一部分,假设要更新TableC表,我们建议的做法是:       

    1) 将TableC重命名为TableC_temp

           SQL语句可以这样写:ALERT TABLE TableC RENAME TO TableC_temp;

    2) 创建新的TableC表

    3) 将数据从TableC_temp中插入到TableC表中

           SQL语句可以这样写:INSERT INTO TableC (Col1, Col2, Col3) SELECT (Col1, Col2, Col3) FROM TableC_temp;                

                       
    经过这三步,TableC就完成了更新,同时,也保留了原来表中的数据。  

    注意:

    在onUpgrade()方法中,删除表时,注意使用事务处理,使得修改能立即反应到数据库文件中。       

    SQL语句

    由于Android是使用开源的SQLite3作为其数据库,所以,我们在开发数据库模块时,一定要注意SQLite3支持哪些关键字,函数等,不是所有的关键字,SQLite都是支持的。

    下面列出了一些参考链接:

    SQLite3官方文档:http://sqlite.org/

    W3CSchool网站:http://www.w3school.com.cn/sql/index.asp/

    SQL语句写得好坏能直接影响到数据库的操作。我曾经就遇到过SQL语句影响查询性能,更新3000条记录,用时30移左右,但在对WHERE条件的字段加上索引后,性能提升到3~4秒。

  • 相关阅读:
    【PHP】 lumen 输出sql信息
    Go学习笔记-使用MySQL数据库
    PHP检测函数是否存在
    Javascript边框闪烁提示
    【转】Ubuntu 16.04下 Mysql 5.7.17源码编译与安装
    python-mysql windows diver地址
    【转载】Python Flask 开发环境搭建(Windows)
    【转载】agentzh 的 Nginx 教程(版本 2016.07.21)
    【转载】写给新手看的Flask+uwsgi+Nginx+Ubuntu部署教程
    【转载】从零开始搭建论坛(三):Flask框架简单介绍
  • 原文地址:https://www.cnblogs.com/dongweiq/p/4239267.html
Copyright © 2020-2023  润新知