176

How can I obtain the value of a boolean field in an SQLite database on Android?

I usually use getString(), getInt(), etc. to get the values of my fields, but there does not seem to be a getBoolean() method.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Kevin Bradshaw
  • 6,327
  • 13
  • 55
  • 78

13 Answers13

361

It is:

boolean value = cursor.getInt(boolean_column_index) > 0;
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Alex Orlov
  • 18,077
  • 7
  • 55
  • 44
46

There is no bool data type in SQLite. Use an int that you fix to 0 or 1 to achieve that effect. See the datatypes reference on SQLite 3.0.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
NG.
  • 22,560
  • 5
  • 55
  • 61
24
boolean value = (cursor.getInt(boolean_column_index) == 1);
Elvis
  • 831
  • 7
  • 9
  • Even though not exactly corresponding to this question (Alex's is the closest), it's an excellent answer, and I'll use this. – Budimir Grom Dec 16 '14 at 15:34
11

Most of the answers here can result in NumberFormatExceptions or "operator is undefined for the types null, int" if the column you stored the int in was allowed to also hold null. The decent way to do this would be to use

Boolean.parseBoolean(cursor.getString(booleanColumnIndex));`

though you are now limited to storing the strings "true" and "false" rather than 0 or 1.

Sojurn
  • 475
  • 6
  • 15
  • 5
    This does not work. Boolean.parseBoolean takes "true" or "false" strings. If you have stored your boolean as 0 or 1 you will always get false. – Gober Jul 17 '13 at 12:32
  • If what Gober says is true, then this answer deserves to be downvoted – Shervin Asgari Oct 11 '13 at 18:16
  • Ah, he's right. I think the clarity of the result outweighs having to store the strings. – Sojurn Oct 12 '13 at 09:22
  • 1
    @ShervinAsgari in the other answer if you store your boolean as true or false it will always get false. So it should be downvoted too with your logic. – Arda Kara Jul 26 '16 at 16:17
6

An implementation found at Ormlite Cursor also checks for Null which none of the other answers do.

   public boolean getBoolean(int columnIndex) {
        if (cursor.isNull(columnIndex) || cursor.getShort(columnIndex) == 0) {
            return false;
        } else {
            return true;
        }
    }
Szymon
  • 42,577
  • 16
  • 96
  • 114
rtack
  • 183
  • 3
  • 10
6

You can also use

boolean value =cursor.getString(boolean_column_index).equals("True");
AndyD273
  • 7,177
  • 12
  • 54
  • 92
zoeb
  • 69
  • 1
  • 1
4

boolean datatype is not available in Cursor.

you will get the result in an int, so you need to convert that int value to a boolean.

You can either use

boolean b = cursor.getInt(boolean_column_index) > 0;

or

boolean b = (cursor.getInt(boolean_column_index) != 0);
Ravi
  • 34,851
  • 21
  • 122
  • 183
2

Another option

boolean value = (cursor.getString(column_index)).equals("1");
Gokhan Arik
  • 2,626
  • 2
  • 24
  • 50
2

boolean b = (cursor.getInt(cursor.getColumnIndex("item")) != 0);

RedBullet
  • 21
  • 3
1

thats what I used:

    val work = Work()
    work.id = cursor.getInt(0)
    work.date = cursor.getString(1)
    work.work_value = cursor.getFloat(2)
    work.place = cursor.getString(3)
    work.wind = cursor.getFloat(4)
    work.isCompetition = cursor.getInt(5) > 0
    return work
EndrIT
  • 65
  • 9
0

Well, that's very simple:

public boolean getBooleanState(SQLiteDatabase db){
    boolean result = false;
    try{
        String QUERY = "SELECT " + BOOLEAN_DATA + " FROM " + TABLE_NAME + " WHERE " + ID + " = 1";
        Cursor cursor = db.rawQuery(QUERY, null);
        if (cursor.moveToFirst()){
            if(cursor.getString(0).equalsIgnoreCase("1")){
                result = true;
            }
        }
        c.close();
    }catch(Exception ee){
        Log.e(TAG, "err getBooleanState: " + TABLE_NAME );
    }
    return result;
}
silexcorp
  • 1,151
  • 1
  • 12
  • 13
0

For an optional (nullable) Boolean stored as INTEGER, you can create a Kotlin extension:

fun Cursor.getBoolean(columnIndex: Int): Boolean? {
    return if (isNull(columnIndex))
        null
    else 
        getInt(columnIndex) != 0
}

and use it like this:

val value: Boolean? = cursor.getBoolean(boolean_column_index)
Mario Huizinga
  • 780
  • 7
  • 14
0

I face the same thing in kotlin. There was the value "true/false" in the database and I access it with this code:

cursor.getString(4).toBoolean()

//first as a string then converting them to boolean

Procrastinator
  • 2,526
  • 30
  • 27
  • 36