3

我无法使用 Android 从 SQLite 数据库获取空值。

我的数据库有 10 列。从第 1 列到第 8 列都填充了值。但是有几行第 9 列和第 10 列中的值为空或某个数字。

我想检查:

String selectQuery = "SELECT * FROM products WHERE barcodeId='" + id + "'";

Cursor myCursor = db.rawQuery(selectQuery, null);
if (myCursor.moveToFirst()) {

    //checking to see if there is something in column 9
    if(myCursor.getString(8) != null){
        // do soemthing
    }else {
        // if there is nothing do something
    }

    //checking to see if there is something in column 10
    if(myCursor.getString(9) != null){
        // do soemthing
    }else {
        // if there is nothing do something
    }
}

有人可以根据我的示例在我的表中的第 9 列和第 10 列中提供工作代码吗?

一个简短的解释也将受到欢迎。谢谢

4

2 回答 2

7

我不知道 Cursor.getString() 是否会返回 null。文档没有说,它可能只是返回一个空字符串。您应该使用Cursor.isNull()来检查列中的空值。

于 2014-02-15T22:21:49.697 回答
3

感谢DavidCAdams解决了我的问题。

如果有人对此感兴趣,这是我的代码。

if(myCursor.isNull(8)){
    Log.w("No value", "Cell is empty");
}else{
    Log.w("Value is present", "There is a value");
}   

if(myCursor.isNull(9)){
    Log.w("No value", "Cell is empty");
}else{
    Log.w("Value is present", "There is a value");
}
于 2014-02-15T22:37:34.437 回答