2014-02-15 61 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 –

相關問題