Android SQLite:无法在索引1处绑定参数,因为索引超出范围.该语句有0个参数



在尝试使数据库工作时遇到了几个问题,现在我收到了以下错误

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.stu54259.plan2cook/com.stu54259.plan2cook.Recipe}: java.lang.IllegalArgumentException: Cannot bind argument at index 1 because the index is out of range.  The statement has 0 parameters.

Recipe.java

package com.stu54259.plan2cook;
import android.annotation.SuppressLint;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.AsyncTask;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.recyclerview.widget.DefaultItemAnimator;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.google.android.material.tabs.TabLayout;
import com.stu54259.plan2cook.Model.RecipeList;
import com.stu54259.plan2cook.database.DatabaseManager;
import java.util.ArrayList;
import java.util.List;
import static com.stu54259.plan2cook.database.DatabaseManager.*;
public class Recipe extends MainActivity {
TabLayout tabLayout;
ImageView recipeImage;
TextView descriptionText, courseText, servingsText, costText, caloriesText, methodText;
RecyclerView listIngredient;
SQLiteDatabase db;
String search_name;
Cursor c;
RecyclerViewAdapter adapterRecipe;
List<RecipeList> itemRecipe = new ArrayList<>();
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.recipe);
//search_name = getIntent().getStringExtra("NAME");
search_name = "Speedy chicken couscous";
loadRecipe();
//recyclerview Recipe
adapterRecipe = new RecyclerViewAdapter(this, itemRecipe);
listIngredient = findViewById(R.id.listIngredient);
RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(this,
LinearLayoutManager.VERTICAL, false);
listIngredient.setLayoutManager(mLayoutManager);
listIngredient.setItemAnimator(new DefaultItemAnimator());
listIngredient.setAdapter(adapterRecipe);
}
public void loadRecipe() {
itemRecipe.clear();
db = (new DatabaseManager(this).getWritableDatabase());
String RECIPE_SEARCH = " SELECT A.ingredient_quantity, B.measurement_name, B.ingredient_name, B.description " +
"FROM " + DatabaseManager.TABLE_QUANTITY + " AS A JOIN " + DatabaseManager.TABLE_INGREDIENTS +
" AS B ON A.ingredient = B.ingredient_name";
c = db.rawQuery(RECIPE_SEARCH, new String[]{"%" + search_name + "%"});
if (c.moveToFirst()) {
do {
RecipeList recipeList = new RecipeList();
recipeList.setId(c.getInt(c.getColumnIndex("COL_ID")));
recipeList.setIngredient_amount(c.getString(c.getColumnIndex("COL_INGREDIENT_QUANTITY")));
recipeList.setMeasurement_name(c.getString(c.getColumnIndex("COL_MEASUREMENT_NAME")));
recipeList.setIngredient_name(c.getString(c.getColumnIndex("COL_INGREDIENT_NAME")));
recipeList.setDescription(c.getString(c.getColumnIndex("COL_DESCRIPTION")));
itemRecipe.add(recipeList);
} while (c.moveToNext());
c.close();
}
}
}

如果需要,可以提供更多的片段,但我不知道这意味着什么,也不知道如何解决?似乎发生在这一行c=db.rawQuery(RECIPE_SEARCH,new String[]{"%"+SEARCH_name+"%"}(;

更新

按照建议进行了这些修改,但现在出现了这个错误从CursorWindow中读取第0行第-1列失败,CursorWindow有15行5列。无法从CursorWindow中读取第0行第1列。在从游标访问数据之前,请确保游标已正确初始化。

public void loadRecipe() {
itemRecipe.clear();
db = (new DatabaseManager(this).getWritableDatabase());
String RECIPE_SEARCH = " SELECT A.recipe, A.ingredient_quantity, B.measurement_name, B.ingredient_name, B.description " +
"FROM " + DatabaseManager.TABLE_QUANTITY + " AS A JOIN " + DatabaseManager.TABLE_INGREDIENTS +
" AS B ON A.ingredient = B.ingredient_name";
String selectQuery = "";
selectQuery = RECIPE_SEARCH + " WHERE A.recipe LIKE ?";
c = db.rawQuery(selectQuery, new String[]{"%" + search_name + "%"});
if (c.moveToFirst()) {
do {
RecipeList recipeList = new RecipeList();
recipeList.setId(c.getInt(c.getColumnIndex("COL_ID")));
recipeList.setIngredient_amount(c.getString(c.getColumnIndex("COL_INGREDIENT_QUANTITY")));
recipeList.setMeasurement_name(c.getString(c.getColumnIndex("COL_MEASUREMENT_NAME")));
recipeList.setIngredient_name(c.getString(c.getColumnIndex("COL_INGREDIENT_NAME")));
recipeList.setDescription(c.getString(c.getColumnIndex("COL_DESCRIPTION")));
itemRecipe.add(recipeList);
} while (c.moveToNext());
c.close();
}
}

您的查询列名与您试图使用游标检索的列名不同。

还要注意,游标是区分大小写的,因此列应该与物理表中的列完全相同

您还应该向查询中添加所需的where子句,如Where ColA like '%?%'

现在已经解决了这个问题,对toString和布局xml进行了调整,再加上这里提供的帮助,已经修复了这个问题。非常感谢,我相信我会有更多的。

最新更新