Obtener booleano de la base de datos usando Android y SQLite


¿Cómo puedo obtener el valor de un campo booleano en una base de datos SQLite en Android?

Normalmente uso getString(), getInt(), etc. para obtener los valores de mis campos, pero no parece haber un método getBoolean().

Author: Peter Mortensen, 2010-11-03

9 answers

Es:

boolean value = cursor.getInt(boolean_column_index) > 0;
 333
Author: Alex Orlov,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-01-20 03:04:14

No hay ningún tipo de datos bool en SQLite. Utilice un int que se fija a 0 o 1 para lograr ese efecto. Véase la referencia de los tipos de datos en SQLite 3.0.

 45
Author: NG.,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-01-20 03:05:37
boolean value = (cursor.getInt(boolean_column_index) == 1);
 20
Author: Elvis,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2011-11-25 16:13:37

La mayoría de las respuestas aquí pueden resultar en NumberFormatExceptions o "operator is undefined for the types null, int" si la columna en la que almacenó el int también tenía permitido contener null. La manera decente de hacer esto sería usar

Boolean.parseBoolean(cursor.getString(booleanColumnIndex));`

Aunque ahora está limitado a almacenar las cadenas "true" y "false" en lugar de 0 o 1.

 8
Author: Sojurn,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2013-10-12 09:21:04

Una implementación encontrada en Cursorlite Cursor también comprueba Null, lo que ninguna de las otras respuestas hace.

   public boolean getBoolean(int columnIndex) {
        if (cursor.isNull(columnIndex) || cursor.getShort(columnIndex) == 0) {
            return false;
        } else {
            return true;
        }
    }
 6
Author: rtack,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2013-12-12 02:21:28

También puedes usar

boolean value =cursor.getString(boolean_column_index).equals("True");
 6
Author: zoeb,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2015-04-22 15:02:02

Otra opción

boolean value = (cursor.getString(column_index)).equals("1");
 3
Author: Gokhan Arik,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2013-06-04 19:59:48

boolean el tipo de datos no está disponible en Cursor.

Obtendrá el resultado en un int, por lo que debe convertir ese valor int en un boolean.

Puede usar

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

O

boolean b = (cursor.getInt(boolean_column_index) != 0);
 3
Author: Ravi Rupareliya,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-01-20 04:12:51

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

 2
Author: RedBullet,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-06-07 13:13:41