【问题标题】:Working with boolean values in an SQLite database在 SQLite 数据库中处理布尔值
【发布时间】:2016-11-10 03:19:26
【问题描述】:

我读到 SQLite 数据库支持 boolean 值。这是我创建表的查询:

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 存储为1false 为0?我必须使用cursor.getInt(column_index) == 1 来获取布尔值吗?我在我的代码中犯了错误吗?如果是这样,我该如何解决这个问题。有什么建议?

【问题讨论】:

  • 你的代码是什么语言的? PS你用谷歌搜索过这个吗?
  • 已经看到有关此论点的问题,但无法回答我的问题

标签: android database sqlite relational-database android-sqlite


【解决方案1】:

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

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

如果你想要 false 的值,那么:

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

查看this 帖子和this 帖子了解更多信息。

【讨论】:

  • 我已经看过这些帖子了,但我根本无法理解。如果我将这些列的值设置为 boolean 类型,当我插入记录时,我必须传递 integer 值(10)或 boolean?
  • 您将表中的列创建为INTEGER 类型并传递10 的值。 booleans 在 SQLite 中不存在,以整数形式存储。
  • 让我看看我是否理解.. 在创建表的查询中,我必须boolean 类型更改为integer(无论是否大写都相同,对吗?),但我可以在插入方法中传递boolean 类型的值,数据库将自动将其转换为integer01)(所以我只需要更改查询来制作表格)
  • 几乎正确。您必须传递一个integer0 用于false1 用于true。如果你传递了boolean,你的应用程序可能会崩溃。数据类型boolean 在SQLite 中不存在。
  • 非常感谢 :)
    我必须对我的提醒应用程序进行重大更改。如果您的知识非常基础,那就很难编码,但我喜欢它
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-12-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-05-31
  • 1970-01-01
相关资源
最近更新 更多