
其实数据库版本升级比较麻烦的就是数据的迁移,data migration。
数据库版本升级有很多方法,按不同需求来处理。
本质上是去执行sql语句去创建临时数据表,然后迁移数据,修改临时表名等。
数据版本升级,为了便于维护代码可以先定义一个抽象类
public abstract class AbstractMigratorHelper {
public abstract void onUpgrade(SQLiteDatabase db)
}1234
然后让自己更新数据库逻辑的类继承这个类
public class DBMigrationHelper6 extends AbstractMigratorHelper {
/* Upgrade from DB schema 6 to schema 7 , version numbers are just examples*/
public void onUpgrade(SQLiteDatabase db) {
/* Create a temporal table where you will copy all the data from the previous table that you need to modify with a non supported sqlite operation */
db.execSQL("CREATE TABLE " + "'post2' (" + //
"'_id' INTEGER PRIMARY KEY ," + // 0: id
"'POST_ID' INTEGER UNIQUE ," + // 1: postId
"'USER_ID' INTEGER," + // 2: userId
dTypes {release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
greendao{
schemaVersion 1
daoPackage 'com.admom.mygreendaotest'
targetGenDir 'src/main/java'
}
//schemaVersion: 数据库schema版本,也可以理解为数据库版本号
//daoPackage:设置DaoMaster、DaoSession、Dao包名
//targetGenDir:设置DaoMaster、DaoSession、Dao目录
//targetGenDirTest:设置生成单元测试目录
//generateTests:设置自动生成单元测试用例
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)