Get boolean from database using Android and SQLite

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.


It is:

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

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.


boolean value = (cursor.getInt(boolean_column_index) == 1);

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.


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