Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/214.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sqlite/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/amazon-s3/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android SQLite:无法在索引1处绑定参数,因为该索引超出范围。该语句有0个参数_Android_Sqlite_Bind - Fatal编程技术网

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

Android SQLite:无法在索引1处绑定参数,因为该索引超出范围。该语句有0个参数,android,sqlite,bind,Android,Sqlite,Bind,在尝试使数据库工作时遇到了几个问题现在我遇到了以下错误 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 rang

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

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

    }
}

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

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


您还应该向查询中添加所需的where子句,如“%”?%”

这一问题现在已经解决,对toString和layout xml进行了调整,并结合此处提供的帮助解决了此问题。非常感谢,我相信我还会有更多的内容。

配方搜索查询不应该有一个where子句,其中包含要格式化的参数吗?
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();
    }

}