简体   繁体   中英

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 . But does Room support 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 .

And same case while reading. It converts 1 or 0 to true/ false respectively.

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.

Adding on @Pankaj Kumar's answer above I want to give 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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