繁体   English   中英

Android房间迁移空错误

[英]Android room migration null error

当我在 Android 上从旧的 sqlite 方式迁移到 Room 时,我需要使用“INTEGER NOT NULL”进行编译。 问题是,当迁移发生时,您正在使用“NOT NULL”参数在新表中插入 NULL 字段,但出现错误

android.database.sqlite.SQLiteConstraintException:NOT NULL 约束失败:note.notification_state(代码 1299)

编辑:

11-29 22:52:58.891 14605-14630/com.aleksandarvasilevski.notes E/AndroidRuntime: FATAL EXCEPTION: pool-1-thread-1
                                                                            Process: com.aleksandarvasilevski.notes, PID: 14605
                                                                            java.lang.IllegalStateException: Migration didn't properly handle note(com.aleksandarvasilevski.notes.repository.db.Note).
                                                                             Expected:
                                                                            TableInfo{name='note', columns={notification_date=Column{name='notification_date', type='TEXT', notNull=false, primaryKeyPosition=0}, priority=Column{name='priority', type='INTEGER', notNull=true, primaryKeyPosition=0}, description=Column{name='description', type='TEXT', notNull=false, primaryKeyPosition=0}, title=Column{name='title', type='TEXT', notNull=false, primaryKeyPosition=0}, id=Column{name='id', type='INTEGER', notNull=true, primaryKeyPosition=1}, notification_state=Column{name='notification_state', type='INTEGER', notNull=true, primaryKeyPosition=0}, created_date=Column{name='created_date', type='TEXT', notNull=false, primaryKeyPosition=0}}, foreignKeys=[], indices=[]}
                                                                             Found:
                                                                            TableInfo{name='note', columns={notification_date=Column{name='notification_date', type='TEXT', notNull=false, primaryKeyPosition=0}, priority=Column{name='priority', type='INTEGER', notNull=false, primaryKeyPosition=0}, title=Column{name='title', type='TEXT', notNull=false, primaryKeyPosition=0}, description=Column{name='description', type='TEXT', notNull=false, primaryKeyPosition=0}, id=Column{name='id', type='INTEGER', notNull=true, primaryKeyPosition=1}, notification_state=Column{name='notification_state', type='INTEGER', notNull=false, primaryKeyPosition=0}, created_date=Column{name='created_date', type='TEXT', notNull=false, primaryKeyPosition=0}}, foreignKeys=[], indices=[]}
                                                                                at com.aleksandarvasilevski.notes.repository.db.NoteDatabase_Impl$1.validateMigration(NoteDatabase_Impl.java:70)
                                                                                at android.arch.persistence.room.RoomOpenHelper.onUpgrade(RoomOpenHelper.java:75)
                                                                                at android.arch.persistence.db.framework.FrameworkSQLiteOpenHelper$OpenHelper.onUpgrade(FrameworkSQLiteOpenHelper.java:118)
                                                                                at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:256)
                                                                                at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:163)
                                                                                at android.arch.persistence.db.framework.FrameworkSQLiteOpenHelper$OpenHelper.getWritableSupportDatabase(FrameworkSQLiteOpenHelper.java:93)
                                                                                at android.arch.persistence.db.framework.FrameworkSQLiteOpenHelper.getWritableDatabase(FrameworkSQLiteOpenHelper.java:54)
                                                                                at android.arch.persistence.room.RoomDatabase.query(RoomDatabase.java:193)
                                                                                at com.aleksandarvasilevski.notes.repository.db.NoteDao_Impl$5.compute(NoteDao_Impl.java:195)
                                                                                at com.aleksandarvasilevski.notes.repository.db.NoteDao_Impl$5.compute(NoteDao_Impl.java:181)
                                                                                at android.arch.lifecycle.ComputableLiveData$2.run(ComputableLiveData.java:87)
                                                                                at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133)
                                                                                at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607)
                                                                                at java.lang.Thread.run(Thread.java:761)

代码:

`@Database(entities = {Note.class}, version = 3) 公共抽象类 NoteDatabase extends RoomDatabase {

public static final Migration MIGRATION_2_3 = new Migration(2, 3) {
    @Override
    public void migrate(@NonNull SupportSQLiteDatabase database) {

        database.execSQL(
                "CREATE TABLE note (id INTEGER NOT NULL, title TEXT, description TEXT, created_date TEXT, notification_date TEXT, notification_state INTEGER, priority INTEGER, PRIMARY KEY(id))");


        database.execSQL(
                "INSERT INTO note (id, title, description, created_date) SELECT _ID, title, description, date FROM notes");

    }
};`

好的,我开始工作了,如果有人遇到同样的问题,请使用 NOT NULL DEFAULT 0(或其他数字)。

@Database(entities = {Note.class}, version = 3)
public abstract class NoteDatabase extends RoomDatabase {

    public static final Migration MIGRATION_2_3 = new Migration(2, 3) {
        @Override
        public void migrate(@NonNull SupportSQLiteDatabase database) {

            database.execSQL(
                    "CREATE TABLE note (id INTEGER NOT NULL, " +
                    "title TEXT, description TEXT, created_date TEXT, notification_date TEXT, " +
                    "notification_state INTEGER NOT NULL DEFAULT 0, " +
                    "priority INTEGER NOT NULL DEFAULT 0, PRIMARY KEY(id))");
            
            database.execSQL(
                    "INSERT INTO note (id, title, description, created_date) " +
                    "SELECT _ID, title, description, date FROM notes");
            
        }
    };
    
    // ...

}

迁移代码:

class Migration1To2: Migration(1, 2){
    override fun migrate(database: SupportSQLiteDatabase) {
        database.execSQL("ALTER TABLE 'table_name' ADD COLUMN 'row_to modify' INTEGER NOT NULL **DEFAULT 1"**)
    }
}

 Dao Entity add **defaultValue = "1"**:
 @ColumnInfo( name="row_to modify", defaultValue = "1")
    var row_to modify: Int

暂无
暂无

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

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