Get boolean from database using Android and SQLite

后端 未结 10 1525
情话喂你
情话喂你 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.

提交回复
热议问题