迁移室数据库,alter table,android?

Posted

技术标签:

【中文标题】迁移室数据库,alter table,android?【英文标题】:Migration room database, alter table, android? 【发布时间】:2018-12-07 23:29:36 【问题描述】:

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

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

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

Migration didn't properly handle UserInfo(ima.rvtech.model.api.result.UserInfo).
 Expected:
TableInfoname='UserInfo', columns=
address=Columnname='address', type='TEXT', notNull=false, primaryKeyPosition=0, 
password=Columnname='password', type='TEXT', notNull=false, primaryKeyPosition=0, 
actBy=Columnname='actBy', type='TEXT', notNull=false, primaryKeyPosition=0, 
emailId=Columnname='emailId', type='TEXT', notNull=false, primaryKeyPosition=0, 
userType=Columnname='userType', type='TEXT', notNull=false, primaryKeyPosition=0, 
pinCode=Columnname='pinCode', type='TEXT', notNull=false, primaryKeyPosition=0, 
uploadImagePath=Columnname='uploadImagePath', type='TEXT', notNull=false, primaryKeyPosition=0, 
loginId=Columnname='loginId', type='TEXT', notNull=false, primaryKeyPosition=0, 
actDate=Columnname='actDate', type='TEXT', notNull=false, primaryKeyPosition=0, 
contactNo=Columnname='contactNo', type='TEXT', notNull=false, primaryKeyPosition=0, 
uploadVideoPath=Columnname='uploadVideoPath', type='TEXT', notNull=false, primaryKeyPosition=0, 
edbNo=Columnname='edbNo', type='TEXT', notNull=false, primaryKeyPosition=0, 
id=Columnname='id', type='INTEGER', notNull=true, primaryKeyPosition=1, 
emergencyContactNo=Columnname='emergencyContactNo', type='TEXT', notNull=false, primaryKeyPosition=0, 
bannerImagePath=Columnname='bannerImagePath', type='TEXT', notNull=false, primaryKeyPosition=0, 
MyFriendListCount=Columnname='MyFriendListCount', type='INTEGER', notNull=true, primaryKeyPosition=0, 
userName=Columnname='userName', type='TEXT', notNull=false, primaryKeyPosition=0, 
operationType=Columnname='operationType', type='TEXT', notNull=false, primaryKeyPosition=0, foreignKeys=[], indices=[]
 Found:
TableInfoname='UserInfo', columns=
address=Columnname='address', type='TEXT', notNull=false, primaryKeyPosition=0,
 password=Columnname='password', type='TEXT', notNull=false, primaryKeyPosition=0, 
actBy=Columnname='actBy', type='TEXT', notNull=false, primaryKeyPosition=0, 
emailId=Columnname='emailId', type='TEXT', notNull=false, primaryKeyPosition=0, 
userType=Columnname='userType', type='TEXT', notNull=false, primaryKeyPosition=0, 
pinCode=Columnname='pinCode', type='TEXT', notNull=false, primaryKeyPosition=0,
 uploadImagePath=Columnname='uploadImagePath', type='TEXT', notNull=false, primaryKeyPosition=0, 
loginId=Columnname='loginId', type='TEXT', notNull=false, primaryKeyPosition=0, 
actDate=Columnname='actDate', type='TEXT', notNull=false, primaryKeyPosition=0, 
contactNo=Columnname='contactNo', type='TEXT', notNull=false, primaryKeyPosition=0, 
uploadVideoPath=Columnname='uploadVideoPath', type='TEXT', notNull=false, primaryKeyPosition=0, 
edbNo=Columnname='edbNo', type='TEXT', notNull=false, primaryKeyPosition=0, 
id=Columnname='id', type='INTEGER', notNull=true, primaryKeyPosition=1, 
emergencyContactNo=Columnname='emergencyContactNo', type='TEXT', notNull=false, primaryKeyPosition=0, 
bannerImagePath=Columnname='bannerImagePath', type='TEXT', notNull=false, primaryKeyPosition=0, 
MyFriendListCount=Columnname='MyFriendListCount', type='INTEGER', notNull=false, primaryKeyPosition=0, 
userName=Columnname='userName', type='TEXT', notNull=false, primaryKeyPosition=0, 
operationType=Columnname='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");
    
;

有人能指出我在这里缺少什么代码吗?

【问题讨论】:

【参考方案1】:
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()

【讨论】:

【参考方案2】:

从 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");

    
;

【讨论】:

【参考方案3】:

房间正在等待:-

MyFriendListCount=Columnname='MyFriendListCount', type='INTEGER', notNull=true, primaryKeyPosition=0, 

您提供了:-

MyFriendListCount=Columnname='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");
    
;

【讨论】:

添加上述代码后出现此异常“无法添加默认值为 NULL 的 NOT NULL 列(代码 1):,同时编译:ALTER TABLE UserInfo ADD COLUMN MyFriendListCount INTEGER NOT NULL”

以上是关于迁移室数据库,alter table,android?的主要内容,如果未能解决你的问题,请参考以下文章

Room Database Migration 无法正确处理 ALTER TABLE 迁移

oracle中比较alter table t move 和alter table t shrink space

Alter table modify enum in Knex js for Postgresql 给出错误

Django South:结合“alter table”以获得更好的性能?

ALTER TABLE CREATE CONSTRAINT IF NOT EXIST 可能吗?

在某些情况下,ALTER TABLE 无法按预期工作