检查 Room DB 中的列是否为 NULL 或空

3
我希望获取一个列表,其中deletedAt值为NULLMyModel。这是我的表格:
@Entity(tableName = "my_table")
data class MyModel(
    @PrimaryKey
    @ColumnInfo(name = "id")
    var id: String,
    @ColumnInfo(name = "deletedAt")
    var deletedAt: Date? = null
)

道:

@Dao
abstract class MyDao : BaseDao<MyModel> {

@Query("SELECT * from my_table WHERE deletedAt IS NULL")
abstract fun getList(): LiveData<List<MyModel>>
}

我的类型转换器:

object DateTypeConverter {
@TypeConverter
@JvmStatic// It need to added else gives unwanted data binding error :D
fun fromTimestamp(value: Long?): Date? {
    return if (value == null) null else Date(value)
}

@TypeConverter
@JvmStatic
fun dateToTimestamp(date: Date?): Long? {
    return date?.time
}

但是,即使某些条目的“deletedAt”值为空,它也会返回所有MyModel数据。


1
你的查询似乎有误,你是在检查createdAt是否为空还是deletedAt是否为空?我认为应该是 @Query("SELECT * from my_table WHERE deletedAt IS NULL"). - Xuzan
抱歉,我已经更新了我的问题。 - Nabin
4个回答

14

尝试使用NULLIF来检查nullempty,如下所示

NULLIF(deletedAt, '') IS NULL

查询完整代码为:Query

@Query("SELECT * from my_table WHERE NULLIF(deletedAt, '') IS NULL")

1
您可以在模型类中使用。
@ColumnInfo(name = "user_name")
@NonNull private String strname;

0

只需写

@Query("SELECT * from my_table WHERE deletedAt IS NULL")

一个常见的错误是写成like null
确保您的数据类为:
data class(
    val id : Long,
    val deletedAt : Long? 
    /....
}

0

检查具有唯一ID的行中的字符串或值

检查房间数据库结构和查询

@Query("select *from surveys where attendance_reg_id !=0 and aw_reg_id !=0 and b_form_reg_id !=0 and evs_reg_id !=0 and stu_status_reg_id !=0 and student_pic !="" and b_form_pic !="" and ar_pic !=""")

suspend fun getCompleteSurveys(): List**


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接