Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/203.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sqlite/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Android Room持久性库中创建表而不迁移版本?_Android_Sqlite_Android Sqlite_Android Room - Fatal编程技术网

如何在Android Room持久性库中创建表而不迁移版本?

如何在Android Room持久性库中创建表而不迁移版本?,android,sqlite,android-sqlite,android-room,Android,Sqlite,Android Sqlite,Android Room,当我只创建一个表而不更改其他表和数据时,是否需要迁移数据库 正如和谷歌所说,大多数人说我们必须迁移数据库,如果每次升级我都要创建表,我必须增加数据库版本和迁移,这非常令人困惑。Room正在开始时比较数据库架构,如果架构不同,它将运行迁移。若模式不同,并且不会添加迁移,则可能会抛出IllegalStateException 更重要的是,当您升级了db版本并且在模式中没有任何更改时,您应该将迁移对象添加到构建器中。如果没有,房间将清除数据库并重新创建它们。您可以在classRoomDatabase.

当我只创建一个表而不更改其他表和数据时,是否需要迁移数据库


正如和谷歌所说,大多数人说我们必须迁移数据库,如果每次升级我都要创建表,我必须增加数据库版本和迁移,这非常令人困惑。Room正在开始时比较数据库架构,如果架构不同,它将运行迁移。若模式不同,并且不会添加迁移,则可能会抛出IllegalStateException

更重要的是,当您升级了db版本并且在模式中没有任何更改时,您应该将迁移对象添加到构建器中。如果没有,房间将清除数据库并重新创建它们。您可以在class
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;
        }
/**
*将移植添加到生成器中。
*
*每个迁移都有一个开始和结束版本,Room运行这些迁移以带来
*数据库更新至最新版本。
*
*如果当前版本和最新版本之间缺少迁移项,请单击“文件室”
*将清除数据库并重新创建,因此即使两个版本之间没有更改,
*您仍然应该向构建器提供迁移对象。
*
*迁移可以处理一个以上的版本(例如,如果您有一个更快的路径可供选择)
*转到版本3到版本5,而不转到版本4)。如果文件室在版本中打开数据库
*3且最新版本为=5,房间将使用可从中迁移的迁移对象
*3比5,而不是3比4和4比5。
*
*@param将可以修改数据库的迁移对象迁移到必要的位置
*变化。
*@还这个
*/
@非空
公共生成器添加迁移(迁移…迁移){
mMigrationContainer.addMigrations(迁移);
归还这个;
}
/**
*允许在{@link Migration}需要时以破坏性方式重新创建数据库表
*找不到将旧数据库架构迁移到最新架构版本。
*
*当设备上的数据库版本与最新架构版本不匹配时,请单击Room
*在数据库上运行必要的{@link Migration}。
*
*如果找不到将数据库带到服务器的{@link Migration}集
*在当前版本中,它将抛出一个{@link IllegalStateException}。
*
*您可以调用此方法来更改此行为以重新创建数据库,而不是
*撞车。
*
*请注意,这将删除由文件室管理的数据库表中的所有数据。
*
*@还这个
*/
@非空
公共生成器回退到破坏性迁移(){
mrequiremiglation=false;
归还这个;
}