android - 如何在Android Room持久库中创建不迁移版本的表?

标签 android sqlite android-sqlite android-room

我只创建一个表,不改变其他表和数据,是否需要迁移数据库?

作为google tutorial google了一下,大部分都说必须迁移数据库,如果我每次升级都建表,就得增加数据库版本和迁移,很困惑

最佳答案

是的,确实如此。 Room 在开始时会比较数据库架构,如果架构不同,它将运行迁移。如果架构不同并且不会添加迁移,则可能会抛出 IllegalStateException。

更重要的是,当您提高数据库版本并且模式没有更改时,您应该将迁移对象添加到构建器。如果没有,Room 将清除您的数据库并重新创建它们。您可以在 RoomDatabase.java 类的文档中阅读更多内容:

  /**
         * Adds a migration to the builder.
         * <p>
         * Each Migration has a start and end versions and Room runs these migrations to bring the
         * database to the latest version.
         * <p>
         * If a migration item is missing between current version and the latest version, Room
         * will clear the database and recreate so even if you have no changes between 2 versions,
         * you should still provide a Migration object to the builder.
         * <p>
         * A migration can handle more than 1 version (e.g. if you have a faster path to choose when
         * going version 3 to 5 without going to version 4). If Room opens a database at version
         * 3 and latest version is &gt;= 5, Room will use the migration object that can migrate from
         * 3 to 5 instead of 3 to 4 and 4 to 5.
         *
         * @param migrations The migration object that can modify the database and to the necessary
         *                   changes.
         * @return this
         */
        @NonNull
        public Builder<T> addMigrations(Migration... migrations) {
            mMigrationContainer.addMigrations(migrations);
            return this;
        }

 /**
         * Allows Room to destructively recreate database tables if {@link Migration}s that would
         * migrate old database schemas to the latest schema version are not found.
         * <p>
         * When the database version on the device does not match the latest schema version, Room
         * runs necessary {@link Migration}s on the database.
         * <p>
         * If it cannot find the set of {@link Migration}s that will bring the database to the
         * current version, it will throw an {@link IllegalStateException}.
         * <p>
         * You can call this method to change this behavior to re-create the database instead of
         * crashing.
         * <p>
         * Note that this will delete all of the data in the database tables managed by Room.
         *
         * @return this
         */
        @NonNull
        public Builder<T> fallbackToDestructiveMigration() {
            mRequireMigration = false;
            return this;
        }

关于android - 如何在Android Room持久库中创建不迁移版本的表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51258454/

相关文章:

java - ClassNotFoundException,即使包含的 JAR 是构建的一部分

android - 在 Xamarin Android 中打开文件

android - ParseObject 保存在本地。错误 : Unable to encode an unsaved parse file

android - 如何在 sqlite 中存储跨区字符串?

sql - 执行查询 sql 时出错 - Golang

java - 将输入字符串与数据库中的字符串进行比较

java - 在 SQlite 数据库中添加新行然后在 ListView 中添加新行时,所有列表行在自定义 ListView 中重复

java - 获取两种颜色之间的中值颜色

android - 使用 SharedPreferences 将值插入 SQLite 时出错

java - 为什么我的光标计数为0?