23

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?

Dishonered
  • 8,449
  • 9
  • 37
  • 50

2 Answers2

61

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.

CanonicalBear
  • 367
  • 8
  • 12
Pankaj Kumar
  • 81,967
  • 29
  • 167
  • 186
  • 2
    `@Query("SELECT * FROM tbl_restaurant WHERE isFavourite == :favourite") fun getFavouriteRestaurant(favourite:Boolean): LiveData>` In my case it's giving all the data. When I check on database it should give just 3 data. – Viks Feb 10 '21 at 02:03
  • 1
    thanks for your comment. Above query is 100% correct I had an issue with fragment class. Now its solved. Note: No need to pass 0 and 1 because Room handle boolean. – Viks Feb 10 '21 at 09:31
  • 1
    @CanonicalBear Could you please add your edit as an answer? It would be with your identity always :) – Pankaj Kumar Mar 09 '21 at 13:26
  • @CanonicalBear That's great. And here is your reward :) – Pankaj Kumar Mar 19 '21 at 05:49
2

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.

CanonicalBear
  • 367
  • 8
  • 12