1.先用GreenDao工具类编写自动创建代码,按照升级后的最新数据库结构来编写
2.GreenDao工具自动生成的代码覆盖到项目里去
3.在项目里找到对应的自动生成的数据库DaoMaster类
在DaoMaster类里有个内部类DevOpenHelper里的方法onUpgrade里写上数据库结构变化的代码,比如某个表新增字段,并要把里面dropAllTables和onCreate方法删掉
/** WARNING: Drops all table on Upgrade! Use only during development. */ public static class DevOpenHelper extends OpenHelper { public DevOpenHelper(Context context, String name, CursorFactory factory,String dbPath) { super(context, name, factory,dbPath); } @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); } }
4.还有记得把DaoMaster类里的成员变量SCHEMA_VERSION值修改为升级后的数据库版本