gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-10-30 20:36:17 25 4
gpt4 key购买 nike

我读到 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 类型,当我添加一条记录时,我应该使用 1 还是 的整数值0 而不是 boolean 值,或者数据库会自动将 true 存储为 1false作为 0?我是否必须使用 cursor.getInt(column_index) == 1 来获取 bool 值?我的代码有误吗?如果是这样,我该如何解决这个问题。有什么建议么?

最佳答案

SQLite 中不存在 boolean。相反,您将 boolean 值作为 SQLite 中的 INTEGER 存储在数据库中。您可以使用的值是 0 代表 false1 代表 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com