Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 826
Dishonered Avatar asked Jul 02 '18 07:07

Dishonered


2 Answers

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.

like image 163
Pankaj Kumar Avatar answered Sep 19 '22 20:09

Pankaj Kumar


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.

like image 20
CanonicalBear Avatar answered Sep 16 '22 20:09

CanonicalBear