1

我有一个包含几列的数据库表。我想通过比较用户输入的字符串来选择某些行,并根据他们的输入选择记录。我有以下代码:

SearchbyIngredient.Java

    final Button searchbutton1 = (Button) findViewById(R.id.searchbutton1);
searchbutton1.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {
Cursor temp = dbIngredientHelper.getDrinks(textView.getText().toString());
        String ingred = temp.getString(0);
        Context context = getApplicationContext();
        iadapter = new IngredientAdapter(temp);
        myListView.setAdapter(iadapter);
    }
});
}

成分助手.Java

public Cursor getDrinks(String drinkName) throws SQLException{
    SQLiteQueryBuilder queryBuilder = new SQLiteQueryBuilder();
    queryBuilder.setTables(TABLE_NAME2);
    String selection = COLUMN_INGRED1 + "= ' " + drinkName + "'";
    String[] asColumnsToReturn = new String[] { COLUMN_ID2, COLUMN_DRINKNAME, COLUMN_INGRED1, COLUMN_AMT1, COLUMN_INGRED2, COLUMN_AMT2, COLUMN_INGRED3, COLUMN_AMT3, COLUMN_INGRED4, COLUMN_AMT4 };
    Cursor mCursor2 = queryBuilder.query(dbSqlite, asColumnsToReturn, selection, null, null, null, COLUMN_DRINKNAME);
    /*Cursor mCursor = dbSqlite.query(true, TABLE_NAME2, new String[]{
            COLUMN_DRINKNAME}, 
            COLUMN_INGRED1 + "= ' " + drinkName + "'", null, null, null, null, null);
    if (mCursor != null){
        mCursor.moveToFirst();
    }*/
    mCursor2.moveToFirst();
    return mCursor2;
}

我的问题是关于

String selection = COLUMN_INGRED1 + "= ' " + drinkName + "'";

此代码当前有效,但是它仅在 COLUMN_INGRED1 中找到包含用户文本的条目(代码中只有一个列)。如何格式化语句或制作一系列语句,以便它还在 COLUMN_INGRED2、COLUMN_INGRED3 和 COLUMN_INGRED4 中查找用户的文本?谢谢

4

1 回答 1

2

适用标准 SQL,使用 OR:

   COLUMN_INGRED1 + "= ' " + drinkName + "' OR " COLUMN_INGRED2 + "= '" +drinkName + "'" ...;

请注意,=将进行完全匹配,您可能需要查看LIKE运算符。

于 2012-04-10T00:05:06.683 回答