1

我正在尝试使用以下命令在 Android 中执行查询:return query(selection, null, null, null),但在使用多个运算符和括号时无法返回所需的结果。这就是我想要做的:

    selection = "(KEY_VARIABLE > '5' AND KEY_VARIABLE2 = 'Yes') OR (KEY_VARIABLE < '5' AND KEY_VARIABLE2 = 'No')"

但是,该查询返回 0 个结果,因为该查询无法识别括号。

有没有一种方法可以在 Android 中使用多个运算符和一个嵌入在运算符语句中的运算符来形成查​​询?

更新:logcat 中也不例外。一切正常,除了嵌入式 OR(AND) 语句。这是代码:

 public Cursor getTierSchools() {

    String range = "";
    String range2 = "";
    String range3 = "";
    double score = LabValues.myscore;
    double scoreplustwo = score + 2;
    double scoreminustwo = score - 2;
    double scoreplusfive = score + 5;
    double scoreminusfive = score - 5;
    double scoreminusone = score - 1;

    range = "( " + KEY_SCHOOLSCORE + " > " + "'" + scoreplusfive + "'" + " AND " + KEY_SCHOOLSTATEBIAS + " = 'Yes' AND " + KEY_SCHOOLSTATELONG + " = " + "'" + LabValues.mystate + "'" + " )";
    range2 = " OR " + "( " + KEY_SCHOOLSCORE + " > " + "'" + scoreminusone + "'" + " AND " + KEY_SCHOOLSTATEBIAS + " = 'Yes' AND " + KEY_SCHOOLSTATELONG + " != " + "'" + LabValues.mystate + "'" + " )";
    range3 = " OR " + "( " + KEY_SCHOOLSCORE + " > " + "'" + scoreplustwo + "'" + " AND " + KEY_SCHOOLSTATEBIAS + " = 'No'" + " )";         

    String selection = range + range2 + range3;
    return query(selection, null, null, sorting);

}

private Cursor query(String selection, String[] selectionArgs, String[] columns, String sortOrder) {
    SQLiteQueryBuilder builder = new SQLiteQueryBuilder();
    builder.setTables(FTS_VIRTUAL_TABLE);
    builder.setProjectionMap(mColumnMap);

    Cursor cursor = builder.query(mDatabaseOpenHelper.getReadableDatabase(),
            columns, selection, selectionArgs, null, null, sortOrder);

    if (cursor == null) {
        return null;
    } else if (!cursor.moveToFirst()) {
        cursor.close();
        return null;
    }
    return cursor;
}

基本上,我试图在括号内整合 AND 语句,类似于你如何形成一个等式: (4+4)/8 = 1 IS NOT EQUAL TO 4+4/8 = 4.5 。

解决方案:

    range = "(( " + KEY_SCHOOLSCORE + " > " + "'" + scoreplusfive + "'" + " AND " + KEY_SCHOOLSTATEBIAS + " = 'Yes' AND " + KEY_SCHOOLSTATELONG + " = " + "'" + LabValues.mystate + "'" + " )";
    range2 = " OR " + "( " + KEY_SCHOOLSCORE + " > " + "'" + scoreminusone + "'" + " AND " + KEY_SCHOOLSTATEBIAS + " = 'Yes' AND " + KEY_SCHOOLSTATELONG + " != " + "'" + LabValues.mystate + "'" + " )";
    range3 = " OR " + "( " + KEY_SCHOOLSCORE + " > " + "'" + scoreplustwo + "'" + " AND " + KEY_SCHOOLSTATEBIAS + " = 'No'" + " ))";        
4

0 回答 0