android 检查房间DB中的列是否为NULL或空

soat7uwm  于 2023-02-06  发布在  Android
关注(0)|答案(4)|浏览(150)

我想得到一个**MyModel的列表,它的deletedAt**值是NULL

@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数据。

dgjrabp2

dgjrabp21#

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

NULLIF(deletedAt, '') IS NULL

完整的Query

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

ttcibm8c2#

您可以在模型类中使用

@ColumnInfo(name = "user_name")
@NonNull private String strname;
jvidinwx

jvidinwx3#

写吧

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

常见的错误是将like null
确保您的数据类

data class(
    val id : Long,
    val deletedAt : Long? 
    /....
}
vyu0f0g1

vyu0f0g14#

选中具有唯一ID的行中的字符串或值
Check the Room DB Structure and Query
@query (“从出勤登记ID!=0、aw登记ID!=0、B表格登记ID!=0、evs登记ID!= 0、stu状态登记ID!=0、学生照片!="”、b表格照片!="”和ar照片!=""”的调查中选择 “)
暂停资金获取完成调查():名单
*

相关问题