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

标签 android database

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

相关文章:

android - LibGDX - ShapeRenderer 在 Android 上太慢/低 FPS

android - FTS3 和 FTS4 匹配 :, - 和 _ 字符

android - 为什么我的 ServiceConnection 方法从未执行过?

android - 如何使用新的设计支持库 23.2 实现底部表

android - 当我停止录制时 MediaRecorder 失败

database - Oracle 查询优化器的诊断输出

mysql - Access(SQL)将一个数据中的两个字段拆分为两个数据

mysql - 如何优化此查询?运行需要 3 分钟

java - 在数据库中排序数据的优缺点?

java "database is locked"SQL异常