3

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

2 Answers 2

7

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.

Sign up to request clarification or add additional context in comments.

Comments

3

Thanks DavidCAdams that fixed my problem.

Here is my code if someone is interested in it.

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.