173

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.

13 Answers 13

360

It is:

boolean value = cursor.getInt(boolean_column_index) > 0;
4
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.

24
boolean value = (cursor.getInt(boolean_column_index) == 1);
1
  • Even though not exactly corresponding to this question (Alex's is the closest), it's an excellent answer, and I'll use this. Dec 16, 2014 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.

4
  • 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, 2013 at 12:32
  • If what Gober says is true, then this answer deserves to be downvoted Oct 11, 2013 at 18:16
  • Ah, he's right. I think the clarity of the result outweighs having to store the strings.
    – Sojurn
    Oct 12, 2013 at 9: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, 2016 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;
        }
    }
0
6

You can also use

boolean value =cursor.getString(boolean_column_index).equals("True");
1
3

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);
2

Another option

boolean value = (cursor.getString(column_index)).equals("1");
2

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

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;
}
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)
0

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
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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.