Search code examples
androidsqlitecursor

How to determine if a column is boolean from cursor object?


I've result of a select query in a cursor object. I've some Boolean columns in the table. The value for Boolean type in SQLite is 0 or 1 depending on if they are false or true respectively. There are columns of int type too which can have value as 0 or 1 or other integer value. I want to return the value of a column from the cursor as,

if column is of boolean type
   return true if value = 1
   return false if value = 0
else 
   return the same value

How can I accomplish this?


Solution

  • How to determine if a column is boolean from cursor object?

    So at first, SQLite does not have booleans type only integer, string, float, blob. So my suggestion is simply test value for int column and based on result perform appropriate action.

    Cursor c = db.rawQuery(query, whereArgs);
    int value = 0;
    String[] intColumnNames = {columns};
    int index = 0;
    if (c.moveToFirst()) {
       while (index < intColumnNames.length) {
          do {
             value = c.getInt(c.getColumnIndex(intColumnNames[index]));
             switch (value) {
                case 0:
                   // do some action
                break;
                case 1:
                   // do some action
                break;
                default:
                   // do some action for other cases
                break;
             }
          } while(c.moveToNext());
          index++;
       }
    }