在将apk发包出去后需要改动Entity的元素,就需要升级数据库。
这时看具体需求,一是不需要原有数据,清除数据库;二是需要保留原有数据。
greendao默认使用的是第一种:
Daomaster:
public static class DevOpenHelper extends OpenHelper {
public DevOpenHelper(Context context, String name) {
super(context, name);
}
public DevOpenHelper(Context context, String name, CursorFactory factory) {
super(context, name, factory);
}
@Override
public void onUpgrade(Database db, int oldVersion, int newVersion) {
Log.i("greenDAO", "Upgrading schema from version " + oldVersion + " to " + newVersion + " by dropping all tables");
dropAllTables(db, true);
onCreate(db);
}
}
这里只说第二种情况。
那么需要升级需要如下操作:
在 CameraTime 中增加 test属性
@Entity
public class CameraTime {
@Id
private Long id;
private String time;
private String standUuid;
public String test;
}
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;
import org.greenrobot.greendao.AbstractDaoMaster;
import org.greenrobot.greendao.AbstractDaoSession;
import org.greenrobot.greendao.database.Database;
import org.greenrobot.greendao.database.DatabaseOpenHelper;
import org.greenrobot.greendao.identityscope.IdentityScopeType;
/**
* Created by zhangli
*
* @time 2018/1/5
*/
public class MyDaoMaster extends AbstractDaoMaster {
public static final int SCHEMA_VERSION = 2;
public MyDaoMaster(Database db, int schemaVersion) {
super(db, schemaVersion);
}
@Override
public AbstractDaoSession newSession() {
return null;
}
@Override
public AbstractDaoSession newSession(IdentityScopeType type) {
return null;
}
public static void createAllTables(Database db, boolean ifNotExists) {
CameraEntityDao.createTable(db, ifNotExists);
CameraTimeDao.createTable(db, ifNotExists);
FaroEntityDao.createTable(db, ifNotExists);
FaroFileDao.createTable(db, ifNotExists);
FloorPlanPictureDao.createTable(db, ifNotExists);
PictureMatrixDao.createTable(db, ifNotExists);
ProjectDao.createTable(db, ifNotExists);
StandingEntityDao.createTable(db, ifNotExists);
UpDataIdDao.createTable(db, ifNotExists);
UserDao.createTable(db, ifNotExists);
ImageRowDao.createTable(db, ifNotExists);
}
/** Drops underlying database table using DAOs. */
public static void dropAllTables(Database db, boolean ifExists) {
CameraEntityDao.dropTable(db, ifExists);
CameraTimeDao.dropTable(db, ifExists);
FaroEntityDao.dropTable(db, ifExists);
FaroFileDao.dropTable(db, ifExists);
FloorPlanPictureDao.dropTable(db, ifExists);
PictureMatrixDao.dropTable(db, ifExists);
ProjectDao.dropTable(db, ifExists);
StandingEntityDao.dropTable(db, ifExists);
UpDataIdDao.dropTable(db, ifExists);
UserDao.dropTable(db, ifExists);
ImageRowDao.dropTable(db, ifExists);
}
public static abstract class OpenHelper extends DatabaseOpenHelper {
public OpenHelper(Context context, String name) {
super(context, name, SCHEMA_VERSION);
}
public OpenHelper(Context context, String name, SQLiteDatabase.CursorFactory factory) {
super(context, name, factory, SCHEMA_VERSION);
}
@Override
public void onCreate(Database db) {
Log.i("greenDAO", "Creating tables for schema version " + SCHEMA_VERSION);
createAllTables(db, false);
}
}
/** WARNING: Drops all table on Upgrade! Use only during development. */
public static class DevOpenHelper extends OpenHelper {
public DevOpenHelper(Context context, String name) {
super(context, name);
}
public DevOpenHelper(Context context, String name, SQLiteDatabase.CursorFactory factory) {
super(context, name, factory);
}
@Override
public void onUpgrade(Database db, int oldVersion, int newVersion) {
Log.i("greenDAO", "Upgrading schema from version " + oldVersion + " to " + newVersion + " by dropping all tables");
switch (oldVersion) {
case 1:
CameraTimeDao.createTable(db, true);
db.execSQL("ALTER TABLE 'CAMERA_TIME' ADD 'TEST' TEXT;");
break;
}
}
}
}
在 onUpgrade 方法中,当旧版本号为1时,增加一个 test 的字段在 CameraTime 表中。
不知道数据库表的命名规则的话,可以将数据库导出本地打开看即可。
在 MyDaoMashter 和 build.gradle 中改变版本号
greendao {
schemaVersion 2
daoPackage 'com.build.scan.greendao.gen'
targetGenDir 'src/main/java'
}
apk安装在版本号为1的的时候即可生效。