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

Android SQLite: Cannot bind argument at index 1 because the index is out of range. The statement has 0 parameters

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

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 + "%"});

更新

按照建议进行了这些修改,但现在出现此错误 无法从具有 15 行、5 列的 CursorWindow 读取第 0 行、第 -1 列。 无法从 CursorWindow 读取第 0 行,col -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();
    }

}

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

另请注意,游标区分大小写,因此列应与物理中的列完全相同 table

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

现在已经解决了 toString 和布局的调整 xml 结合此处提供的帮助解决了这个问题。非常感谢,我相信我还会有更多。