我在游标对象中有一个选择查询的结果。我在表中有一些 bool 列。 SQLite 中 bool 类型的值是 0 或 1,具体取决于它们分别是 false 还是 true。也有 int 类型的列,其值可以为 0 或 1 或其他整数值。我想从游标中返回一列的值,
if column is of boolean type
return true if value = 1
return false if value = 0
else
return the same value
我怎样才能做到这一点?
最佳答案
How to determine if a column is boolean from cursor object?
所以一开始,SQLite
没有 bool 类型 只有整数、字符串、 float 和 blob。所以我的建议是简单地测试 int 列的值,并根据结果执行适当的操作。
Cursor c = db.rawQuery(query, whereArgs);
int value = 0;
String[] intColumnNames = {columns};
int index = 0;
if (c.moveToFirst()) {
while (index < intColumnNames.length) {
do {
value = c.getInt(c.getColumnIndex(intColumnNames[index]));
switch (value) {
case 0:
// do some action
break;
case 1:
// do some action
break;
default:
// do some action for other cases
break;
}
} while(c.moveToNext());
index++;
}
}
关于android - 如何确定一列是否为游标对象的 bool 值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15334874/