How to get null value from SQLite database in Android

NewestStackOverflowUser picture NewestStackOverflowUser · Feb 15, 2014 · Viewed 7.1k times · Source

I am having trouble getting a null value from SQLite database using Android.

My database has 10 columns. From column 1 to 8 all are filled with values. But there are several rows where the values in column 9 and column 10 are either null or some number.

I want to check:

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
    }
}

Can some one provide a working code according to my example with column 9 and 10 in my table.

A brief explanation will also be welcomed. Thanks

Answer

David C Adams picture David C Adams · Feb 15, 2014

I don't know if Cursor.getString() will return a null or not. The docs don't say, it may just return an empty string. You should use Cursor.isNull() instead to check for a null value in your column.