Android GreenDao database upgrade (with Demo)

Insert image description here

Preface

       Long time no see everyone, it’s already late August in the blink of an eye. I’ve been busy with work lately, so I don’t have time to update my blog. I took some time out of my busy schedule to update everyone aboutthe upgrade of GreenDao3+ database.

       For a detailed introduction to GreenDao and some logical information增、删、改、查 etc., you can refer to an article I wrote last year, "Android GreenDao 3.2. 2 Detailed explanation and actual combat (with Demo)》
Insert image description here


       During version iteration, we often need to upgrade the database, and GreenDao's defaultDaoMaster.DevOpenHelper will delete the old table and then create a new table when upgrading data. There is no Migrate old data to new tables, resulting in data loss.

       This is not advisable in actual development project applications, so we need to make adjustments.

       Of course, before this, I also researched and consulted with Du Niang, and found that many articles were written in a messy manner, and in the end, the actual operation did not achieve the expected results, but most of them were for method. are all handled in the Database version upgradesonUpgrade(Database db,int olbVersion,int newVersion)

       It is not that there is no solution in the end, 网上有不少关于MigrationHelper的源码, it mainly creates a temporary table to migrate the data of the old table to the new table. You can go to Baidu to see the source code. Bar. Next, let’s talk about my personal actual operation!

Project actual combat

What is used here is packaged by Yu WeiguoGreenDaoUpgradeHelper, < a i=4> It is a GreenDao. It can be used to solve database upgrade problems easily and requires only one line of code. https://github.com/yuweiguocn/GreenDaoUpgradeHelper
数据库升级帮助类

1,Introduction:implementation 'com.github.yuweiguocn:GreenDaoUpgradeHelper:v2.1.0'

2. Create a new class, inherit DaoMaster.OpenHelper, override the onUpgrade(Database db,int olbVersion,int newVersion) method, and use MigrationHelper in this method. Database upgrade and data migration.

 MigrationHelper.migrate(Database db, 
 		new MigrationHelper.ReCreateAllTableListener() {
    
    

 		}, xxxDao.class);

db: database operation object
ReCreateAllTableListener: listener for re-creating the table
xxxDao.class: database operation object corresponding to the bean

public class MyOpenHelper extends DaoMaster.OpenHelper {
    
    

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

    @Override
    public void onUpgrade(Database db, int oldVersion, int newVersion) {
    
    

        //把需要管理的数据库表DAO作为最后一个参数传入到方法中
        MigrationHelper.migrate(db, new MigrationHelper.ReCreateAllTableListener() {
    
    

            @Override
            public void onCreateAllTables(Database db, boolean ifNotExists) {
    
    
                DaoMaster.createAllTables(db, ifNotExists);
            }

            @Override
            public void onDropAllTables(Database db, boolean ifExists) {
    
    
                DaoMaster.dropAllTables(db, ifExists);
            }
        }, RecordBeanDao.class);// 修改beanDao对象
    }
}

3. In the database initialization place, use MyOpenHelper instead of DaoMaster.DevOpenHelper to create the database and other operations.

//封装数据库的创建、更新、删除
MyOpenHelper openHelper = new MyOpenHelper(AppApplication.getContext(),DB_NAME,null);
//获取数据库
SQLiteDatabase mDb = openHelper.getWritableDatabase();
//封装数据库中表的创建、更新、删除
DaoMaster mDaoMaster = new DaoMaster(mDb);  //合起来就是对数据库的操作
//对表操作的对象。
DaoSession mSession = mDaoMaster.newSession(); //可以认为是对数据的操作

4. Modify the fields of the entity object

 @Entity
 public class RecordBean {
    
    
    @Id
    private Long id;
    private String name;
    
    private String age; // 添加的字段

5. Execute compilation (key point) to allow the bean object to regenerate configuration information and Dao classes.

6. Modify the gradle database information under the app, the version number of the database in build.gradleschemaVersion, just increase it by 1, and finally run the app.

 greendao {
    
    
    // 数据库版本号,升级时及时修改
    schemaVersion 2
    daoPackage 'com.harry.greendaomaster.greendao.gen' // 设置DaoMaster、DaoSession、Dao包名
    targetGenDir 'src/main/java' // 设置DaoMaster、DaoSession、Dao目录
 }

at last

       ​​​​​​​​​​​​​​​​​​​​​​​​​​ ​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​ out of actual testing whether it is deleting a form, or modifying or deleting form fields, it is all effective. Moreover, old data will be retained and will not be lost.

Finally, this is the need in my personal project, and everyone also needs to actually develop it according to their personal needs. Welcome everyone, leave a message to discuss and learn together!

Project address:Android GreenDao database upgrade

Guess you like

Origin blog.csdn.net/chen_md/article/details/132452503