在android中从sqlite获取空整数

在android中从sqlite获取空整数,android,sqlite,Android,Sqlite,我有一个问题,实际上可能是一个设计问题,但我正在努力找到一个解决办法 在android应用程序中的sqlite数据库中,我有一个名为Customer的表。它有大约40列不同的字符串和int数据类型。在实践中,这些列中的任何一列都可能为空,也可能不为空 在我的代码中,我有一个名为getCustomer()的函数,该函数查询数据库中的特定客户,并将其所有单元格从数据库中放入customer类,该类包含每列的变量。然后,我可以根据自己的意愿传递客户对象。函数返回这个Customer对象 我的问题是整数

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

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

在我的代码中,我有一个名为
getCustomer()
的函数,该函数查询数据库中的特定客户,并将其所有单元格从数据库中放入
customer
类,该类包含每列的变量。然后,我可以根据自己的意愿传递客户对象。函数返回这个
Customer
对象

我的问题是整数可能是空的。我熟悉java中
int
如何不能为null,但
Integer
如何可以为null。但我的问题实际上在于数据库中的单元格可以是空的(例如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值可能为null时,如何从sqlite数据库检索int?

您可以尝试
isNull()
函数。

如果(cursor.getInt(5)!=0){

}

请参阅文档

@sanders的方法是正确的,下面是您如何编辑代码以使用它:

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

请看一下这个答案:

我认为这应该对你有用:

int lIndex = cursor.getColumnIndexOrThrow(COLUMN);
Integer lInteger = null;
if (!cursor.isNull(lIndex)
lInteger = cursor.getInt(lIndex);

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

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

检查cursor.getInt(5)的值是多少…我认为它是0。如果为零,则根据INT默认值为0继续操作,因此您应该检查获取值是否等于zeor这没有帮助
cursor.getInt()
返回一个
int
值,当放入
Integer
中时,它将始终为非
null
。另外,
Integer
没有
isNull()
方法。我不知道为什么我会得到负评分。任何人请解释为什么我的答案是正确的
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