【问题标题】:How to get Bool from Cursor (Java SQLite)如何从光标中获取布尔值(Java SQLite)
【发布时间】:2021-12-17 20:06:06
【问题描述】:

例如,如果我做一个像这样的表

db.execSQL("create table myTable (" + "id integer primary key autoincrement," + "title text," + "isImportant numeric" + ");");

想要获得“头衔”,我愿意

String result;
Cursor c...;
int titleIndex = c.getColumnIndex("title");
do
  ...
  result = c.getString(titleIndex);
  ...
while (c.moveToNext());

与 c.getInt、c.getDoule 等相同。但没有 c.getBoolean。那么如何从该表中获取“isImportant”的值?

【问题讨论】:

    标签: java android sqlite boolean android-sqlite


    【解决方案1】:

    没有getBoolean() 方法,因为there is no Boolean data type in SQLite(尽管您可以将列定义为布尔值)。

    如果您将列中的值存储为01,那么您可以像这样检索布尔值:

    String result;
    boolean isImportant;
    Cursor c...;
    int titleIndex = c.getColumnIndex("title");
    do
        ...
        result = c.getString(titleIndex);
        isImportant = (c.getInt(isImportantIndex) == 1);
        ...
    while (c.moveToNext());
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-11-12
      • 1970-01-01
      • 2020-02-16
      • 2014-02-12
      • 2019-12-30
      • 1970-01-01
      • 2019-01-31
      相关资源
      最近更新 更多