获取空整数针对Android源码 [英] Getting null ints from sqlite in android

查看:98
本文介绍了获取空整数针对Android源码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有可能实际上是一个设计问题的问题,但我在努力寻找办法解决它。

I have an issue that may in fact be a design issue but I am struggling to find a way around it.

在我的Andr​​oid应用我的SQLite数据库,我有一个名为表客户。这有各种字符串和int数据类型的40列。在实践中,任何这些列可以或可以不为空。

In my sqlite database in my android application, I have a table called Customer. This has around 40 columns of various string and int data types. In practice, any of these columns may or may not be null.

在我的code,我有一个函数简称 getCustomer(),其中查询特定客户数据库,其所有单元格放置从数据库成客户类,它包含了每一列的变量。那么我可以通过客户对象周围如我所愿。在 getCustomer()函数返回这个客户对象。

In my code, I have a function simply called getCustomer(), which queries the database for a specific customer, and places all their cells from the database into a Customer class, which contains variables for each column. I can then pass that customer object around as I wish. The getCustomer() function returns this Customer object.

我的问题是可为空整数。我熟悉如何 INT 不能在java中无效,但如何整数可以为null。但我的问题其实在于事实的细胞数据库的可能是空的(如空)。

My problem is integers which may be null. I am familiar with how int cannot be null in java, but how Integer can be null. But my problem actually lies in the fact that the cells in the database can be empty (eg null).

有关字符串列,我只是这样做:

For string columns, I simply do this:

Customer cust = new Customer();
cust.firstName = cursor.getString(0);

如果 cursor.getString(0); 返回一个空值,那么的firstName 变量赋值为null。尼斯和简单。然而,随着一个int:

If cursor.getString(0); returns a null value, then the firstName variable is assigned null. Nice and simple. However with an int:

Customer cust = new Customer();
cust.daysSinceJoining = cursor.getInt(5);

在运行时,如果 daysSinceJoining 上面的崩溃为空。所以,我试过如下:

The above crashes at run-time if daysSinceJoining is null. So I tried the following:

Customer cust = new Customer();
if (cursor.getInt(5) != null)
    cust.daysSinceJoining = cursor.getInt(5);

然而,这给了我一个编译器错误,因为你不能在那样的空比较使用 INT

我怎样才能解决这个问题呢?我怎样才能检索SQLite数据库一个​​int当int值的可能的为空?

How can I get around this problem? How can I retrieve an int from an sqlite database when the int value could be null?

推荐答案

您可以尝试 ISNULL()功能。

这篇关于获取空整数针对Android源码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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