美文网首页
greendao升级(二)

greendao升级(二)

作者: 重新起步 | 来源:发表于2021-08-09 10:12 被阅读0次

    greendao升级(一)
    greendao升级(二)
    greendao升级(三)
    greendao升级(四)

    greendao升级版本导致数据丢失

    数据库中存在数据:

    image

    此时修改greendao的配置:

    
    greendao{
    
        schemaVersion 10
    
        daoPackage 'com.ford.sync.fordcalendar.greendao'
    
        targetGenDir 'src/main/java/'
    
        generateTestsfalse
    
    }
    
    

    schemaVersion 的值从10改到11;

    @Entity
    public class ABean {
    
        private int first;
    
        private int second;
    
    
        @Convert(converter = ListBBeanConvert.class, columnType = String.class)
        private List<BBean> bBeans;
    
    

    给ABean添加属性second;

    重新运行代码,

    查看log如下:


    在查看数据库


    1.运行代码的时候,没有报错
    2.运行后的数据库,多了SECOND属性
    3.数据库数据丢失了

    我们看到我们保存的数据丢失了,这个很不好,比如我们开发一个日历软件,用户存储的提醒,因为我们的app升级,就导致了数据丢失了,这样用户只能重新设置提醒,但是用户设置提醒,就是希望你提醒他的,结果你app都记不得,那客户不是得爆炸;

    那么具体原因是啥呢,为啥数据会丢失呢?
    查看greendao的默认升级代码:

     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);
            }
        }
    

    可以看到,greendao默认的处理方式就是先删除表,在新建表, 那么我们想要保留数据,应该如何做呢?
    参考:greendao升级(三)

    相关文章

      网友评论

          本文标题:greendao升级(二)

          本文链接:https://www.haomeiwen.com/subject/ukxzvltx.html