SQLite异常:在”FROM”附近:多重连接的语法错误

huangapple go评论89阅读模式
英文:

SQLiteException: near "FROM": syntax error multiple Joins

问题

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")));
            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();
    }
}
英文:

Can anyone see if there is an error in my query here, it's my first attempt at multiple Joins, below is the logcat error. Thanks in advance

> android.database.sqlite.SQLiteException: near "FROM": syntax error
> (code 1 SQLITE_ERROR[1]): , while compiling: 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 ?

Code

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

}

答案1

得分: 2

以下是翻译好的内容:

你的代码中存在几个问题。第一个问题是在变量 RECIPE_SEARCH 中,在 FROM 子句之前,你必须移除 D.plan_name 后面的逗号。第二个问题是 WHERE 子句必须位于 GROUP BY 子句之前。第三个问题是你必须为查询返回的列 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();
}

此外,你的查询返回了在循环中未使用的列,我没有将其删除,但你可以自行删除。

英文:

There are several problems in your code.<br/>

The 1st is a comma that you must remove after D.plan_name inside the variable RECIPE_SEARCH right before the FROM clause.<br/>

The 2nd is the WHERE clause that must precede the GROUP BY clause.<br/>

The 3d is that you must alias the column SUM(A.quantity) that is returned by your query so you can retrieve it by that alias, say quantity.

The 4th is that there is no column ingredient_name returned by your query, but I assume this is the column A.ingredient which should be aliased to ingredient_name.

So change to this:

public void loadIngredient() {
    shopList.clear();
    db = (new DatabaseManager(this).getWritableDatabase());
    String RECIPE_SEARCH = 
        &quot;SELECT SUM(A.quantity) quantity, A.ingredient ingredient_name, A.recipe, B.ingredient_type, B.measurement_name, C.id, D.plan_name &quot; +
        &quot;FROM &quot; + DatabaseManager.TABLE_QUANTITY + &quot; AS A JOIN &quot; + DatabaseManager.TABLE_INGREDIENTS + &quot; AS B ON A.ingredient = B.ingredient_name &quot; + 
        &quot;JOIN &quot; + DatabaseManager.TABLE_PLAN_RECIPES + &quot; AS C ON A.recipe = C.recipe_name &quot; +
        &quot;JOIN &quot; + DatabaseManager.TABLE_MEAL_PLAN + &quot; AS D ON C.id = D.plan_recipe &quot; +
        &quot;WHERE D.plan_name LIKE ? GROUP BY A.ingredient&quot;;   
    c = db.rawQuery(RECIPE_SEARCH, new String[]{&quot;%&quot; + search + &quot;%&quot;});

    if (c.moveToFirst()) {
        do {
            Shopping_List shopping_list = new Shopping_List();
            shopping_list.setQuantity(c.getDouble(c.getColumnIndex(&quot;quantity&quot;)));
            shopping_list.setIngredient_name(c.getString(c.getColumnIndex(&quot;ingredient_name&quot;)));
            shopping_list.setIngredient_type(c.getString(c.getColumnIndex(&quot;ingredient_type&quot;)));
            shopping_list.setMeasurement_name(c.getString(c.getColumnIndex(&quot;measurement_name&quot;)));
            shopList.add(shopping_list);
        } while (c.moveToNext());
    }
    c.close();
    db.close();
}

Also, your query returns columns that you don't use in the loop, which I did not remove, by you may remove them.

huangapple
  • 本文由 发表于 2020年10月3日 22:31:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/64185332.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定