2014-02-15 62 views
1

我无法从使用Android的SQLite数据库中获取空值。如何从Android中的SQLite数据库获得空值

我的数据库有10列。从第1列到第8列,所有数据都填入数值。但有几列的第9列和第10列的值为空或某个数字。

我要检查:根据我在我的表列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 
    } 
} 

能有人提供了一个工作代码。

简单的解释也将受到欢迎。 谢谢

回答

3

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

1

谢谢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"); 
} 
+0

欢迎来到stackoverflow –

相关问题