How to look for NULL in Android database query and OR in select

A few queries regarding a database query:

So, I have the following database query that contains a WHERE statement that includes three string variables (rackingSystem, manufacturer, Amber Risk).

public Cursor fetchComponentsAndSpecForManufacturer(long inspectionId, String rackingSystem, String manufacturer) { Cursor mCursor = rmDb.query(true, DAMAGED_COMPONENTS_TABLE, new String[] { DAMAGED_COMPONENT_ID, LOCATION_LINK, RUN_LINK, AREA_LINK, INSPECTION_LINK, LOCATION_REF, RACKING_SYSTEM, COMPONENT, COMPONENT_TYPE, QUANTITY, POSITION, RISK, ACTION_REQUIRED, NOTES_GENERAL, MANUFACTURER, TEXT1, TEXT2, TEXT3, TEXT4, NOTES_SPEC, SPEC_SAVED}, INSPECTION_LINK + " = " + inspectionId + " AND " + RACKING_SYSTEM + " = ? AND " + MANUFACTURER + " = ? AND " + RISK + " = ? ", new String[] {rackingSystem, manufacturer, "Amber Risk"}, COMPONENT_TYPE + ", " + TEXT1 + ", " + TEXT2 + ", " + TEXT3 + ", " + TEXT4 + ", " + NOTES_SPEC, null, null, null); if (mCursor != null) { mCursor.moveToFirst(); } return mCursor; } 

Question 1:

How to search for NULL as, in the above example, the manufacturer may be NULL in the database (since the user may not have added anything to the database for this bit yet).

Question 2:

Can I include an OR statement in a WHERE statement? Therefore, at the moment I am surfing for all records that are “Amber Risk”, but I also need to include “Red Risk” records in this list. If I can do this, how can I change the WHERE statement?

+3
android null where
Jan 05 '13 at 19:00
source share
1 answer

How to search for NULL as, in the above example, the manufacturer may be NULL in the database

Use MANUFACTURER + " IS NULL" .

Can I include an OR operator in a WHERE clause?

Yes. Let the search for zero or empty manufacturers:

 MANUFACTURER + " IS NULL OR " + MANUFACTURER + " = ''" 



So, at the moment I’m doing the earring for all records that are “Amber Risk”, but I also need to include “Red Risk” records in this list.

 INSPECTION_LINK + " = " + inspectionId + " AND " + RACKING_SYSTEM + " = ? AND " + MANUFACTURER + " IS NULL AND (" + RISK + " = ? OR " + RISK + " = ?)", new String[] {rackingSystem, manufacturer, "Amber Risk", "Red Risk"}, 
+6
Jan 05 '13 at 19:03
source share