mysql 字段为空 integer,将null作为Integer值插入数据库

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

I declare the field just like "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

Many thanks

解决方案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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值