【问题标题】:Migration room database, alter table, android?迁移室数据库,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:
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");
    }
};

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

【问题讨论】:

    标签: android sqlite migration android-room


    【解决方案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=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");
            }
        };
        

        【讨论】:

        • 添加上述代码后出现此异常“无法添加默认值为 NULL 的 NOT NULL 列(代码 1):,同时编译:ALTER TABLE UserInfo ADD COLUMN MyFriendListCount INTEGER NOT NULL”
        猜你喜欢
        • 2018-12-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-05-11
        • 2020-11-24
        • 2018-03-04
        • 2017-10-27
        • 2021-08-13
        相关资源
        最近更新 更多