greenDao模式升级 [英] greenDao Schema Upgrade

查看:467
本文介绍了greenDao模式升级的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,感谢你真棒工具!

First of all, thanks for your awesome tool!

我看到有关架构升级/迁移使用绿道另一个问题(<一href="http://stackoverflow.com/questions/13373170/greendao-schema-update-and-data-migration">here)

I have seen another question about schema upgrade/migration using green dao (here)

有很多在答案的良好格局用做模式升级时,链接 - 但有什么你实际上做你的数据正确迁移是没有例子,我无法找到任何东西。

There are lots of links in that answer for a good pattern to use when doing schema upgrades - however there are no examples of what you actually do to your data to migrate it properly and I'm having trouble finding anything.

在我的情况,我的移民是非常直截了当 - 我不想改变任何现有的数据,我只是需要一些新的表添加到我的架构,我怀疑是一个相当普遍的情况。

In my case, my migration is incredibly straight forward - I do not wish to transform any existing data, I simply need to add some new tables to my schema, which I suspect is a fairly common situation.

什么是添加新表到您的模式,但不删除你的用户已经保存数据最简单的方法?一个具体的例子是大大AP preciated。

这将是真棒,如果greenDao提供一类类似DevOpenHelper,将只需添加pviously存在的模式新表/列没有$ P $没有先下探现有脊髓痨/数据。

It would be awesome if greenDao provided a class similar to DevOpenHelper that would simply add new tables/columns that didn't previously exist in the schema without dropping existing tabes/data first.

推荐答案

我终于有时间深入研究一下这个自己,并意识到它很容易添加一个新表,同时保留在旧表中的数据。

I finally had time to dig in to this myself and realized it's quite easy to add a new table while retaining data in old tables.

免责声明:当我意识到这一点的实现是针对我的方案,我认为这是对于像我这样谁使用了一个Android ORM工具(greenDao)有用的专门处理的SQLite在Android上。我理解常见的那些谁从一开始就写你自己创建表的查询,这是pretty的,但对于某人谁一直庇护从使用的SQLite数据库与Android的胆量,我觉得这个例子会有所帮助

DISCLAIMER: While I realize this implementation is specific to my scenario, I think it's helpful for someone like me who has used an Android ORM tool (greenDao) exclusively to deal with SQLite on Android. I understand this is pretty common for those of you who have written your own table creation queries from the beginning, but for someone who has been sheltered from the guts of using a SQLite DB with Android, I think this example will be helpful.

答: 你可以修改DevOpenHelper内部类或创建自己的类。我选择了编辑DevOpenHelper暂时保持我的例子简单 - 但是,请注意,如果你重新生成您的greendao类,DevOpenHelper将被覆盖。这将是一个更好的主意来创建自己的类,如MyOpenHelper并使用它。

ANSWER: You can either modify the DevOpenHelper inner class or create your own class. I chose to edit DevOpenHelper for the time being to keep my example simple - however, note that if you regenerate your greendao classes, DevOpenHelper will be overwritten. It would be a better idea to create your own class like "MyOpenHelper" and use that instead.

在我的变化,DevOpenHelper.onUpgrade是这样的:

Before my changes, DevOpenHelper.onUpgrade looked like this:

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) 
{
        Log.i("greenDAO", "Upgrading schema from version " + oldVersion + " to " + newVersion + " by dropping all tables");
        dropAllTables(db, true);
        onCreate(db);
}

相反删除所有的表,看看该createAllTables方法是自动生成的GreenDao。

Instead of dropping all tables, take a look at the createAllTables method that is auto-generated by GreenDao.

重写onUpgrade检查,如果oldVersion是你想从升级的,那么只有调用CREATETABLE方法新表。这里是我的onUpgrade方法看起来像现在:

Rewrite onUpgrade to check if the "oldVersion" is the one you want to upgrade from, then only call the createTable methods for "new" tables. Here is what my onUpgrade method looks like now:

public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) 
{
        Log.i("greenDAO", "Upgrading schema from version " + oldVersion + " to " + 

        //Going from older schema to new schema
        if(oldVersion == 3 && newVersion == 4)
        {
            boolean ifNotExists = false;

            //Leave old tables alone and only create ones that didn't exist
            //in the previous schema
            NewTable1Dao.createTable(db, ifNotExists);
            NewTable2Dao.createTable(db, ifNotExists);
            NewTable3Dao.createTable(db, ifNotExists);
            NewTable4Dao.createTable(db, ifNotExists);
        }
        else
        {
            dropAllTables(db, true);
            onCreate(db);
        }
}

添加一个新的列有异曲同工之处,但你必须写一些SQL或看看自动生成的SQL创建greenDao声明,并充分利用它们。

Adding a new column would be similar, except you'd have to write some SQL or take a look at the auto-generated SQL create statements from greenDao and leverage those.

要添加一个新列(NEW_COLUMN,假设它是一个整数类型),以现有的表(​​EXISTING_TABLE),请执行以下操作:

To add a single new column (NEW_COLUMN, assuming it's an INTEGER type) to an existing table (EXISTING_TABLE), do the following:

db.execSQL("ALTER TABLE 'EXISTING_TABLE' ADD 'NEW_COLUMN' INTEGER");

对于我来说,现在,所有我需要做的是增加新的表,所以这结束了,而直线前进。希望别人认为这是有用的。

For me right now, all I needed to do was add new Tables so this ended up being rather straight forward. Hopefully someone else finds this useful.

这篇关于greenDao模式升级的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆