Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android SQLite query where column is not null and not empty

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?

like image 921
cfrz Avatar asked Jul 12 '15 21:07

cfrz


People also ask

Where column is not NULL SQLite?

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.

Is NULL or empty in SQLite?

SQLite IS NOT NULL operator.

How check NULL values in all columns in SQLite?

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) + ...

How to use is NOT NULL in Android SQLite?

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.

Can a prime key be null in SQLite?

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.

How to where clause in Android SQLite?

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.

How do you check if a value is null in SQL?

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.


2 Answers

I believe the correct syntax would be:

AND key IS NOT NULL AND key != ""

where key is your column

like image 110
BladeCoder Avatar answered Oct 17 '22 05:10

BladeCoder


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," ") != ""
like image 5
Hugo Matilla Avatar answered Oct 17 '22 06:10

Hugo Matilla