【发布时间】:2021-01-18 22:40:01
【问题描述】:
任何人都可以在这里查看我的查询是否有错误,这是我第一次尝试多个联接,下面是 logcat 错误。提前致谢
android.database.sqlite.SQLiteException:靠近“FROM”:语法错误 (代码 1 SQLITE_ERROR[1]):,编译时:SELECT SUM(A.quantity), A.ingredient, A.recipe, B.ingredient_type, B.measurement_name, C.id, D.plan_name, FROM QUANTITY AS A JOIN INGREDIENTS AS B ON A.ingredient = B.ingredient_name 按 A.recipe 加入计划食谱 = C.recipe_name 按 C.id 加入 MEAL_PLAN = D.plan_recipe 按 A.ingredient 分组 D.plan_name LIKE 在哪里?
代码
public void loadIngredient() {
shopList.clear();
db = (new DatabaseManager(this).getWritableDatabase());
String RECIPE_SEARCH = " SELECT SUM(A.quantity), A.ingredient, A.recipe, B.ingredient_type, B.measurement_name, C.id, D.plan_name, " +
"FROM " + DatabaseManager.TABLE_QUANTITY + " AS A JOIN " + DatabaseManager.TABLE_INGREDIENTS +
" AS B ON A.ingredient = B.ingredient_name" + " JOIN " + DatabaseManager.TABLE_PLAN_RECIPES + " AS C ON A.recipe = C.recipe_name " +
" JOIN " + DatabaseManager.TABLE_MEAL_PLAN + " AS D ON C.id = D.plan_recipe GROUP BY A.ingredient";
String selectQuery = "";
selectQuery = RECIPE_SEARCH + " WHERE D.plan_name LIKE ?";
c = db.rawQuery(selectQuery, new String[]{"%" + search + "%"});
if (c.moveToFirst()) {
do {
Shopping_List shopping_list = new Shopping_List();
shopping_list.setQuantity(c.getDouble(c.getColumnIndex("quantity")));
shopping_list.setIngredient_name(c.getString(c.getColumnIndex("ingredient_name")));
shopping_list.setIngredient_type(c.getString(c.getColumnIndex("ingredient_type")));
shopping_list.setMeasurement_name(c.getString(c.getColumnIndex("measurement_name")));
shopList.add(shopping_list);
} while (c.moveToNext());
c.close();
}
}
【问题讨论】:
-
去掉
D.plan_name字符串RECIPE_SEARCH之后的,。 -
android.database.sqlite.SQLiteException: near "WHERE": syntax error (code 1 SQLITE_ERROR[1]): ,同时编译:SELECT SUM(A.quantity), A.ingredient, A. recipe, B.ingredient_type, B.measurement_name, C.id, D.plan_name FROM QUANTITY AS A JOIN INGREDIENTS AS B ON A.ingredient = B.ingredient_name JOIN PLAN_RECIPES AS C ON A.recipe = C.recipe_name JOIN MEAL_PLAN AS D ON C.id = D.plan_recipe GROUP BY A.ingredient WHERE D.plan_name LIKE ?
-
将 WHERE 子句放在 GROUP BY 子句之前。
-
无法从有 34 行 7 列的 CursorWindow 中读取第 0 行第 -1 列。
-
看我的回答...
标签: java android sql sqlite android-sqlite