java - 在整数列 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;
解决方案
由于int
永远不能为 null,因此空间无法将newColumn
实际上为 null 的数据库行映射到int
.
Integer
然而可以为空,因为它被int
包装到一个对象中,所以试试这个:
private Integer newColumn;
推荐阅读
- ruby-on-rails - Jekyll 中的环境
- c++ - 保存在向量中的 C++ Lambda 多次执行
- json - json JSON_ELEMENT_T oracle pl CHARACTER SET 解析 ISO88591
- python - 如何将自定义对象序列化为 JSON?
- c++ - cmake 基于目标的条件编译
- android - Android SQlite 错误 *没有这样的表:帐户*
- amazon-web-services - 我的 AWS Lambda 由 S3 对象创建触发,它发出复制对象请求。这会创建一个无限循环。有什么办法吗?
- ios - 在 Swift 中将 Markdown 格式的字符串转换为 NSAttributedString
- excel - Excel - 为什么两个相等的整数有非零差异?
- javascript - Javascript - firebase.auth().onAuthStateChanged 在登录/注销时未触发