I am trying to store a null value into the database but everytime I load the value I get 0.
我试图将空值存储到数据库中,但每次我加载值得到0。
I declare the field just like "intVal integer"
我声明该字段就像“intVal integer”
This is how I retrieve it:
这是我检索它的方式:
Integer x;
x = cursor.getInt(cursor.getColumnIndexOrThrow(MyDBAdapter.KEY_X));
Is that reliable? or is it undefined?
这可靠吗?还是未定义?
So it seems to me one cannot save null as an undefined integervalue
所以在我看来,无法将null保存为未定义的整数值
Many thanks
非常感谢
3 个解决方案
#1
15
From the getInt()
documentation:
从getInt()文档:
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形式返回请求列的值。结果以及当列值为null时,此方法是否抛出异常,列类型不是整数类型,或者整数值超出范围[Integer.MIN_VALUE,Integer.MAX_VALUE]是实现定义的。
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
#2
0
SQLite is dynamically typed. Since you defined column of type Integer and it didn't find a value it returned 0 based on the hint.
SQLite是动态类型的。由于您定义了Integer类型的列,并且它没有找到值,因此它根据提示返回0。
Try using String as the type of column
尝试使用String作为列的类型
#3
0
As Rajdeep Dua mentioned, SQLite uses a dynamic typing. Here's what the documentation says:
正如Rajdeep Dua所提到的,SQLite使用动态类型。这是文档说的内容:
Most SQL database engines (every SQL database engine other than SQLite, as far as we know) uses static, rigid typing. With static typing, the datatype of a value is determined by its container - the particular column in which the value is stored.
大多数SQL数据库引擎(除了SQLite之外的每个SQL数据库引擎,据我们所知)都使用静态,严格的类型。使用静态类型时,值的数据类型由其容器(存储值的特定列)确定。
SQLite uses a more general dynamic type system. In SQLite, the datatype of a value is associated with the value itself, not with its container.
SQLite使用更通用的动态类型系统。在SQLite中,值的数据类型与值本身相关联,而不是与其容器相关联。
The Android documentation says that the result of getInt() when the column value is null
is implementation defined. For me, it returns 0 as well.
Android文档说,当列值为null时,getInt()的结果是实现定义的。对我来说,它也返回0。
So I used getString() instead, although the result of this method is also implementation defined, for me, it returns null
when the column value is null
, or a string containing the number otherwise.
所以我使用了getString(),虽然这个方法的结果也是实现定义的,对我来说,当列值为null时返回null,否则返回包含数字的字符串。
So the code looks like this:
所以代码看起来像这样:
Strint strVal = cursor.getInt(cursor.getColumnIndexOrThrow(MyDBAdapter.KEY_X));
if(strVal != null){
int intVal = Integer.parseInt(strVal);
}
#1
15
From the getInt()
documentation:
从getInt()文档:
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形式返回请求列的值。结果以及当列值为null时,此方法是否抛出异常,列类型不是整数类型,或者整数值超出范围[Integer.MIN_VALUE,Integer.MAX_VALUE]是实现定义的。
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
#2
0
SQLite is dynamically typed. Since you defined column of type Integer and it didn't find a value it returned 0 based on the hint.
SQLite是动态类型的。由于您定义了Integer类型的列,并且它没有找到值,因此它根据提示返回0。
Try using String as the type of column
尝试使用String作为列的类型
#3
0
As Rajdeep Dua mentioned, SQLite uses a dynamic typing. Here's what the documentation says:
正如Rajdeep Dua所提到的,SQLite使用动态类型。这是文档说的内容:
Most SQL database engines (every SQL database engine other than SQLite, as far as we know) uses static, rigid typing. With static typing, the datatype of a value is determined by its container - the particular column in which the value is stored.
大多数SQL数据库引擎(除了SQLite之外的每个SQL数据库引擎,据我们所知)都使用静态,严格的类型。使用静态类型时,值的数据类型由其容器(存储值的特定列)确定。
SQLite uses a more general dynamic type system. In SQLite, the datatype of a value is associated with the value itself, not with its container.
SQLite使用更通用的动态类型系统。在SQLite中,值的数据类型与值本身相关联,而不是与其容器相关联。
The Android documentation says that the result of getInt() when the column value is null
is implementation defined. For me, it returns 0 as well.
Android文档说,当列值为null时,getInt()的结果是实现定义的。对我来说,它也返回0。
So I used getString() instead, although the result of this method is also implementation defined, for me, it returns null
when the column value is null
, or a string containing the number otherwise.
所以我使用了getString(),虽然这个方法的结果也是实现定义的,对我来说,当列值为null时返回null,否则返回包含数字的字符串。
So the code looks like this:
所以代码看起来像这样:
Strint strVal = cursor.getInt(cursor.getColumnIndexOrThrow(MyDBAdapter.KEY_X));
if(strVal != null){
int intVal = Integer.parseInt(strVal);
}