插入空的整数值到数据库 [英] inserting null as Integer value into a database

查看:274
本文介绍了插入空的整数值到数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试存储空值到数据库中,但每次我打开我得到0的值。

I am trying to store a null value into the database but everytime I load the value I get 0.

我宣布现场就像 INTVAL整数

这是我如何找回它:

Integer x;

x = cursor.getInt(cursor.getColumnIndexOrThrow(MyDBAdapter.KEY_X));

是可靠的?抑或是未定义?

Is that reliable? or is it undefined?

所以,在我看来,一个人不能为空保存为未定义integervalue

So it seems to me one cannot save null as an undefined integervalue

感谢

推荐答案

调用getInt()文档

返回所需的列一个int值。   结果这种方法是否抛出一个异常,当列值为null,该列类型不是整型或整型值的范围为[Integer.MIN_VALUE的,是Integer.MAX_VALUE]外面是实现定义的。

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.

所以,这是一个实现细节,你不应该依赖。

So, it's an implementation detail which you shouldn't rely on.

您仍然可以得到你想要的效果,但:你只需做的空检查你读了前值

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

这篇关于插入空的整数值到数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆