如何通过从资产中销毁数据库并重新加载数据库来迁移Room数据库

时间:2018-08-09 15:57:03

标签: android android-sqlite database-migration android-room

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

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

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

如果添加迁移方法,则期望与发现之间存在太多差异,而且我不知道如何将某些COLUMN设置为“ NOT NULL”。

  

预期:       TableInfo {name ='poi',columns = {sound_path = Column {name ='sound_path',type ='TEXT',   finity ='2',notNull = false,primaryKeyPosition = 0},   name_FR = Column {name ='name_FR',type ='TEXT',affinity ='2',   notNull = false,primaryKeyPosition = 0},text_FR = Column {name ='text_FR',   type ='TEXT',affinity ='2',notNull = false,primaryKeyPosition = 0},   地址=列{名称='地址',类型='文本',亲和力='2',   notNull = false,primaryKeyPosition = 0},city = Column {name ='city',   type ='INTEGER',affinity ='3',notNull = true,primaryKeyPosition = 0},   text_EN = Column {name ='text_EN',type ='TEXT',affinity ='2',   notNull = false,primaryKeyPosition = 0},   video_path = Column {name ='video_path',type ='TEXT',affinity ='2',   notNull = false,primaryKeyPosition = 0},   opening_hour =栏{名称='opening_hour',类型='文字',相似度='2',   notNull = false,primaryKeyPosition = 0},   img_360_paths =列{名称='img_360_paths',类型='TEXT',亲和力='2',   notNull = false,primaryKeyPosition = 0},   open_schedule_EN =列{name ='open_schedule_EN',类型='TEXT',   finity ='2',notNull = false,primaryKeyPosition = 0},   close_days = Column {name ='closed_days',type ='TEXT',affinity ='2',   notNull = false,primaryKeyPosition = 0},   open_schedule_FR =列{name ='open_schedule_FR',type ='TEXT',   finity ='2',notNull = false,primaryKeyPosition = 0},   category_id =列{name ='category_id',type ='INTEGER',亲和力='3',   notNull = true,primaryKeyPosition = 0},   img_paths =列{名称='img_paths',类型='TEXT',亲和力='2',   notNull = false,primaryKeyPosition = 0},   close_hour = Column {name ='closing_hour',type ='TEXT',affinity ='2',   notNull = false,primaryKeyPosition = 0},price = Column {name ='price',   type ='INTEGER',affinity ='3',notNull = true,primaryKeyPosition = 0},   game_path = Column {name ='game_path',type ='TEXT',affinity ='2',   notNull = false,primaryKeyPosition = 0},can_skip = Column {name ='can_skip',   type ='INTEGER',affinity ='3',notNull = true,primaryKeyPosition = 0},   id = Column {name ='id',type ='INTEGER',affinity ='3',notNull = true,   primaryKeyPosition = 1},closed_months =列{name =“ closed_months”,   type ='TEXT',affinity ='2',notNull = false,primaryKeyPosition = 0},   vr_path = Column {name ='vr_path',type ='TEXT',affinity ='2',   notNull = false,primaryKeyPosition = 0},name_EN = Column {name ='name_EN',   type ='TEXT',affinity ='2',notNull = false,primaryKeyPosition = 0}},   foreignKeys = [],索引= []}        找到:08-09 17:27:47.990 22583-22583 / com.rendrsoftworks.vrlib E / AndroidRuntime:TableInfo {name ='poi',   column = {sound_path = Column {name ='sound_path',type ='TEXT',   finity ='2',notNull = false,primaryKeyPosition = 0},   name_FR = Column {name ='name_FR',type ='TEXT',affinity ='2',   notNull = false,primaryKeyPosition = 0},text_FR = Column {name ='text_FR',   type ='TEXT',affinity ='2',notNull = false,primaryKeyPosition = 0},   地址=列{名称='地址',类型='文本',亲和力='2',   notNull = false,primaryKeyPosition = 0},city = Column {name ='city',   type ='INTEGER',affinity ='3',notNull = false,primaryKeyPosition = 0},   text_EN = Column {name ='text_EN',type ='TEXT',affinity ='2',   notNull = false,primaryKeyPosition = 0},   video_path = Column {name ='video_path',type ='TEXT',affinity ='2',   notNull = false,primaryKeyPosition = 0},   opening_hour =栏{名称='opening_hour',类型='文字',相似度='2',   notNull = false,primaryKeyPosition = 0},   img_360_paths =列{名称='img_360_paths',类型='TEXT',亲和力='2',   notNull = false,primaryKeyPosition = 0},   open_schedule_EN =列{name ='open_schedule_EN',类型='TEXT',   finity ='2',notNull = false,primaryKeyPosition = 0},   close_days = Column {name ='closed_days',type ='TEXT',affinity ='2',   notNull = false,primaryKeyPosition = 0},   open_schedule_FR =列{name ='open_schedule_FR',type ='TEXT',   finity ='2',notNull = false,primaryKeyPosition = 0},   category_id =列{name ='category_id',type ='INTEGER',亲和力='3',   notNull = false,primaryKeyPosition = 0},   img_paths =列{名称='img_paths',类型='TEXT',亲和力='2',   notNull = false,primaryKeyPosition = 0},   close_hour = Column {name ='closing_hour',type ='TEXT',affinity ='2',   notNull = false,primaryKeyPosition = 0},price = Column {name ='price',   type ='INTEGER',affinity ='3',notNull = false,primaryKeyPosition = 0},   game_path = Column {name ='game_path',type ='TEXT',affinity ='2',   notNull = false,primaryKeyPosition = 0},can_skip = Column {name ='can_skip',   type ='INTEGER',affinity ='3',notNull = false,primaryKeyPosition = 0},   id = Column {name ='id',type ='INTEGER',affinity ='3',notNull = false,   primaryKeyPosition = 1},closed_months =列{name =“ closed_months”,   type ='TEXT',affinity ='2',notNull = false,primaryKeyPosition = 0},   vr_path = Column {name ='vr_path',type ='TEXT',affinity ='2',   notNull = false,primaryKeyPosition = 0},name_EN = Column {name ='name_EN',   type ='TEXT',affinity ='2',notNull = false,primaryKeyPosition = 0}},   foreignKeys = [],索引= []}

1 个答案:

答案 0 :(得分:1)

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

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

然后,从仿真器或手机中卸载您的应用,然后进行重建,即可正常运行。

助手:

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

DB Browser for SQLite:可让您轻松管理数据库。