Android SQLite query where column is not null and not empty

落花浮王杯 提交于 2019-12-20 12:28:13

问题


I cannot figure out the syntax for the .query call. I need to select all records that match a certain column that do not have a null or empty value for a second (different) column

my best attempt:

Cursor cursor = mDatabase.query(DatabaseOpenHelper.TABLE_ROOMS, mAllColumns,
    DatabaseOpenHelper.KEY_ROOM_HOSPITAL_ID
    + " =? AND " + DatabaseOpenHelper.KEY_ISO + " IS NOT NULL OR NOT ?",
    new String[]{String.valueOf(hospitalId), ""}, null, null, null);

This is returning ALL records. If I use AND in place of OR, it returns records matching hospitalId, but ignores the NOT NULL OR NOT "" part.

Any tips? Should I use a rawQuery call?


回答1:


I believe the correct syntax would be:

AND key IS NOT NULL AND key != ""

where key is your column




回答2:


If multiple spaces can be in your column and you also want to treat them as empty values use TRIM

column_name IS NOT NULL AND TRIM(column_name," ") != ""


来源:https://stackoverflow.com/questions/31372918/android-sqlite-query-where-column-is-not-null-and-not-empty

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!