本文介绍了将null作为Integer值插入数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图存储一个空值到数据库,但每次加载值我获得0。
I am trying to store a null value into the database but everytime I load the value I get 0.
我声明的字段就像 intVal integer
这是我如何检索它:
Integer x;
x = cursor.getInt(cursor.getColumnIndexOrThrow(MyDBAdapter.KEY_X));
这可靠吗?或者是未定义的?
Is that reliable? or is it undefined?
因此,我似乎无法将null保存为未定义的整数值。
So it seems to me one cannot save null as an undefined integervalue
推荐答案
从:
将请求的列的值作为int返回。
结果以及当列值为null时,此方法是否抛出异常,列类型不是整数类型,或整数值超出范围[Integer.MIN_VALUE,Integer.MAX_VALUE]是实现定义的。
所以,这是一个实现细节,你不应该依赖。
So, it's an implementation detail which you shouldn't rely on.
您仍然可以获得所需的效果,但是:您只需在读取值之前执行null检查。
You can still get the effect you want, though: you simply do the null check before you read the value.
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
这篇关于将null作为Integer值插入数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!