繁体   English   中英

迁移房间数据库,alter table,android?

[英]Migration room database, alter table, android?

在我的应用程序中,我使用 ROOM db 来保存一些数据。

我在版本 1 中有一些列的表 UserInfo。

后来我在数据库中添加了一个整数列,我升级了数据库版本,添加了迁移代码,但我收到了以下异常

Migration didn't properly handle UserInfo(ima.rvtech.model.api.result.UserInfo).
 Expected:
TableInfo{name='UserInfo', columns={
address=Column{name='address', type='TEXT', notNull=false, primaryKeyPosition=0}, 
password=Column{name='password', type='TEXT', notNull=false, primaryKeyPosition=0}, 
actBy=Column{name='actBy', type='TEXT', notNull=false, primaryKeyPosition=0}, 
emailId=Column{name='emailId', type='TEXT', notNull=false, primaryKeyPosition=0}, 
userType=Column{name='userType', type='TEXT', notNull=false, primaryKeyPosition=0}, 
pinCode=Column{name='pinCode', type='TEXT', notNull=false, primaryKeyPosition=0}, 
uploadImagePath=Column{name='uploadImagePath', type='TEXT', notNull=false, primaryKeyPosition=0}, 
loginId=Column{name='loginId', type='TEXT', notNull=false, primaryKeyPosition=0}, 
actDate=Column{name='actDate', type='TEXT', notNull=false, primaryKeyPosition=0}, 
contactNo=Column{name='contactNo', type='TEXT', notNull=false, primaryKeyPosition=0}, 
uploadVideoPath=Column{name='uploadVideoPath', type='TEXT', notNull=false, primaryKeyPosition=0}, 
edbNo=Column{name='edbNo', type='TEXT', notNull=false, primaryKeyPosition=0}, 
id=Column{name='id', type='INTEGER', notNull=true, primaryKeyPosition=1}, 
emergencyContactNo=Column{name='emergencyContactNo', type='TEXT', notNull=false, primaryKeyPosition=0}, 
bannerImagePath=Column{name='bannerImagePath', type='TEXT', notNull=false, primaryKeyPosition=0}, 
MyFriendListCount=Column{name='MyFriendListCount', type='INTEGER', notNull=true, primaryKeyPosition=0}, 
userName=Column{name='userName', type='TEXT', notNull=false, primaryKeyPosition=0}, 
operationType=Column{name='operationType', type='TEXT', notNull=false, primaryKeyPosition=0}}, foreignKeys=[], indices=[]}
 Found:
TableInfo{name='UserInfo', columns={
address=Column{name='address', type='TEXT', notNull=false, primaryKeyPosition=0},
 password=Column{name='password', type='TEXT', notNull=false, primaryKeyPosition=0}, 
actBy=Column{name='actBy', type='TEXT', notNull=false, primaryKeyPosition=0}, 
emailId=Column{name='emailId', type='TEXT', notNull=false, primaryKeyPosition=0}, 
userType=Column{name='userType', type='TEXT', notNull=false, primaryKeyPosition=0}, 
pinCode=Column{name='pinCode', type='TEXT', notNull=false, primaryKeyPosition=0},
 uploadImagePath=Column{name='uploadImagePath', type='TEXT', notNull=false, primaryKeyPosition=0}, 
loginId=Column{name='loginId', type='TEXT', notNull=false, primaryKeyPosition=0}, 
actDate=Column{name='actDate', type='TEXT', notNull=false, primaryKeyPosition=0}, 
contactNo=Column{name='contactNo', type='TEXT', notNull=false, primaryKeyPosition=0}, 
uploadVideoPath=Column{name='uploadVideoPath', type='TEXT', notNull=false, primaryKeyPosition=0}, 
edbNo=Column{name='edbNo', type='TEXT', notNull=false, primaryKeyPosition=0}, 
id=Column{name='id', type='INTEGER', notNull=true, primaryKeyPosition=1}, 
emergencyContactNo=Column{name='emergencyContactNo', type='TEXT', notNull=false, primaryKeyPosition=0}, 
bannerImagePath=Column{name='bannerImagePath', type='TEXT', notNull=false, primaryKeyPosition=0}, 
MyFriendListCount=Column{name='MyFriendListCount', type='INTEGER', notNull=false, primaryKeyPosition=0}, 
userName=Column{name='userName', type='TEXT', notNull=false, primaryKeyPosition=0}, 
operationType=Column{name='operationType', type='TEXT', notNull=false, primaryKeyPosition=0}}, foreignKeys=[], indices=[]}

我在下一版本MyFriendListCount添加一个整数列

下面是我的迁移代码

public static final Migration MIGRATION_1_2 = new Migration(1, 2) {
    @Override
    public void migrate(SupportSQLiteDatabase database) {
        database.execSQL("ALTER TABLE UserInfo "
                + " ADD COLUMN MyFriendListCount INTEGER");
    }
};

有人可以指出我在这里缺少哪一段代码吗?

从 Mike 的解决方案中得到帮助以帮助我理解,当您选择 NOT NULL 时,您还需要将默认值设置为列

public static final Migration MIGRATION_1_2 = new Migration(1, 2) {
    @Override
    public void migrate(SupportSQLiteDatabase database) {
        database.execSQL("ALTER TABLE UserInfo "
                + " ADD COLUMN MyFriendListCount INTEGER default 0 NOT NULL");

    }
};

房间期待:-

MyFriendListCount=Column{name='MyFriendListCount', type='INTEGER', notNull=true, primaryKeyPosition=0}, 

您提供了:-

MyFriendListCount=Column{name='MyFriendListCount', type='INTEGER', notNull=false, primaryKeyPosition=0}, 

我相信你需要添加NOT NULL约束,所以也许使用 :-

public static final Migration MIGRATION_1_2 = new Migration(1, 2) {
    @Override
    public void migrate(SupportSQLiteDatabase database) {
        database.execSQL("ALTER TABLE UserInfo "
                + " ADD COLUMN MyFriendListCount INTEGER NOT NULL");
    }
};
For version from 2 to 3    

 val MIGRATION_2_3 = object : Migration(2, 3) {
        override fun migrate(database: SupportSQLiteDatabase) {
            //Integer values
            database.execSQL(
                "ALTER TABLE ProjectListingResponse "
                        + " ADD COLUMN dummy INTEGER default 0 NOT NULL"
            );
            //String values
            database.execSQL(
                "ALTER TABLE ProjectListingResponse "
                        + " ADD COLUMN dummy2 TEXT default 0 NOT NULL"
            );
        }
    }

  Room.databaseBuilder(context.applicationContext, AppDatabase::class.java,DATABASE_NAME)
                       // .fallbackToDestructiveMigration()         //will delete all existing data from device and update new schema
                       .addMigrations(MIGRATION_1_2, MIGRATION_2_3)  //Only update the schema much recomonded
                        .build()

您可以使用助手来进行表格更改,例如: 添加,删除,重命名或更改列的方案。

它处理临时表的创建,用数据填充临时表并删除旧表。

助手源代码: https : //gist.github.com/Benjiko99/23fbeee37d1d9f9a8b52ad21fc2585b9

fun alterTableUsage(database: SupportSQLiteDatabase) {
    DbMigrationsHelper.alterTable(
        db = database,
        tableName = "Reservations",
        columns = mapOf(
            "id INTEGER".toExisting(), // Retains without changes
            "title TEXT".toExisting("name"), // Renames column "name" to "title"
            "description TEXT".toNothing(), // Adds a new column
            "creatorId INTEGER NOT NULL".toExisting() // Change scheme from "creatorId INTEGER" and add "NON NULL"
            // Any columns that existed in the "Reservations" table
            // and aren't specified in this map will be removed
        ),
        primaryKeys = listOf("id") // Add more than one key to create a compound primary key
    )
}

暂无
暂无

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

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