5

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

在我的 android 应用程序的 sqlite 数据库中,我有一个名为Customer. 这有大约 40 列各种字符串和 int 数据类型。实际上,这些列中的任何一个都可能为空,也可能不为空。

在我的代码中,我有一个简单地称为 的函数getCustomer(),它查询数据库中的特定客户,并将数据库中的所有单元格放入一个Customer类中,该类包含每列的变量。然后,我可以随意传递该客户对象。该getCustomer()函数返回此Customer对象。

我的问题是可能为空的整数。我熟悉intjava中如何不能为null,但如何Integer可以为null。但我的问题实际上在于数据库中的单元格可以为空(例如空)。

对于字符串列,我只是这样做:

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

如果cursor.getString(0);返回空值,firstName则为变量分配空值。很好很简单。但是有一个int:

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

daysSinceJoining如果为空,则上述运行时崩溃。所以我尝试了以下方法:

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

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

我怎样才能解决这个问题?当 int 值可能为空时,如何从 sqlite 数据库中检索 int ?

4

5 回答 5

14

@sanders 关于该isNull()方法是正确的,这是您编辑代码以使用它的方式:

Customer cust = new Customer();
if (!cursor.isNull(5))
    cust.daysSinceJoining = cursor.getInt(5);
于 2013-08-05T09:21:56.790 回答
12

你可以试试这个isNull()功能。

于 2013-08-05T09:02:31.023 回答
6

请看一下那个答案:

getInt 空约束

我认为这应该对你有用:

int lIndex = cursor.getColumnIndexOrThrow(COLUMN);
Integer lInteger = null;
if (!cursor.isNull(lIndex)
lInteger = cursor.getInt(lIndex);
于 2013-08-05T09:24:05.627 回答
0

您可以创建游标包装器并向游标类添加新功能:

public class CustomCursor extends CursorWrapper {

    public CustomCursor(Cursor cursor) { super(cursor); } //simple constructor

    public Integer getInteger(int columnIndex) { // new method to return Integer instead of int
        if (super.isNull(columnIndex)){
            return null;
        }else{
            return super.getInt(columnIndex);
        }
    }
}

示例用法:

Cursor defaultCursor = db.rawQuery("select null,2 ", null);
defaultCursor.moveToFirst();
CustomCursor customCursor = new CustomCursor(defaultCursor);
customCursor.moveToFirst(); //custom cursor can do anything that default cursor can do

int defaultInt0 = defaultCursor.getInt(0); //nulls are usually converted into zero
int defaultInt1 = defaultCursor.getInt(1); //2 is correct

int customInt0 = customCursor.getInt(0); //these 2 are same as above , ie zero and 2
int customInt1 = customCursor.getInt(1);

Integer customInteger0 = customCursor.getInteger(0); // this will give a null Integer
Integer customInteger1 = customCursor.getInteger(1); // this will give a 2 Integer

Log.v("custom log.v call ", "lets see what outputs, null is usually converted to the word 'null' by the String.valueOf method :"
        +String.valueOf(defaultInt0)+","+String.valueOf(defaultInt1)+","
        +String.valueOf(customInt0)+","+String.valueOf(customInt1)+","
        +String.valueOf(customInteger0)+","+String.valueOf(customInteger1)
); 
//V: lets see what outputs, null is usually converted to the word 'null' by the String.valueOf method :0,2,0,2,null,2
于 2017-02-09T21:33:26.870 回答
-2

if (cursor.getInt(5) !=0){

cust.daysSinceJoining = cursor.getInt(5);

}

或者

int index = cursor.getColumnIndex(KEY_NAME);
Integer x = null;
if (!cursor.isNull(index)
   cust.daysSinceJoining = cursor.getInt(5);
}

请参阅文档 getInt() 文档:

于 2013-08-05T09:08:28.440 回答