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?
Introduction to SQLite NOT NULL constraint Based on the SQL standard, PRIMARY KEY should always imply NOT NULL . However, SQLite allows NULL values in the PRIMARY KEY column except that a column is INTEGER PRIMARY KEY column or the table is a WITHOUT ROWID table or the column is defined as a NOT NULL column.
SQLite IS NOT NULL operator.
To do it all in one step you can use something like: SELECT SUM(CASE COLUMN1 WHEN NULL THEN 1 ELSE 0 END) + SUM(CASE COLUMN2 WHEN NULL THEN 1 ELSE 0 END) + SUM(CASE COLUMN3 WHEN NULL THEN 1 ELSE 0 END) + ...
This example demonstrate about How to use IS NOT NULL in Android sqlite Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main.xml.
Based on the SQL standard, PRIMARY KEY should always imply NOT . However, SQLite allows NULL values in the PRIMARY KEY column except that a column is INTEGER PRIMARY KEY column or the table is a WITHOUT ROWID table or the column is defined as a NOT NULL column. This is due to a bug in some early versions.
This example demonstrate about How to where Clause in Android sqlite. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main.xml.
To check if a value is NULL or not, you use the IS NULL operator instead: { column | expression } IS NULL; Code language: SQL (Structured Query Language) (sql) The IS NULL operator returns 1 if the column or expression evaluates to NULL.
I believe the correct syntax would be:
AND key IS NOT NULL AND key != ""
where key
is your column
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," ") != ""
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With