android - 在 SQLite 数据库中使用 bool 值

标签 android database sqlite relational-database android-sqlite

我读到 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);
    }

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

最佳答案

SQLite 中不存在 bool 值。相反,您可以将 boolean 值作为 SQLite 中的 INTEGER 存储在数据库中。您可以使用的值是 0(表示 false)和 1(表示 true)。当您从数据库中选择INTEGER时,要将其更改为boolean,您可以使用:

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

如果您想要值为 false,则:

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

看看this发布和this发布以获取更多信息。

关于android - 在 SQLite 数据库中使用 bool 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38251678/

相关文章:

javascript - android中的HTML anchor 链接

android - 在 TextView 中显示微调器内容

android - "[aapt] ERROR: Unknown option ' --generate-dependencies '"用于 HelloWorld 的 ant 调试

MySQL 错误 : : 'Access denied for user ' root' @'localhost'

node.js - 更新 SQLite 数据库而不重新启动应用程序

android - 停止动画并重新开始

php - 基于其他查询形成查询?

mysql - MySQL数据库在哪里存储数据?

java - 使用 SQlite 数据库在自定义基础适配器 ListView 中存储复选框状态

java - 我有保留窗口,但数据未保存。如何将 boolean 值存储到数据库中?