【问题标题】:SQLiteException: near "FROM": syntax error multiple JoinsSQLiteException:在“FROM”附近:语法错误多个连接
【发布时间】: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


【解决方案1】:

您的代码中有几个问题。

第一个是逗号,必须在 D.plan_name 之后的变量 RECIPE_SEARCH 中删除,就在 FROM 子句之前。

第二个是WHERE 子句,必须在GROUP BY 子句之前。

3d 是您必须为查询返回的列 SUM(A.quantity) 设置别名,以便您可以通过该别名检索它,例如 quantity

第四个是您的查询没有返回列ingredient_name,但我假设这是列A.ingredient,应该别名为ingredient_name

所以改成这样:

public void loadIngredient() {
    shopList.clear();
    db = (new DatabaseManager(this).getWritableDatabase());
    String RECIPE_SEARCH = 
        "SELECT SUM(A.quantity) quantity, A.ingredient ingredient_name, 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 " +
        "WHERE D.plan_name LIKE ? GROUP BY A.ingredient";   
    c = db.rawQuery(RECIPE_SEARCH, 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();
    db.close();
}

此外,您的查询会返回您未在循环中使用的列,我没有将其删除,您可以将它们删除。

【讨论】:

  • 我仍然无法从 CursorWindow 读取第 0 行,第 -1 行。确保在从光标访问数据之前正确初始化光标。
  • 再次检查列名/别名。它们必须与循环内的那些完全相同:quantity, ingredient_name, ingredient_type, measurement_name。您可以每次删除 1 以找出哪个不相同。
  • 我看不出这些有什么问题
  • 您是否照原样复制代码?您是否尝试从循环中一次删除 1 列?首先删除shopping_list.setQuantity(c.getDouble(c.getColumnIndex("quantity"))); 并运行。然后删除shopping_list.setIngredient_name(c.getString(c.getColumnIndex("ingredient_name")));
  • 为什么把别名改成ingredient_quantity?在这一行中:shopping_list.setQuantity(c.getDouble(c.getColumnIndex("quantity"))); 您正在尝试使用名称 quantity 检索它。
猜你喜欢
  • 1970-01-01
  • 2020-05-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-01-11
  • 2013-03-13
  • 1970-01-01
相关资源
最近更新 更多