简体   繁体   English

Room 数据库是否支持实体中的布尔变量?

[英]Does Room database support boolean variables in entity?

I know that sqlite does not support Boolean and we need to use int columns to mimic the behavior of Boolean .我知道 sqlite 不支持 Boolean ,我们需要使用 int 列来模仿 Boolean 的行为。 But does Room support Boolean ?但是 Room 支持 Boolean 吗? What if have a Boolean in my entity ?如果我的实体中有一个布尔值怎么办? Will it work as expected?它会按预期工作吗?

Yes it does.是的,它确实。 When you store boolean using room, it automatically stores 1 for true and 0 for false .当您使用 room 存储 boolean 时,它会自动1 for true存储1 for true 0 for false存储0 for false

And same case while reading.阅读时也是同样的情况。 It converts 1 or 0 to true/ false respectively.它分别将 1 或 0 转换为真/假。

Edit: I would like to add to this answer by giving an example of a migration, where a Boolean column was added: Original Entity编辑:我想通过给出一个迁移示例来添加这个答案,其中添加了一个布尔列:原始实体

@Entity(tableName = TABLE_NAME)
data class SessionEntity(
    @PrimaryKey(autoGenerate = true) var key: Int = 0,
    @ColumnInfo(name = "start_datetime") val startDatetime: String,
    @ColumnInfo(name = "end_datetime") val endDatetime: String,
) {
    companion object {
        const val TABLE_NAME = "sessions"
    }
}

Updated Entity (added a Boolean column)更新实体(添加布尔列)

@Entity(tableName = TABLE_NAME)
data class SessionEntity(
    @PrimaryKey(autoGenerate = true) var key: Int = 0,
    @ColumnInfo(name = "start_datetime") val startDatetime: String,
    @ColumnInfo(name = "end_datetime") val endDatetime: String,
    @ColumnInfo(name = "uploaded_attempted") val uploadAttempted: Boolean, //new Boolean column added
) {
    companion object {
        const val TABLE_NAME = "sessions"
    }
}

This is the migration I wrote for it这是我为它写的迁移

val MIGRATION_1_2 = object : Migration(1,2){
override fun migrate(database: SupportSQLiteDatabase) {
    database.execSQL("ALTER TABLE $TABLE_NAME ADD COLUMN uploaded_attempted INTEGER NOT NULL DEFAULT(0)")
}

As can be seen, the column being added is of type INTEGER , and it needs a NOT NULL attribute and a default value.可以看出,被添加的列是INTEGER类型,它需要一个NOT NULL属性和一个默认值。

Adding on @Pankaj Kumar's answer above I want to give an example of a migration, where a Boolean column was added: Original Entity添加上面@Pankaj Kumar 的回答,我想举一个迁移的例子,其中添加了一个布尔列:原始实体

    @Entity(tableName = TABLE_NAME)
    data class SessionEntity(
        @PrimaryKey(autoGenerate = true) var key: Int = 0,
        @ColumnInfo(name = "start_datetime") val startDatetime: String,
        @ColumnInfo(name = "end_datetime") val endDatetime: String,
    ) {
        companion object {
            const val TABLE_NAME = "sessions"
        }
    }

Updated Entity (added a Boolean column)更新实体(添加布尔列)

@Entity(tableName = TABLE_NAME)
data class SessionEntity(
    @PrimaryKey(autoGenerate = true) var key: Int = 0,
    @ColumnInfo(name = "start_datetime") val startDatetime: String,
    @ColumnInfo(name = "end_datetime") val endDatetime: String,
    @ColumnInfo(name = "uploaded_attempted") val uploadAttempted: Boolean, //new Boolean column added
) {
    companion object {
        const val TABLE_NAME = "sessions"
    }
}

This is the migration I wrote for it这是我为它写的迁移

val MIGRATION_1_2 = object : Migration(1,2){
override fun migrate(database: SupportSQLiteDatabase) {
    database.execSQL("ALTER TABLE $TABLE_NAME ADD COLUMN uploaded_attempted INTEGER NOT NULL DEFAULT(0)")
}

As can be seen, the column being added is of type INTEGER , and it needs a NOT NULL attribute and a default value.可以看出,被添加的列是INTEGER类型,它需要一个NOT NULL属性和一个默认值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM