문제

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

도움이 되었습니까?

해결책

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.

다른 팁

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");
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top