You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Java SQLite开发中如何从Cursor对象获取Boolean类型值

获取isImportant布尔值的实现方案

SQLite 本身没有原生布尔类型,你建表时使用的numeric类型存储布尔值,本质会以整数1(对应true)、0(对应false)的形式存储,直接通过getInt取值后转换即可,具体实现如下:

基础写法

int isImportantIndex = c.getColumnIndex("isImportant");
// 非0即代表true,兼容SQLite默认的布尔存储规则
boolean isImportant = c.getInt(isImportantIndex) != 0;

兼容空值的写法

如果你的表允许isImportant字段为空,可以先做判空处理再取值:

boolean isImportant;
int isImportantIndex = c.getColumnIndex("isImportant");
if (c.isNull(isImportantIndex)) {
    // 可根据业务需求自定义空值对应的默认值
    isImportant = false;
} else {
    isImportant = c.getInt(isImportantIndex) != 0;
}

内容的提问来源于stack exchange,提问作者nikitka00009

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.27 19:06:04