如何通过从资源中销毁和重新加载数据库来迁移房间数据库



我正在使用Android Room作为我的应用程序数据库。我需要将版本从1更改为2,正确的.db嵌入到我的应用程序文件夹assets/databases/

我可以用addMigrations()指定迁移方法,也可以使用fallbackToDestructiveMigration()

fallbackToDestructiveMigration()清空了我的数据库,我不知道如何从文件夹assets/databases/中的数据库再次填充它。也许我可以指定一个回调时发生故障到破坏性迁移?

如果我添加一个迁移方法,那么预期的&找到了,加上我不知道如何将一些COLUMN设置为"NOT NULL"。

预期:TableInfo{name='poi',columns={sound_path=Column{name='sound_path',type='TEXT',affinity="2",notNull=false,primaryKeyPosition=0},name_FR=列{name='name_FR',type='TEXT',affinity='2',notNull=false,primaryKeyPosition=0},text_FR=列{name=‘text_FR’,type='TEXT',affinity='2',notNull=false,primaryKeyPosition=0},address=列{name='address',type='TEXT',affinity='2',notNull=false,primaryKeyPosition=0},city=Column{name='city',type='NTEGER',affinity='3',notNull=true,primaryKeyPosition=0},text_EN=列{name='text_EN',type='text',affinity='2',notNull=false,primaryKeyPosition=0},video_path=列{name='video_path',type='TEXT',affinity='2',notNull=false,primaryKeyPosition=0},opening_hour=列{name='opening_hour',type='TEXT',affinity='2',notNull=false,primaryKeyPosition=0},img_360_paths=列{name='img_360-paths',type='TEXT',affinity='2',notNull=false,primaryKeyPosition=0},open_schedule_EN=列{name='open_schedule_EN',type='TEXT',affinity="2",notNull=false,primaryKeyPosition=0},closed_days=列{name='closed_days',type='TEXT',affinity='2',notNull=false,primaryKeyPosition=0},open_schedule_FR=列{name='open_schedule_FR',type='TEXT',affinity="2",notNull=false,primaryKeyPosition=0},category_id=列{name='category_id',type='NTEGER',affinity='3',notNull=true,primaryKeyPosition=0},img_paths=列{name='img_paths',type='TEXT',affinity='2',notNull=false,primaryKeyPosition=0},closing_hour=列{name='closing_hur',type='TEXT',affinity='2',notNull=false,primaryKeyPosition=0},price=Column{name='price',type='NTEGER',affinity='3',notNull=true,primaryKeyPosition=0},game_path=列{name='game_path',type='TEXT',affinity='2',notNull=false,primaryKeyPosition=0},can_skip=列{name='can_skip',type='NTEGER',affinity='3',notNull=true,primaryKeyPosition=0},id=列{name=‘id’,type=‘NTEGER’,affinity=‘3’,notNull=true,primaryKeyPosition=1},closed_months=列{name='closed_month',type='TEXT',affinity='2',notNull=false,primaryKeyPosition=0},vr_path=列{name='vr_path',type='TEXT',affinity='2',notNull=false,primaryKeyPosition=0},name_EN=列{name=‘name_EN’,type='TEXT',affinity='2',notNull=false,primaryKeyPosition=0}},foreignKeys=[],indexs=[]}发现时间:08-09 17:27:47.990 22583-22583/com.rendersoftworks.vrlib E/AndroidRuntime:TableInfo{name='poi',columns={sound_path=Column{name='sound_path',type='TEXT',affinity="2",notNull=false,primaryKeyPosition=0},name_FR=列{name='name_FR',type='TEXT',affinity='2',notNull=false,primaryKeyPosition=0},text_FR=列{name=‘text_FR’,type='TEXT',affinity='2',notNull=false,primaryKeyPosition=0},address=列{name='address',type='TEXT',affinity='2',notNull=false,primaryKeyPosition=0},city=Column{name='city',type='NTEGER',affinity='3',notNull=false,primaryKeyPosition=0},text_EN=列{name='text_EN',type='text',affinity='2',notNull=false,primaryKeyPosition=0},video_path=列{name='video_path',type='TEXT',affinity='2',notNull=false,primaryKeyPosition=0},opening_hour=列{name='opening_hour',type='TEXT',affinity='2',notNull=false,primaryKeyPosition=0},img_360_paths=列{name='img_360-paths',type='TEXT',affinity='2',notNull=false,primaryKeyPosition=0},open_schedule_EN=列{name='open_schedule_EN',type='TEXT',affinity="2",notNull=false,primaryKeyPosition=0},closed_days=列{name='closed_days',type='TEXT',affinity='2',notNull=false,primaryKeyPosition=0},open_schedule_FR=列{name='open_schedule_FR',type='TEXT',affinity="2",notNull=false,primaryKeyPosition=0},category_id=列{name='category_id',type='NTEGER',affinity='3',notNull=false,primaryKeyPosition=0},img_paths=列{name='img_paths',type='TEXT',affinity='2',notNull=false,primaryKeyPosition=0},closing_hour=列{name='closing_hur',type='TEXT',affinity='2',notNull=false,primaryKeyPosition=0},price=Column{name='price',type='NTEGER',affinity='3',notNull=false,primaryKeyPosition=0},game_path=列{name='game_path',type='TEXT',affinity='2',notNull=false,primaryKeyPosition=0},can_skip=列{name='can_skip',type='NTEGER',affinity='3',notNull=false,primaryKeyPosition=0},id=列{name=‘id’,type=‘NTEGER’,affinity=‘3’,notNull=false,primaryKeyPosition=1},closed_months=列{name='closed_month',type='TEXT',affinity='2',notNull=false,primaryKeyPosition=0},vr_path=列{name='vr_path',type='TEXT',affinity='2',notNull=false,primaryKeyPosition=0},name_EN=列{name=‘name_EN’,type='TEXT',affinity='2',notNull=false,primaryKeyPosition=0}},foreignKeys=[],indexs=[]}

我也遇到过同样的问题。您不必运行这些方法fallbackToDestructiveMigration()addMigrations(),至少在上述情况下不必运行。只需使poi类与数据库表相同并检查注释即可,例如,错误消息中显示的poi表似乎有一个不同的字段,该字段位于poi"can_skip" notNull = true中,而在数据库表"can_skip" notNull = false中,因此您所要做的就是删除注释@NonNull,它应该像这个

@ColumnInfo(name = "can_skip") // @NonNull remove this private int mCanSkip;

之后,从模拟器或手机中卸载你的应用程序,然后重建,它就会正常工作。

帮助者:

RoomSQLiteDifferenceFinder:允许您确定数据库表及其类之间的差异。

SQLite数据库浏览器:允许您轻松管理数据库。

最新更新