房间,如何设置'fields not null value'为false?

Room, how to set 'field's notnull value' is false?

我想使用 room 在我的数据库中添加一个新列。我使用 Room 的迁移将我的数据库迁移到新版本,但它无法正常工作。一个INTERGER字段的默认notnull值是true,那真是给我带来了麻烦。请帮我。我已经坚持了好几个小时了。

我有一个位置 class。

package com.example.phamf.javamvvmapp.Model;
import ...

@Entity (tableName = "location")
public class Location {

    @PrimaryKey(autoGenerate = true)
    private int id;

    private float x;

    private float y;

    private String name;


    public Location(int id, float x, float y, String name) {
        this.id = id;
        this.x = x;
        this.y = y;
        this.name = name;
    }

    public Location () {

    }

    // Getter and setter
}

我想添加一个名为 type 的新字段,它的类型是 INTEGER,所以我将上面的 Location.java 文件修改为

package com.example.phamf.javamvvmapp.Model;
import ...

@Entity (tableName = "location")
public class Location {

    @PrimaryKey(autoGenerate = true)
    private int id;

    private int type;        

    private float x;

    private float y;

    private String name;


    public Location(int id, float x, float y, String name, int type) {
        this.id = id;
        this.x = x;
        this.y = y;
        this.name = name;
        this.type = type;
    }

    public Location () {

    }

    // Getter and setter
}

这是我的 RoomDatabase class。

    @Database(entities = {Location.class}, version = 2)
    public abstract class LocationRoomDatabase extends RoomDatabase {

    private static LocationRoomDatabase DATABASE;

    public static LocationRoomDatabase getDatabase (final Context context) {

        Migration migration = new Migration(1, 2) {
            @Override
            public void migrate(@NonNull SupportSQLiteDatabase database) {
                database.execSQL("ALTER TABLE location ADD type INTEGER");
            }
        };

        if (DATABASE == null) {
            DATABASE = Room.databaseBuilder(context, LocationRoomDatabase.class, "database")
                           .addMigrations(migration).build();
    }

        return DATABASE;
    }

    public abstract LocationDAO locationDAO();
}

问题是当我运行上面的代码时,我得到一个错误

//...

Caused by: java.lang.IllegalStateException:
   Migration didn't properly handle location(com.example.phamf.javamvvmapp.Model.Location).
Expected:
     TableInfo{..., type=Column{name='type', type='INTEGER', affinity='3', notNull=true, primaryKeyPosition=0}, foreignKeys=[], indices=[]}
Found:
     TableInfo{..., type=Column{name='type', type='INTEGER', affinity='3', notNull=false, primaryKeyPosition=0}}, foreignKeys=[], indices=[]}

//...

Expected TableInfo 和 Found TableInfo 之间的唯一区别是 'the notNull value'。 Expected 希望它为真,但 Found 不希望它为真。我意识到问题就在那里,所以我修改了我的迁移代码

        Migration migration = new Migration(1, 2) {
            @Override
            public void migrate(@NonNull SupportSQLiteDatabase database) {
                database.execSQL("ALTER TABLE location ADD type INTEGER not null");
            }
        };

我认为它会完美 运行 但后来我得到一个错误

Caused by: android.database.sqlite.SQLiteException: Cannot add a NOT NULL column with default value NULL (code 1 SQLITE_ERROR): , while compiling: ALTER TABLE location ADD type INTEGER not null

我也试过向 'type' 字段添加默认值,但它也不起作用

 private int type = 0;

有什么方法可以解决这个问题,请帮帮我。太感谢了。 如有语法错误,请见谅。

我终于找到了解决办法。通过在 SQL 命令中的“... ADD COLUMN column_name” 后添加 "DEFAULT someValue"。这使我的字段具有与预期的 table 信息相匹配的默认值。 即:

sql.execSQL("ALTER TABLE table_name ADD COLUMN column_name INTEGER DEFAULT 1 not null")