Android GreenDao framework using advanced database upgrade

reference

Github

series

Android GreenDao framework uses basic articles
Android GreenDao framework uses additions, deletions and changes to check articles
Android GreenDao framework uses advanced articles queryBuilder

If GreenDao directly modify the entity class, the data will be cleared directly if the version is added. This problem can only be solved through external forces.

1. Add dependency

Add in the build.gradle file of the project

allprojects {
    
    
    repositories {
    
    
       ...
        maven {
    
     url "https://jitpack.io" }
    }
}

Add in the project's build.gradle file

 implementation 'io.github.yuweiguocn:GreenDaoUpgradeHelper:v2.2.1'

The currently cited version is the latest version, and subsequent updates should pay attention to the reference document above.

2. Rewrite the Helper class

public class GreenDaoUpgradeHelper extends DaoMaster.OpenHelper {
    
    


    public GreenDaoUpgradeHelper(Context context, String name, SQLiteDatabase.CursorFactory factory) {
    
    
        super(context, name, factory);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    
    
        super.onUpgrade(db, oldVersion, newVersion);
        MigrationHelper.migrate(db, new MigrationHelper.ReCreateAllTableListener() {
    
    
            @Override
            public void onCreateAllTables(Database db, boolean ifNotExists) {
    
    
                DaoMaster.createAllTables(db, true);
            }

            @Override
            public void onDropAllTables(Database db, boolean ifExists) {
    
    
                DaoMaster.dropAllTables(db, true);
            }
        }, TestUserDao.class);
    }
}

You must pay attention to the parameter of TestUserDao.class, because the monitoring method of the shortcut key does not have this parameter, and no error is reported. If you run it directly, it doesn't know which table to upgrade. Multiple upgrade table classes can be placed in this position.

Three. Replace

public class GreenDaoManager {
    
    
    private static GreenDaoManager greenDaoManager;
    private DaoSession daoSession;

    /**
     * 私有化
     */
    private GreenDaoManager(){
    
    

    }

    /**
     * 对外暴露的获取方法
     * @return 单例对象
     */
    public static GreenDaoManager getInstance(){
    
    
        if (null==greenDaoManager){
    
    
            synchronized (GreenDaoManager.class){
    
    
                if (null==greenDaoManager){
    
    
                    greenDaoManager=new GreenDaoManager();
                }
            }
        }
        return greenDaoManager;
    }

    /**
     * 数据库初始化
     * @param context 上下文
     */
    public void init(Context context){
    
    
    	//替换位置
        GreenDaoUpgradeHelper helper = new GreenDaoUpgradeHelper(context, "test-db", null);
        SQLiteDatabase db = helper.getWritableDatabase();
        DaoMaster daoMaster = new DaoMaster(db);
        daoSession = daoMaster.newSession();
    }

    /**
     * 获取管理特定架构的所有可用DAO对象
     * @return daoSession
     */
    public DaoSession getDaoSession(){
    
    
        return daoSession;
    }
}

Four. Upgrade version

Modify in the project's build.gradle file

greendao {
    
    
        schemaVersion 2
}

Five. Modify the entity class

@Entity
public class TestUser {
    
    
    @Id(autoincrement = true)
    private Long id;
    @NotNull
    private String name;
    @NotNull
    private int age;

    private String character;
    private String status;

    @NotNull
    private String sex;
    @Index(unique = true)
    private String uid;
    @Generated(hash = 223104968)
    public TestUser(Long id, @NotNull String name, int age, String character,
            String status, @NotNull String sex, String uid) {
    
    
        this.id = id;
        this.name = name;
        this.age = age;
        this.character = character;
        this.status = status;
        this.sex = sex;
        this.uid = uid;
    }
    @Generated(hash = 925009630)
    public TestUser() {
    
    
    }
    public Long getId() {
    
    
        return this.id;
    }
    public void setId(Long id) {
    
    
        this.id = id;
    }
    public String getName() {
    
    
        return this.name;
    }
    public void setName(String name) {
    
    
        this.name = name;
    }
    public int getAge() {
    
    
        return this.age;
    }
    public void setAge(int age) {
    
    
        this.age = age;
    }
    public String getSex() {
    
    
        return this.sex;
    }
    public void setSex(String sex) {
    
    
        this.sex = sex;
    }
    public String getUid() {
    
    
        return this.uid;
    }
    public void setUid(String uid) {
    
    
        this.uid = uid;
    }
    public String getCharacter() {
    
    
        return this.character;
    }
    public void setCharacter(String character) {
    
    
        this.character = character;
    }
    public String getStatus() {
    
    
        return this.status;
    }
    public void setStatus(String status) {
    
    
        this.status = status;
    }
}

Click Build → Make Project and the code will be automatically generated.

The data will not be lost after completion. After the first 3 steps are completed, you can repeat steps 4 and 5.

Guess you like

Origin blog.csdn.net/m0_48440239/article/details/114311035