Android SQL 查詢null值

在SQLlite語法中,NULL表示一個缺失數值的值,與空白不同。空白是不佔用空間的,而NULL值是佔用空間的。

在Android的寫法中,若DB欄位值為NULL,使用以下的方式是無法查詢到的。

1
2
3
4
ContentResolver resolver = myContext.getContentResolver();
int count = resolver.delete(getDataUri(), Contract.DataColumns.TIME + "=? ", new String[] { null });

Log.e(TAG, "count of Null Item: " + count);

可使用

1
column_name is null or column_name=""

的方式查詢NULL值

1
2
3
4
ContentResolver resolver = myContext.getContentResolver();
int count = resolver.delete(getDataUri(), Contract.DataColumns.TIME + " is null or " + Contract.DataColumns.TIME + " =? ", new String[] { "" });

Log.e(TAG, "count of Null Item: " + count);