简体   繁体   English

android 机房重装app迁移未运行

[英]android room the reinstall app migration is not running

when I created a migration, I inserted some data, but the newly installed app did not perform the migration.创建迁移时,我插入了一些数据,但新安装的应用程序没有执行迁移。
do I need to insert/update data on the onCreate call every time I migrate?每次迁移时是否需要在 onCreate 调用中插入/更新数据?

Migrations won't run if you install the app, as no Database exists so it is created.如果您安装该应用程序,迁移将不会运行,因为不存在数据库,因此创建了它。

Migrations are designed to handle new versions of the App being published and specifically new versions that also change the Room database version.迁移旨在处理正在发布的应用程序的新版本,特别是也会更改 Room 数据库版本的新版本。

do I need to insert/update data on the onCreate call every time I migrate?每次迁移时是否需要在 onCreate 调用中插入/更新数据?

NO!不! onCreate will not run, it only runs once automatically for the lifetime of the database (unless you call it from the Migration). onCreate 不会运行,它只会在数据库的生命周期内自动运行一次(除非您从迁移中调用它)。

  • For an App to be installed, it cannot exist and thus must be uninstalled, the uninstall deletes all the App's data including the databases.对于要安装的应用程序,它不能存在,因此必须卸载,卸载会删除所有应用程序的数据,包括数据库。

You should be doing what is required in the Mirgation code and then it depends on what you do.您应该执行迁移代码中所需的操作,然后取决于您执行的操作。 eg :-例如:-

If adding a column for instance and you use the ALTER TABLE your_table ADD COLUMN the_new_column_definition then the existing data is preserved.例如,如果添加一列并且您使用 ALTER TABLE your_table ADD COLUMN the_new_column_definition,则保留现有数据。

If adding a new table then there is no issue.如果添加新表,则没有问题。

If however you wanted to change a column's definition (other than it's name) then you have to drop and recreate the table.但是,如果您想更改列的定义(而不是它的名称),那么您必须删除并重新创建表。 What would typically be done would be to create the new table, populate it from the original table, rename or drop the original table and the rename the new table to be the name of the original table.通常要做的是创建新表,从原始表填充它,重命名或删除原始表,并将新表重命名为原始表的名称。

You may wish to have a look at SQLite ALTER TABLE您不妨看看SQLite ALTER TABLE

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM