(管用)Sqlite數據庫升級

調用構造器DBHelper中super裏面的方法,增大newVersion的值,就會自動執行onUpgrade();增加數據庫字段的sql語句: String upgradeGoods = “alter table Person add column " + addColGoods + " text”;db.execSQL(upgradeGoods);

 private static int newVersion = 1;
  public DBHelper(DBConfig dbConfig) {
        super(mContext, dbConfig.getDbName(), null, newVersion);
        mDBConfig = dbConfig;
        Log.e("wy", "數據庫版本號: " + dbConfig.getDbVersion() + "數據庫名稱:" + dbConfig.getDbName());

    }

全代碼

package com.wintec.huashang.dbUtil;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

/**
 * Created by Administrator on 2016/5/27.
 */
public class DBHelper extends SQLiteOpenHelper {
    private static Context mContext;
    private DBConfig mDBConfig;
    //    數據庫版本號,初始版本號爲1
    private static int newVersion = 1;

    public static void init(Context context) {
        mContext = context;
    }

    public DBHelper(DBConfig dbConfig) {
        super(mContext, dbConfig.getDbName(), null, newVersion);
        mDBConfig = dbConfig;
        Log.e("wy", "數據庫版本號: " + dbConfig.getDbVersion() + "數據庫名稱:" + dbConfig.getDbName());

    }


    @Override
    public void onCreate(SQLiteDatabase db) {
        Log.e("wy", "run: " + "創建表了");
        db.execSQL("create table Person(_id integer primary key autoincrement,idc integer,itemCode text,name text,price real,unitId integer,classifyId integer,classifyName text,previewImage text,searchKey text,parentClassifyId integer,parentClassifyName text,unitPrint text)");

    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        Log.e("wy", "run: " + "執行數據庫更新方法");
        if (mDBConfig.getOnUpgradeListener() != null) {

            mDBConfig.getOnUpgradeListener().onUpgrade();
        }

//升級數據庫,不改變表結構 注意空格
        //添加列 addcol_goods2 , text 爲字符串數據類型 ,person爲表名
        //alter table person add column addcol_goods2 text
        //添加的列名
        String addColGoods = "addcol_goods";
        //添加列的sql語句
        String upgradeGoods = "alter table Person add column " + addColGoods + " text";
        //執行sql語句 一次只能添加一個字段
        db.execSQL(upgradeGoods);


//        if(oldVersion != newVersion)
//        {
//            switch (newVersion)
//            {
////                第二個版本
//                case 2:
//
//                    //升級數據庫,不改變表結構 注意空格
//                    //添加列 addcol_goods2 , text 爲字符串數據類型 ,person爲表名
//                    //alter table person add column addcol_goods2 text
//                    //添加的列名
//                    String addColGoods = "addcol_goods";
//                    //添加列的sql語句
//                    String upgradeGoods = "alter table Person add column "+ addColGoods+" text";
//                    //執行sql語句 一次只能添加一個字段
//                    db.execSQL(upgradeGoods);
//
//                    break;
//
//
//            }
//        }
    }
}

https://www.cnblogs.com/CharlesGrant/p/9144253.html

https://blog.csdn.net/xiaol206/article/details/80161363

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章