我试图将一个空值存储到数据库中,但每次加载该值时我都得到 0。
我声明字段就像“intVal integer”
这是我检索它的方式:
Integer x;
x = cursor.getInt(cursor.getColumnIndexOrThrow(MyDBAdapter.KEY_X));
靠谱吗?还是未定义?
所以在我看来不能将 null 保存为未定义的整数值
非常感谢
最佳答案
来自 the getInt()
documentation :
Returns the value of the requested column as an int. The result and whether this method throws an exception when the column value is null, the column type is not an integral type, or the integer value is outside the range [Integer.MIN_VALUE, Integer.MAX_VALUE] is implementation-defined.
因此,这是您不应依赖的实现细节。
不过,您仍然可以获得您想要的效果:您只需在读取值之前进行空检查。
int index = cursor.getColumnIndexOrThrow(MyDBAdapter.KEY_X);
Integer x = null;
if (!cursor.isNull(index)
x = cursor.getInt(index);
// now x is either null or contains a valid value
关于android - 将空值作为整数值插入数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8063768/