Android没有这张表格,错误代码1 SQLITE_ERROR。

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

Android No such table code 1 SQLITE_ERROR

问题

我在一个SQL查询上遇到了困难:

"SELECT A.ingredient_quantity, B.measurement_name, B.ingredient_name, B.description " + 
"FROM TABLE_QUANTITY JOIN TABLE_INGREDIENT ON A.ingredient = B.ingredient_name"

这告诉我:

no such table: TABLE_QUANTITY (code 1 SQLITE_ERROR)

这些表是:

private static final String CREATE_TABLE_QUANTITY = "CREATE TABLE " + TABLE_QUANTITY + "(" + COL_ID +
     " INTEGER PRIMARY KEY, AUTO INCREMENT," + COL_INGREDIENT_QUANTITY + " NUMERIC, " +
     COL_RECIPE + " TEXT," + COL_INGREDIENT + " TEXT, FOREIGN KEY (" + COL_RECIPE + ") " +
     "REFERENCES " + TABLE_RECIPE + "(" + COL_RECIPE_NAME + "), FOREIGN KEY (" + COL_INGREDIENT + ") " +
     "REFERENCES " + TABLE_INGREDIENTS + "(" + COL_INGREDIENT_NAME + "))";

private static final String CREATE_TABLE_INGREDIENTS = "CREATE TABLE " + TABLE_INGREDIENTS + "(" +
     COL_ID + " INTEGER PRIMARY KEY, AUTO INCREMENT," + COL_INGREDIENT_NAME + " TEXT," +
     COL_DESCRIPTION + " TEXT," + COL_MEASUREMENT + " TEXT," + COL_INGREDIENT_TYPE + " TEXT, " +
     "FOREIGN KEY (" + COL_MEASUREMENT + ") REFERENCES " + TABLE_MEASUREMENT + "(" + COL_MEASUREMENT_NAME + "), " +
     "FOREIGN KEY (" + COL_INGREDIENT_TYPE + ") REFERENCES " + TABLE_INGREDIENT_TYPE + "(" + COL_TYPE_NAME + "))";

更新

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";
    String selectQuery = "";
    selectQuery = selectQuery + RECIPE_SEARCH;

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

根据建议进行了一些更改,谢谢。然而仍然出现以下错误:

Caused by: android.database.sqlite.SQLiteException: no such table: QUANTITY (code 1 SQLITE_ERROR): , while compiling: SELECT + A.ingredient_quantity, B.measurement_name, B.ingredient_name, B.description FROM QUANTITY AS A JOIN INGREDIENTS AS B ON A.ingredient = B.ingredient_name
英文:

I'm struggling on an sql query I have: 

"SELECT A.ingredient_quantity, B.measurement_name, B.ingredient_name, B.description " + 
"FROM TABLE_QUANTITY JOIN TABLE_INGREDIENT ON A.ingredient = B.ingredient_name"

This is telling me:

> no such table: TABLE_QUANTITY (code 1 SQLITE_ERROR)

The tables are:

private static final String CREATE_TABLE_QUANTITY = "CREATE TABLE " + TABLE_QUANTITY + "(" + COL_ID +
     " INTEGER PRIMARY KEY, AUTO INCREMENT," + COL_INGREDIENT_QUANTITY + " NUMERIC, " +
     COL_RECIPE + " TEXT," + COL_INGREDIENT + " TEXT, FOREIGN KEY (" + COL_RECIPE + ") " +
     "REFERENCES " + TABLE_RECIPE + "(" + COL_RECIPE_NAME + "), FOREIGN KEY (" + COL_INGREDIENT + ") " +
     "REFERENCES " + TABLE_INGREDIENTS + "(" + COL_INGREDIENT_NAME + "))";

private static final String CREATE_TABLE_INGREDIENTS = "CREATE TABLE " + TABLE_INGREDIENTS + "("
     + COL_ID + " INTEGER PRIMARY KEY, AUTO INCREMENT," + COL_INGREDIENT_NAME + " TEXT,"
     + COL_DESCRIPTION + " TEXT," + COL_MEASUREMENT + " TEXT," + COL_INGREDIENT_TYPE + " TEXT, " +
     "FOREIGN KEY (" + COL_MEASUREMENT + ") REFERENCES " + TABLE_MEASUREMENT + "(" + COL_MEASUREMENT_NAME + "), " +
     "FOREIGN KEY (" + COL_INGREDIENT_TYPE + ") REFERENCES " + TABLE_INGREDIENT_TYPE + "(" + COL_TYPE_NAME + "))";

Update

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";
    String selectQuery = "";
    selectQuery = selectQuery + RECIPE_SEARCH;

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

}

Made some chnages based on suggestions, thankyou. However still getting
Caused by: android.database.sqlite.SQLiteException: no such table: QUANTITY (code 1 SQLITE_ERROR): , while compiling: SELECT + A.ingredient_quantity, B.measurement_name, B.ingredient_name, B.description FROM QUANTITY AS A JOIN INGREDIENTS AS B ON A.ingredient = B.ingredient_name

答案1

得分: 0

`TABLE_QUANTITY``TABLE_INGREDIENT` 是字符串变量您必须在 SQL 语句中将它们连接起来并设置别名 A 和 B

    "SELECT A.ingredient_quantity, B.measurement_name, B.ingredient_name, B.description " +
    "FROM " + TABLE_QUANTITY + " AS A JOIN " + TABLE_INGREDIENT + 
    " AS B ON A.ingredient = B.ingredient_name"

另一个问题是这个

    INTEGER PRIMARY KEY, AUTO INCREMENT

在两个表的 `CREATE` 语句中都是错误的<br/>
移除逗号使其变为

    INTEGER PRIMARY KEY AUTOINCREMENT

您可能需要从设备中卸载该应用以便删除数据库然后重新运行应用以重新创建数据库和表
英文:

TABLE_QUANTITY and TABLE_INGREDIENT are string variables that you must concatenate inside the SQL statement and set the aliases A and B also:

&quot;SELECT A.ingredient_quantity, B.measurement_name, B.ingredient_name, B.description &quot; +
&quot;FROM &quot; + TABLE_QUANTITY + &quot;AS A JOIN &quot; + TABLE_INGREDIENT + 
&quot; AS B ON A.ingredient = B.ingredient_name&quot;

What is also wrong is this:

INTEGER PRIMARY KEY, AUTO INCREMENT

in both CREATE statements of the 2 tables.<br/>
Remove the comma so it is:

INTEGER PRIMARY KEY AUTOINCREMENT

You may have to uninstall the app from the device so the db is deleted and rerun to recreate the db and the tables.

huangapple
  • 本文由 发表于 2020年9月17日 05:23:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/63928107.html
匿名

发表评论

匿名网友

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

确定