【发布时间】:2020-09-18 22:39:48
【问题描述】:
在尝试使数据库正常工作时遇到了几个问题我收到以下错误
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();
}
}
}
如有必要,可以提供更多的 sn-ps,但我不知道这意味着什么或如何解决?似乎发生在这一行 c = db.rawQuery(RECIPE_SEARCH, new String[]{"%" + search_name + "%"});
更新
按照建议进行了这些修改,但现在出现此错误 无法从具有 15 行 5 列的 CursorWindow 读取第 0 行第 -1 列。 无法从 CursorWindow 读取第 0 行 col -1。确保 Cursor 在从中访问数据之前已正确初始化。
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 子句吗?