如何通过从资产中销毁和重新加载数据库来迁移 Room 数据库
How Migrate Room database by destroying and reloading database from assets
我正在为我的应用程序数据库使用 Android Room。我需要将版本从 1 更改为 2,正确的 .db 嵌入在我的应用程序文件夹中 assets/databases/
我可以使用 addMigrations()
指定迁移方法或使用 fallbackToDestructiveMigration()
fallbackToDestructiveMigration()
清空我的数据库,我不知道如何从我的文件夹 assets/databases/
中的数据库重新填充它。也许我可以在发生 fallbackToDestructiveMigration 时指定回调吗?
如果我添加迁移方法,预期与发现之间存在太多差异,而且我不知道如何将某些 COLUMN 设置为 "NOT NULL"。
Expected:
TableInfo{name='poi', columns={sound_path=Column{name='sound_path', type='TEXT',
affinity='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},
address=Column{name='address', type='TEXT', affinity='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=Column{name='opening_hour', type='TEXT', affinity='2',
notNull=false, primaryKeyPosition=0},
img_360_paths=Column{name='img_360_paths', type='TEXT', affinity='2',
notNull=false, primaryKeyPosition=0},
open_schedule_EN=Column{name='open_schedule_EN', type='TEXT',
affinity='2', notNull=false, primaryKeyPosition=0},
closed_days=Column{name='closed_days', type='TEXT', affinity='2',
notNull=false, primaryKeyPosition=0},
open_schedule_FR=Column{name='open_schedule_FR', type='TEXT',
affinity='2', notNull=false, primaryKeyPosition=0},
category_id=Column{name='category_id', type='INTEGER', affinity='3',
notNull=true, primaryKeyPosition=0},
img_paths=Column{name='img_paths', type='TEXT', affinity='2',
notNull=false, primaryKeyPosition=0},
closing_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=Column{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=[], indices=[]}
Found: 08-09 17:27:47.990 22583-22583/com.rendrsoftworks.vrlib E/AndroidRuntime: TableInfo{name='poi',
columns={sound_path=Column{name='sound_path', type='TEXT',
affinity='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},
address=Column{name='address', type='TEXT', affinity='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=Column{name='opening_hour', type='TEXT', affinity='2',
notNull=false, primaryKeyPosition=0},
img_360_paths=Column{name='img_360_paths', type='TEXT', affinity='2',
notNull=false, primaryKeyPosition=0},
open_schedule_EN=Column{name='open_schedule_EN', type='TEXT',
affinity='2', notNull=false, primaryKeyPosition=0},
closed_days=Column{name='closed_days', type='TEXT', affinity='2',
notNull=false, primaryKeyPosition=0},
open_schedule_FR=Column{name='open_schedule_FR', type='TEXT',
affinity='2', notNull=false, primaryKeyPosition=0},
category_id=Column{name='category_id', type='INTEGER', affinity='3',
notNull=false, primaryKeyPosition=0},
img_paths=Column{name='img_paths', type='TEXT', affinity='2',
notNull=false, primaryKeyPosition=0},
closing_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=Column{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=[], indices=[]}
我遇到过同样的问题。您不必 运行 这些方法 fallbackToDestructiveMigration()
或 addMigrations()
,至少在上述情况下不需要。只需使 poi
class 与 db table 相同并检查符号,例如,错误消息中显示的 poi
table 似乎有一个不同的字段在你的 poi
class "can_skip" notNull = true
而在你的数据库 table "can_skip" notNull = false
所以你所要做的就是删除注释 @NonNull
应该是这样的
@ColumnInfo(name = "can_skip")
// @NonNull remove this
private int mCanSkip;
。
之后,从模拟器或 phone 中卸载您的应用,然后重新构建,它将正常运行。
帮手:
RoomSQLiteDifferenceFinder:允许您确定 db table 与其 class.
之间的差异
DB Browser for SQLite: 让您轻松管理您的数据库。
我正在为我的应用程序数据库使用 Android Room。我需要将版本从 1 更改为 2,正确的 .db 嵌入在我的应用程序文件夹中 assets/databases/
我可以使用 addMigrations()
指定迁移方法或使用 fallbackToDestructiveMigration()
fallbackToDestructiveMigration()
清空我的数据库,我不知道如何从我的文件夹 assets/databases/
中的数据库重新填充它。也许我可以在发生 fallbackToDestructiveMigration 时指定回调吗?
如果我添加迁移方法,预期与发现之间存在太多差异,而且我不知道如何将某些 COLUMN 设置为 "NOT NULL"。
Expected: TableInfo{name='poi', columns={sound_path=Column{name='sound_path', type='TEXT', affinity='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}, address=Column{name='address', type='TEXT', affinity='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=Column{name='opening_hour', type='TEXT', affinity='2', notNull=false, primaryKeyPosition=0}, img_360_paths=Column{name='img_360_paths', type='TEXT', affinity='2', notNull=false, primaryKeyPosition=0}, open_schedule_EN=Column{name='open_schedule_EN', type='TEXT', affinity='2', notNull=false, primaryKeyPosition=0}, closed_days=Column{name='closed_days', type='TEXT', affinity='2', notNull=false, primaryKeyPosition=0}, open_schedule_FR=Column{name='open_schedule_FR', type='TEXT', affinity='2', notNull=false, primaryKeyPosition=0}, category_id=Column{name='category_id', type='INTEGER', affinity='3', notNull=true, primaryKeyPosition=0}, img_paths=Column{name='img_paths', type='TEXT', affinity='2', notNull=false, primaryKeyPosition=0}, closing_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=Column{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=[], indices=[]} Found: 08-09 17:27:47.990 22583-22583/com.rendrsoftworks.vrlib E/AndroidRuntime: TableInfo{name='poi', columns={sound_path=Column{name='sound_path', type='TEXT', affinity='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}, address=Column{name='address', type='TEXT', affinity='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=Column{name='opening_hour', type='TEXT', affinity='2', notNull=false, primaryKeyPosition=0}, img_360_paths=Column{name='img_360_paths', type='TEXT', affinity='2', notNull=false, primaryKeyPosition=0}, open_schedule_EN=Column{name='open_schedule_EN', type='TEXT', affinity='2', notNull=false, primaryKeyPosition=0}, closed_days=Column{name='closed_days', type='TEXT', affinity='2', notNull=false, primaryKeyPosition=0}, open_schedule_FR=Column{name='open_schedule_FR', type='TEXT', affinity='2', notNull=false, primaryKeyPosition=0}, category_id=Column{name='category_id', type='INTEGER', affinity='3', notNull=false, primaryKeyPosition=0}, img_paths=Column{name='img_paths', type='TEXT', affinity='2', notNull=false, primaryKeyPosition=0}, closing_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=Column{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=[], indices=[]}
我遇到过同样的问题。您不必 运行 这些方法 fallbackToDestructiveMigration()
或 addMigrations()
,至少在上述情况下不需要。只需使 poi
class 与 db table 相同并检查符号,例如,错误消息中显示的 poi
table 似乎有一个不同的字段在你的 poi
class "can_skip" notNull = true
而在你的数据库 table "can_skip" notNull = false
所以你所要做的就是删除注释 @NonNull
应该是这样的
@ColumnInfo(name = "can_skip")
// @NonNull remove this
private int mCanSkip;
。
之后,从模拟器或 phone 中卸载您的应用,然后重新构建,它将正常运行。
帮手:
RoomSQLiteDifferenceFinder:允许您确定 db table 与其 class.
之间的差异DB Browser for SQLite: 让您轻松管理您的数据库。