Search code examples
javaandroidsqlitebooleanandroid-sqlite

How to get Bool from Cursor (Java SQLite)


So for example if I do a table like

db.execSQL("create table myTable (" + "id integer primary key autoincrement," + "title text," + "isImportant numeric" + ");");

and want to get "title", I do

String result;
Cursor c...;
int titleIndex = c.getColumnIndex("title");
do
  ...
  result = c.getString(titleIndex);
  ...
while (c.moveToNext());

Same with c.getInt, c.getDoule, etc. But there is no c.getBoolean. So how can I get value of "isImportant" from that table?


Solution

  • There is no getBoolean() method because there is no Boolean data type in SQLite (although you can define a column as Boolean).

    If you have stored the values in the column as 0 or 1, then you can retrieve boolean values like this:

    String result;
    boolean isImportant;
    Cursor c...;
    int titleIndex = c.getColumnIndex("title");
    do
        ...
        result = c.getString(titleIndex);
        isImportant = (c.getInt(isImportantIndex) == 1);
        ...
    while (c.moveToNext());