前一篇讲了greendao3.0的基本使用:http://blog.csdn.net/huangxiaoguo1/article/details/52916189
这一篇看看数据库到底该怎么升级呢?看我升级后的效果
看增加了一个NUM字段 。
最新有小伙伴遇到数据库升级问题了,说网上都是2.0版本的升级方法,自己使用的是3.0,没法升级数据库了….
然后问别人,别人让他改为2.0版本,让数据库里面有两个表供应用操作,天啦这还了得,我用的也是3.0,那以后还得了,非把人累死……..
数据库的升级大家都知道,创建临时表,进行过渡储存而已,网上有人提供了一个MigrationHelper类,真好正好可以使用!
but 蹦出了这样一个大bug,请看:
什么新增字段不能为空? 我没有设置呀,到底为什么呢?
我相信有的小伙伴也遇到过吧!那下面我们一起来看看怎么回事吧
思路:
创建临时表-->删除原表-->创建新表-->复制临时表数据到新表并删除临时表;这样数据库表的更新就完成了
package cn.hnshangyu.testgreendao.helper;
import android.database.Cursor;
import android.text.TextUtils;
import android.util.Log;
import org.greenrobot.greendao.AbstractDao;
import org.greenrobot.greendao.database.Database;
import org.greenrobot.greendao.internal.DaoConfig;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import cn.hnshangyu.testgreendao.greendao.DaoMaster;
public class MigrationHelper {
private static final String CONVERSION_CLASS_NOT_FOUND_EXCEPTION = "MIGRATION HELPER - CLASS DOESN'T MATCH WITH THE CURRENT PARAMETERS";
private static MigrationHelper instance;
public static MigrationHelper getInstance() {
if (instance == null) {
instance = new MigrationHelper();
}
return instance;
}
public void migrate(Database db, Class extends AbstractDao, ?>>... daoClasses) {
generateTempTables(db, daoClasses);
DaoMaster.dropAllTables(db, true);
DaoMaster.createAllTables(db, false);
restoreData(db, daoClasses);
}
/** * 生成临时列表 * * @param db * @param daoClasses */
private void generateTempTables(Database db, Class extends AbstractDao, ?>>... daoClasses) {
for (int i = 0; i < daoClasses.length; i++) {
DaoConfig daoConfig = new DaoConfig(db, daoClasses[i]);
String divider = "";
String tableName = daoConfig.tablename;
String tempTableName = daoConfig.tablename.concat("_TEMP");
ArrayList properties = new ArrayList<>();
StringBuilder createTableStringBuilder = new StringBuilder();
createTableStringBuilder.append("CREATE TABLE ").append(tempTableName).append(" (");
for (int j = 0; j < daoConfig.properties.length; j++) {
String columnName = daoConfig.properties[j].columnName;
if (getColumns(db, tableName).contains(columnName)) {
properties.add(columnName);
String type = null;
try {
type = getTypeByClass(daoConfig.properties[j].type);
} catch (Exception exception) {
exception.printStackTrace();
}
createTableStringBuilder.append(divider).append(columnName).append(" ").append(type);
if (daoConfig.properties[j].primaryKey) {
createTableStringBuilder.append(" PRIMARY KEY");
}
divider = ",";
}
}
createTableStringBuilder.append(");");
db.execSQL(createTableStringBuilder.toString());
StringBuilder insertTableStringBuilder = new StringBuilder();
insertTableStringBuilder.append("INSERT INTO ").append(tempTableName).append(" (");
insertTableStringBuilder.append(TextUtils.join(",", properties));
insertTableStringBuilder.append(") SELECT ");
insertTableStringBuilder.append(TextUtils.join(",", properties));
insertTableStringBuilder.append(" FROM ").append(tableName).append(";");
db.execSQL(insertTableStringBuilder.toString());
}
}
/** * 存储新的数据库表 以及数据 * * @param db * @param daoClasses */
private void restoreData(Database db, Class extends AbstractDao, ?>>... daoClasses) {
for (int i = 0; i < daoClasses.length; i++) {
DaoConfig daoConfig = new DaoConfig(db, daoClasses[i]);
String tableName = daoConfig.tablename;
String tempTableName = daoConfig.tablename.concat("_TEMP");
ArrayList properties = new ArrayList();
for (int j = 0; j < daoConfig.properties.length; j++) {
String columnName = daoConfig.properties[j].columnName;
if (getColumns(db, tempTableName).contains(columnName)) {
properties.add(columnName);
}
}
StringBuilder insertTableStringBuilder = new StringBuilder();
insertTableStringBuilder.append("INSERT INTO ").append(tableName).append(" (");
insertTableStringBuilder.append(TextUtils.join(",", properties));
insertTableStringBuilder.append(") SELECT ");
insertTableStringBuilder.append(TextUtils.join(",", properties));
insertTableStringBuilder.append(" FROM ").append(tempTableName).append(";");
StringBuilder dropTableStringBuilder = new StringBuilder();
dropTableStringBuilder.append("DROP TABLE ").append(tempTableName);
db.execSQL(insertTableStringBuilder.toString());
db.execSQL(dropTableStringBuilder.toString());
}
}
private String getTypeByClass(Class> type) throws Exception {
if (type.equals(String.class)) {
return "TEXT";
}
if (type.equals(Long.class) || type.equals(Integer.class) || type.equals(long.class)) {
return "INTEGER";
}
if (type.equals(Boolean.class)) {
return "BOOLEAN";
}
Exception exception = new Exception(CONVERSION_CLASS_NOT_FOUND_EXCEPTION.concat(" - Class: ").concat(type.toString()));
exception.printStackTrace();
throw exception;
}
private List getColumns(Database db, String tableName) {
List columns = new ArrayList<>();
Cursor cursor = null;
try {
cursor = db.rawQuery("SELECT * FROM " + tableName + " limit 1", null);
if (cursor != null) {
columns = new ArrayList<>(Arrays.asList(cursor.getColumnNames()));
}
} catch (Exception e) {
Log.v(tableName, e.getMessage(), e);
e.printStackTrace();
} finally {
if (cursor != null)
cursor.close();
}
return columns;
}
}
注意:
这个类为配置greendao后系统自动生成,不能直接在这里操作,不然每次运行此类都是重新生成的。
package cn.hnshangyu.testgreendao.helper;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;
import org.greenrobot.greendao.database.Database;
import cn.hnshangyu.testgreendao.greendao.DaoMaster;
import cn.hnshangyu.testgreendao.greendao.StudentDao;
public class MyOpenHelper extends DaoMaster.OpenHelper {
public MyOpenHelper(Context context, String name, SQLiteDatabase.CursorFactory factory) {
super(context, name, factory);
}
/** * 数据库升级 * @param db * @param oldVersion * @param newVersion */
@Override
public void onUpgrade(Database db, int oldVersion, int newVersion) {
//操作数据库的更新 有几个表升级都可以传入到下面
MigrationHelper.getInstance().migrate(db,StudentDao.class);
}
}
之前的greendao3.0以上使用步骤(一)中,我创建了一个DbManager管理类来进行对数据库的统一管理,今天同样使用这个类:
那么与之前的有什么区别呢??
其实就是在获取DaoMaster时有所改变,打开数据库方式不同
1、之前的方法
/** * 获取DaoMaster * * @param context * @return */
public static DaoMaster getDaoMaster(Context context) {
if (null == mDaoMaster) {
synchronized (DbManager.class) {
if (null == mDaoMaster) {
mDaoMaster = new DaoMaster(getWritableDatabase(context));
}
}
}
return mDaoMaster;
}
2、现在的
/** * 获取DaoMaster * * 判断是否存在数据库,如果没有则创建数据库 * @param context * @return */
public static DaoMaster getDaoMaster(Context context) {
if (null == mDaoMaster) {
synchronized (DbManager.class) {
if (null == mDaoMaster) {
MyOpenHelper helper = new MyOpenHelper(context,DB_NAME,null);
mDaoMaster = new DaoMaster(helper.getWritableDatabase());
}
}
}
return mDaoMaster;
}
3、整个类的类容:
package cn.hnshangyu.testgreendao.db;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import cn.hnshangyu.testgreendao.greendao.DaoMaster;
import cn.hnshangyu.testgreendao.greendao.DaoSession;
import cn.hnshangyu.testgreendao.helper.MyOpenHelper;
public class DbManager {
// 是否加密
public static final boolean ENCRYPTED = true;
private static final String DB_NAME = "test.db";
private static DbManager mDbManager;
private static DaoMaster.DevOpenHelper mDevOpenHelper;
private static DaoMaster mDaoMaster;
private static DaoSession mDaoSession;
private Context mContext;
private DbManager(Context context) {
this.mContext = context;
// 初始化数据库信息
mDevOpenHelper = new DaoMaster.DevOpenHelper(context, DB_NAME);
getDaoMaster(context);
getDaoSession(context);
}
public static DbManager getInstance(Context context) {
if (null == mDbManager) {
synchronized (DbManager.class) {
if (null == mDbManager) {
mDbManager = new DbManager(context);
}
}
}
return mDbManager;
}
/** * 获取可读数据库 * * @param context * @return */
public static SQLiteDatabase getReadableDatabase(Context context) {
if (null == mDevOpenHelper) {
getInstance(context);
}
return mDevOpenHelper.getReadableDatabase();
}
/** * 获取可写数据库 * * @param context * @return */
public static SQLiteDatabase getWritableDatabase(Context context) {
if (null == mDevOpenHelper) {
getInstance(context);
}
return mDevOpenHelper.getWritableDatabase();
}
/** * 获取DaoMaster * * 判断是否存在数据库,如果没有则创建数据库 * @param context * @return */
public static DaoMaster getDaoMaster(Context context) {
if (null == mDaoMaster) {
synchronized (DbManager.class) {
if (null == mDaoMaster) {
MyOpenHelper helper = new MyOpenHelper(context,DB_NAME,null);
mDaoMaster = new DaoMaster(helper.getWritableDatabase());
}
}
}
return mDaoMaster;
}
/** * 获取DaoMaster * * @param context * @return */
// public static DaoMaster getDaoMaster(Context context) {
// if (null == mDaoMaster) {
// synchronized (DbManager.class) {
// if (null == mDaoMaster) {
//
// mDaoMaster = new DaoMaster(getWritableDatabase(context));
// }
// }
// }
// return mDaoMaster;
// }
/** * 获取DaoSession * * @param context * @return */
public static DaoSession getDaoSession(Context context) {
if (null == mDaoSession) {
synchronized (DbManager.class) {
mDaoSession = getDaoMaster(context).newSession();
}
}
return mDaoSession;
}
}
package cn.hnshangyu.testgreendao.bean;
import org.greenrobot.greendao.annotation.Entity;
import org.greenrobot.greendao.annotation.Id;
import org.greenrobot.greendao.annotation.Keep;
import org.greenrobot.greendao.annotation.Generated;
import org.greenrobot.greendao.annotation.NotNull;
@Entity(generateConstructors = false)
public class Student {
@Id
private Long id;
private String name;
private int age;
private int num;
public int getNum() {
return num;
}
public void setNum(int num) {
this.num = num;
}
@Override
public String toString() {
return "Student{" +
"id=" + id +
", name='" + name + '\'' +
", age=" + age +
", num=" + num +
'}';
}
public Student() {
}
@Keep
public Student(String name, int age) {
this.name = name;
this.age = age;
}
public Student(Long id, String name, int age) {
this.id = id;
this.name = name;
this.age = age;
}
@Keep
public Long getId() {
return id;
}
@Keep
public void setId(Long id) {
this.id = id;
}
@Keep
public String getName() {
return name;
}
@Keep
public void setName(String name) {
this.name = name;
}
@Keep
public int getAge() {
return age;
}
@Keep
public void setAge(int age) {
this.age = age;
}
@Keep
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof Student)) return false;
Student student = (Student) o;
return name.equals(student.name);
}
@Keep
@Override
public int hashCode() {
return (int) (id ^ (id >>> 32));
}
}
大家看到了我加了一个int类型的字段
版本升为2了,同步一下
然后运行程序………
哎呀!你会发现报了一个重大bug,就是开始我提到的新增字段不能为空,这是为什么呢?bean对象中我没有限定不能为空啊!
好吧我们看看怎么回事吧!
MigrationHelper创建表的时候调用的是DaoMaster的createAllTables方法:
/** Creates underlying database table using DAOs. */
public static void createAllTables(Database db, boolean ifNotExists) {
StudentDao.createTable(db, ifNotExists);
}
从greendao生成的源码中可以看到调用的是StudentDao的createTable方法
那么在StudentDao中:
/** Creates the underlying database table. */
public static void createTable(Database db, boolean ifNotExists) {
String constraint = ifNotExists? "IF NOT EXISTS ": "";
db.execSQL("CREATE TABLE " + constraint + "\"STUDENT\" (" + //
"\"_id\" INTEGER PRIMARY KEY ," + // 0: id
"\"NAME\" TEXT," + // 1: name
"\"AGE\" INTEGER NOT NULL ," + // 2: age
"\"NUM\" INTEGER NOT NULL);"); // 3: num
}
哎呀!发现了,INTEGER NOT NULL (integer)不能为空?这下知道原因了,int类型的是不能为空的怎么办呢??
我们又看到了NAME这个字段他是TEXT类型,也就是String类型的可以为空,
因此,我们先把NUM字段改为String类型
package cn.hnshangyu.testgreendao.bean;
import org.greenrobot.greendao.annotation.Entity;
import org.greenrobot.greendao.annotation.Id;
import org.greenrobot.greendao.annotation.Keep;
import org.greenrobot.greendao.annotation.Generated;
import org.greenrobot.greendao.annotation.NotNull;
@Entity(generateConstructors = false)
public class Student {
@Id
private Long id;
private String name;
private int age;
private String num;
public String getNum() {
return num;
}
public void setNum(String num) {
this.num = num;
}
@Override
public String toString() {
return "Student{" +
"id=" + id +
", name='" + name + '\'' +
", age=" + age +
", num=" + num +
'}';
}
public Student() {
}
@Keep
public Student(String name, int age) {
this.name = name;
this.age = age;
}
public Student(Long id, String name, int age) {
this.id = id;
this.name = name;
this.age = age;
}
@Keep
public Long getId() {
return id;
}
@Keep
public void setId(Long id) {
this.id = id;
}
@Keep
public String getName() {
return name;
}
@Keep
public void setName(String name) {
this.name = name;
}
@Keep
public int getAge() {
return age;
}
@Keep
public void setAge(int age) {
this.age = age;
}
@Keep
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof Student)) return false;
Student student = (Student) o;
return name.equals(student.name);
}
@Keep
@Override
public int hashCode() {
return (int) (id ^ (id >>> 32));
}
}
重新进行上述数据库更新操作,把bean对象中的num改为了String类型,
运行后结果正常,数据库更新了!
/** Creates the underlying database table. */
public static void createTable(Database db, boolean ifNotExists) {
String constraint = ifNotExists? "IF NOT EXISTS ": "";
db.execSQL("CREATE TABLE " + constraint + "\"STUDENT\" (" + //
"\"_id\" INTEGER PRIMARY KEY ," + // 0: id
"\"NAME\" TEXT," + // 1: name
"\"AGE\" INTEGER NOT NULL ," + // 2: age
"\"NUM\" TEXT);"); // 3: num
}
新的字段NUM为TEXT类型,一切正常!
总结:在greendao3.0升级数据库时
我们基本思路为:
创建临时表-->删除原表-->创建新表-->复制临时表数据到新表并删除临时表
并且我们新增加的和修改的字段做好为String类型,避免字段不能为null的情况发生
到这里greendao3.0数据库更新说明完毕……
有什么不妥,欢迎指出,谢谢啦………
Demo下载地址:http://download.csdn.net/download/huangxiaoguo1/9738797
如果你的数据库需要加密的话请看下一篇:greendao3.0以上使用步骤(三):数据库加密