Get boolean from database using Android and SQLite

后端 未结 10 1458
情话喂你
情话喂你 2020-12-07 09:02

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

相关标签:
10条回答
  • 2020-12-07 09:42

    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.

    0 讨论(0)
  • 2020-12-07 09:46
    boolean value = (cursor.getInt(boolean_column_index) == 1);
    
    0 讨论(0)
  • 2020-12-07 09:47

    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.

    0 讨论(0)
  • 2020-12-07 09:53

    You can also use

    boolean value =cursor.getString(boolean_column_index).equals("True");
    
    0 讨论(0)
  • 2020-12-07 09:54

    It is:

    boolean value = cursor.getInt(boolean_column_index) > 0;
    
    0 讨论(0)
  • 2020-12-07 09:56

    Another option

    boolean value = (cursor.getString(column_index)).equals("1");
    
    0 讨论(0)
提交回复
热议问题