首页 > 解决方案 > 在整数列 SQLite Android Room 中允许空值

问题描述

我正在尝试使用向我的数据库添加一个新列

static final Migration MIGRATION_16_17 = new Migration(16,17) {
        @Override
        public void migrate(@NonNull SupportSQLiteDatabase database) {

            database.execSQL("ALTER TABLE `myTable` ADD COLUMN newColumn INTEGER");

        }
    };

但是它给了我错误:

原因:java.lang.IllegalStateException:迁移没有正确处理:衣服(com.dayaramo.wearyourcloset.Objects.ClothingItem)。

问题是它期望 notNull=true 即使我想在此列中允许空值。

我的问题是整数列可以有空值,如果是这样,我该如何设置 notNull=false?

我的实体类

 @PrimaryKey(autoGenerate = true)
    private long id;
    private String item;
    private int itemProductNum;
    private int newColumn;

标签: javaandroidsqliteandroid-room

解决方案


由于int永远不能为 null,因此空间无法将newColumn实际上为 null 的数据库行映射到int.

Integer然而可以为空,因为它被int包装到一个对象中,所以试试这个:

private Integer newColumn;

推荐阅读