How to drop and recreate all tables on DB version update in ActiveAndroid?

*爱你&永不变心* 提交于 2019-12-10 11:25:49

问题


I'm using ActiveAndroid in my application as the Database system. At current stage I don't want to write the migration files for migrating current tables to altered ones for when I update DB version.

Instead what I want to do is to drop all tables and recreate a new set of tables.

Unfortunately from digging in the ActiveAndroid the Database version test happens at initialization stage in which I pass a ActiveAndroid Configuration object to configure the Database.

At this point their implementation only checks for migration files in the assets folder. I don't mind to create migrations file to do what I need but the problem is that I don't know how to get the current tables initialized in the DB to recreate them.

Does some one knows how to do that with ActiveAndroid?


回答1:


Not sure if I understood correctly but if you are in development then the easiest is to just uninstall the app and let Active Android recreate the DB schema when you install it again.

When in production you will need to use the migration scripts as per their documentation.

I faced a similar issue and I needed to force a few tables to be recreated. Unfortunately you will need to do it manually. However you can use the ActiveAndroid helper classes to help you out.

  1. First you need to process the old table data somehow.
  2. Drop the tables:

    SQLiteUtils.execSql("DROP TABLE IF EXISTS Your_Table_Name");

  3. Recreate the table using the ActiveAndroid classes.

    SQLiteUtils.execSql(SQLiteUtils.createTableDefinition(Cache.getTableInfo(YOUR_MODEL_CLASS.class)));

  4. Insert the old data into your newwly create table.



来源:https://stackoverflow.com/questions/34495753/how-to-drop-and-recreate-all-tables-on-db-version-update-in-activeandroid

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!