gpt4 book ai didi

android - 将空值作为整数值插入数据库

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:57:49 25 4
gpt4 key购买 nike

我试图将一个空值存储到数据库中,但每次加载该值时我都得到 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com