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
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.
boolean value = (cursor.getInt(boolean_column_index) == 1);
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.
You can also use
boolean value =cursor.getString(boolean_column_index).equals("True");
It is:
boolean value = cursor.getInt(boolean_column_index) > 0;
Another option
boolean value = (cursor.getString(column_index)).equals("1");