如何在Android数据库查询&中搜索NULL选择中的OR语句

时间:2013-01-05 19:01:00

标签: android null where

关于数据库查询的几个问题:

所以我有以下数据库查询,它有一个WHERE语句,其中包含三个String变量(rackingSystem,制造商,“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;
}

问题1:

如何搜索NULL,因为在上面的示例中,制造商在数据库中可能为NULL(因为用户可能没有在该数据库中输入任何内容)。

问题2:

我可以在WHERE语句中包含OR语句吗?所以目前我正在搜索所有“Amber Risk”的记录,但我还需要在此列表中包含“Red Risk”条目。如果我可以这样做,我该如何编辑WHERE语句?

1 个答案:

答案 0 :(得分:6)

  

如何搜索NULL,因为在上面的示例中,制造商在数据库中可能为NULL

使用MANUFACTURER + " IS NULL"

  

我可以在WHERE语句中包含OR语句吗?

是。让我们搜索空或空制造商:

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

  

所以目前我正在搜索所有" Amber Risk"的记录,但我还需要在此列表中包含&​​#34; Red Risk"。

INSPECTION_LINK + " = " + inspectionId + " AND " + RACKING_SYSTEM + " = ? AND " + 
        MANUFACTURER + " IS NULL AND (" + RISK + " = ? OR " + RISK + " = ?)", 
new String[] {rackingSystem, manufacturer, "Amber Risk", "Red Risk"},