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.
How to get null value from SQLite database in Android
-
12-10-2022 - |
题
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
解决方案
其他提示
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");
}
不隶属于 StackOverflow