Java 使用 Android 和 SQLite 从数据库中获取布尔值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4088080/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 11:28:08  来源:igfitidea点击:

Get boolean from database using Android and SQLite

javaandroidsqliteboolean

提问by Kevin Bradshaw

How can I obtain the value of a boolean field in an SQLitedatabase on Android?

如何在 Android 上的SQLite数据库中获取布尔字段的值?

I usually use getString(), getInt(), etc. to get the values of my fields, but there does not seem to be a getBoolean()method.

我通常使用getString(),getInt()等来获取我的字段的值,但似乎没有getBoolean()方法。

采纳答案by Alex Orlov

It is:

这是:

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

回答by NG.

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 referenceon SQLite 3.0.

SQLite 中没有 bool 数据类型。使用您固定为 0 或 1 的 int 来实现该效果。请参阅SQLite 3.0上的数据类型参考

回答by zoeb

You can also use

你也可以使用

boolean value =cursor.getString(boolean_column_index).equals("True");

回答by Elvis

boolean value = (cursor.getInt(boolean_column_index) == 1);

回答by Sojurn

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

如果您在其中存储 int 的列也允许为 null,则此处的大多数答案都可能导致 NumberFormatExceptions 或“未定义类型为 null、int 的运算符”。这样做的体面方法是使用

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

though you are now limited to storing the strings "true" and "false" rather than 0 or 1.

尽管您现在只能存储字符串“true”和“false”而不是 0 或 1。

回答by Gokhan Arik

Another option

另外一个选项

boolean value = (cursor.getString(column_index)).equals("1");

回答by rtack

An implementation found at Ormlite Cursoralso checks for Null which none of the other answers do.

Ormlite Cursor 上的一个实现也检查了 Null,而其他答案都没有。

   public boolean getBoolean(int columnIndex) {
        if (cursor.isNull(columnIndex) || cursor.getShort(columnIndex) == 0) {
            return false;
        } else {
            return true;
        }
    }

回答by RedBullet

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

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

回答by Ravi

booleandatatype is not available in Cursor.

boolean数据类型在Cursor.

you will get the result in an int, so you need to convert that intvalue to a boolean.

您将在 an 中获得结果int,因此您需要将该int值转换为 a boolean

You can either use

您可以使用

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

or

或者

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

回答by silexcorp

Well, that's very simple:

嗯,这很简单:

public boolean getBooleanState(SQLiteDatabase db){
    boolean result = false;
    try{
        String QUERY = "SELECT " + BOOLEAN_DATA + " FROM " + TABLE_NAME + " WHERE " + ID + " = 1";
        Cursor cursor = db.rawQuery(QUERY, null);
        if (cursor.moveToFirst()){
            if(cursor.getString(0).equalsIgnoreCase("1")){
                result = true;
            }
        }
        c.close();
    }catch(Exception ee){
        Log.e(TAG, "err getBooleanState: " + TABLE_NAME );
    }
    return result;
}