标签:
前序:
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
for (int j = oldVersion; j <= newVersion; j++) {
switch (j) {
case 2:
//创建临时表
db.execSQL(TEMP_SQL_CREATE_TABLE_SUBSCRIBE);
//执行OnCreate方法,这个方法中放的是表的初始化操作工作,比如创建新表之类的
onCreate(db);
//删除之前的表里面的那4条默认的数据
for (int i = 0; i < arrWhereAct.length; i++) {
db.execSQL(DELETE_TEMP_SUBSCRIBE + arrWhereAct[i]);
}
//将临时表中的数据放入表A
Cursor cursor = db.rawQuery(INSERT_SUBSCRIBE, null);
if (cursor.moveToFirst()) {
do {
db.execSQL(cursor.getString(cursor
.getColumnIndex("insertSQL")));
} while (cursor.moveToNext());
}
cursor.close();
//将临时表删除掉
db.execSQL(DROP_TEMP_SUBSCRIBE);
break;
default:
break;
}
}
}
import java.util.ArrayList;
import java.util.List;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
/**
* @description:数据库帮助类
* @author samy
* @date 2015年6月16日 下午8:41:18
*/
public class DBHelper extends SQLiteOpenHelper {
public static final String NAME = Constant.DATABASE_NAME; // DB name
public static final List<DBUpdateListener> mUpdateList = new ArrayList<DBUpdateListener>();
public DBHelper(Context context) {
// super(context, NAME, null, Constant.DATABASE_VERSION);
super(context, NAME, null, 133);
// version 187:新增tips表和app_recommend表
// version 188:新增matchsync表
// version 189:新增widget_status表
}
public DBHelper(Context context, String name, CursorFactory factory, int version) {
super(context, name, factory, version);
}
/**
* 用户第一次使用软件时调用的操作,用于获取数据库创建语句(SW),然后创建数据库
*/
@Override
public void onCreate(SQLiteDatabase db) {
IMDebug.print("创建--会员版--数据库!!!");
db.execSQL(BaseModelTool.getCreateTableSql(UserInfo.class));
db.execSQL(BaseModelTool.getCreateTableSql(ShopIndustry.class)); // 行业
/** V2.0.0版本的数据库变更,增加最后一次聊天的表 */
db.execSQL(BaseModelTool.getCreateTableSql(GroupAssistantStatisticModel.class));
}
/**
* 数据库更新,如果数据库有更新,那么调用BaseModelTool.getAlterTableSql((Class<?> modelType, String string);方法向表中添加相应新字段
*/
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
int tempVersion = oldVersion;
addDBUpdateListener();
for (int i = 0; i < mUpdateList.size(); i++) {
if (mUpdateList.get(i).getmPreVersionNo() >= tempVersion) {
mUpdateList.get(i).onUpgrade(db, tempVersion, newVersion);
tempVersion = mUpdateList.get(i).getmCurrVersionNo();
}
}
mUpdateList.clear();
}
private void addDBUpdateListener() {
// v1.5.7 --> v2.0.0 个人信息里面惠信余额和银行卡数的添加
mUpdateList.add(new DBUpdate_V131_V132());
}
/**
* @description:观察者模式 IM数据库:v1.5.7 --> v2.0.0
* @author samy
* @date 2015年6月16日 下午8:39:43
*/
private class DBUpdate_V131_V132 extends DBUpdateListener {
public DBUpdate_V131_V132() {
setmPreVersionNo(132);
setmCurrVersionNo(133);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
String sql="";
if (!checkColumnExist(db, "userInfo", "earnings")) {
sql = "alter table userInfo add earnings text;";
}
if (!checkColumnExist(db, "userInfo", "accountbalance")) {
sql = "alter table userInfo add accountbalance text;";
}
if (!checkColumnExist(db, "userInfo", "bankcardnum")) {
sql = "alter table userInfo add bankcardnum text;";
}
db.execSQL(sql);
db.execSQL(BaseModelTool.getDropTableSql(IMGroupRelation.class));
}
}
/**
* @description:判断字段是否在这个表中存在,对字段进行处理时要记得先判断
* @author samy
* @date 2015年6月16日 下午8:36:39
*/
private boolean checkColumnExist(SQLiteDatabase db, String tableName, String columnName) {
boolean result = false;
Cursor cursor = null;
try {
cursor = db.rawQuery("SELECT * FROM " + tableName + " LIMIT 0", null);// 查询一行
result = cursor != null && cursor.getColumnIndex(columnName) != -1;
}
catch (Exception e) {
Log.e("SQL", "checkColumnExists..." + e.getMessage());
}
finally {
if (null != cursor && !cursor.isClosed()) {
cursor.close();
}
}
return result;
}
/**
* @description:判断表是否存在
* @author samy
* @date 2015年6月16日 下午8:36:19
*/
public boolean tabbleIsExist(SQLiteDatabase db, String tableName) {
boolean result = false;
if (tableName == null) { return false; }
Cursor cursor = null;
try {
db = this.getReadableDatabase();
String sql = "select count(*) from sqlite_master where type =‘table‘ and name =‘" + tableName.trim() + "‘ ";
cursor = db.rawQuery(sql, null);
if (cursor.moveToNext()) {
int count = cursor.getInt(0);
if (count > 0) {
result = true;
}
}
}
catch (Exception e) {
// TODO: handle exception
}
finally {
if (null != cursor && !cursor.isClosed()) {
cursor.close();
}
}
return result;
}
private abstract class DBUpdateListener {
private int mPreVersionNo;
private int mCurrVersionNo;
public abstract void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion);
@SuppressWarnings("unused")
public int getmCurrVersionNo() {
return mCurrVersionNo;
}
protected void setmCurrVersionNo(int mCurrVersionNo) {
this.mCurrVersionNo = mCurrVersionNo;
}
@SuppressWarnings("unused")
public int getmPreVersionNo() {
return mPreVersionNo;
}
protected void setmPreVersionNo(int mPreVersionNo) {
this.mPreVersionNo = mPreVersionNo;
}
}
}
/**各个环境对应不同的环境数据库名称,避免数据db冲突*/
public static final String ENVIRONMENT_FLAG = getEnvironmentFlag();
private static final String getEnvironmentFlag() {
if (Configuration.IS_RELEASE_ENVIRONMENT) {
return "";
} else {
if (Configuration.IS_RELEASE_TEST_ENVIRONMENT) {
return "_release_test";
} else {
return "_test";
}
}
}
/** 数据库的名称 **/
public static final String DATABASE_NAME = getDatabaseName();
private static final String getDatabaseName() {
return "member" + ENVIRONMENT_FLAG + ".db";
}
标签:
原文地址:http://www.cnblogs.com/hongfeiliuxing/p/4581776.html