在 SQLite 数据库中使用布尔值

Working with boolean values in an SQLite database

我读到 SQLite 数据库支持 boolean 值。这是我创建 table:

的查询
private static final String CREATE_TABLE_PROMEMORIE_RSI = "CREATE TABLE IF NOT EXIXSTS "
            + promemorieRSI.PROMEMORIE_RSI_TABLE + " ("
            + promemorieRSI.ID + " integer primary key autoincrement, "
            + promemorieRSI.GIORNO + " integer, "
            + promemorieRSI.MESE + " integer, "
            + promemorieRSI.ANNO + " integer, "
            + promemorieRSI.NOT_RIP_RSI + " boolean, "
            + promemorieRSI.RIP_VAL_RSI + " integer, "
            + promemorieRSI.UNIT_RIP_VAL_RSI + " text, "
            + promemorieRSI.NOT_FISSA_RSI + " boolean, "
            + promemorieRSI.ETICHETTA_RSI + " text, "
            + promemorieRSI.REQUEST_CODE_RSI + " integer);";

如您所见,我将 not_rip_rsinot_fissa_rsi 列设置为 boolean 值。我读到数据库将 truefalse 值分别存储为 10 整数值,但现在我无法理解:

为了更好的解释,这是我的插入方法:

public void inserisciPromemoriaRSI(Integer giorno, Integer mese, Integer anno, Boolean not_rip, Integer rip_val, String unit_rip_val, Boolean not_fissa, String etichetta, Integer request_code){
        ContentValues val = new ContentValues();
        val.put(promemorieRSI.GIORNO, giorno);
        val.put(promemorieRSI.MESE, mese);
        val.put(promemorieRSI.ANNO, anno);
        val.put(promemorieRSI.NOT_RIP_RSI, not_rip);
        val.put(promemorieRSI.RIP_VAL_RSI, rip_val);
        val.put(promemorieRSI.UNIT_RIP_VAL_RSI, unit_rip_val);
        val.put(promemorieRSI.NOT_FISSA_RSI, not_fissa);
        val.put(promemorieRSI.ETICHETTA_RSI, etichetta);
        val.put(promemorieRSI.REQUEST_CODE_RSI, request_code);
        mDb.insert(promemorieRSI.PROMEMORIE_RSI_TABLE, null, val);
    }

如您所见,为了添加记录,我对这些列使用了 boolean 值。即使我将这些列的条目的值设置为 boolean 类型,当我添加一条记录时,我是否应该使用 10 的整数值而不是 boolean 值,或者数据库会自动将 true 存储为 1 并将 false 存储为 0?我必须使用 cursor.getInt(column_index) == 1 来获取布尔值吗?我的代码有误吗?如果是这样,我该如何解决这个问题。有什么建议么?

SQLite 中不存在 boolean。相反,您将 boolean 值作为 SQLite 中的 INTEGER 存储在数据库中。您可以使用的值是 0 for false1 for true。当您从数据库中 select INTEGER 将其更改为 boolean 时,您使用:

boolean isTrue = cursor.getInt(columnNo) > 0;

如果您想要 false 的值,则:

boolean isFalse = cursor.getInt(columnNo) < 1;

查看 this post and this post 了解更多信息。