gpt4 book ai didi

android - Android SQLite 中的选择语句

转载 作者:行者123 更新时间:2023-11-29 22:09:06 25 4
gpt4 key购买 nike

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

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);
}
});
}

IngredientHelper.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 中查找用户的文本?谢谢

最佳答案

适用标准 SQL,使用 OR:

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

请注意 = 将进行精确匹配,您可能需要查看 LIKE运营商。

关于android - Android SQLite 中的选择语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10081254/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com