SQLiteException没有这样的表,我错过了什么?

时间:2018-04-12 13:16:09

标签: java sqlite

我整个上午都研究过这个错误的其他例子,但到目前为止我没有尝试过任何事情。这可能是显而易见的,因为我对此很陌生。

我正在尝试返回GET_ALL_INGREDIENTS查询的结果。然后,我会将其放入二维阵列中,但您不必担心这一点。我的TABLE_RECIPES表工作正常。

SQLiteException: no such table: TABLE_INGREDIENTS (code 1): , while compiling: SELECT INGR ...等...

这发生在这段代码的最后一行:

public String[][] getRecipeIngredients(int id) {

String GET_ALL_INGREDIENTS = "SELECT INGREDIENT_NAME, INGREDIENT_QUANTITY, INGREDIENT_UNIT " +
            "FROM TABLE_INGREDIENTS " +
            "INNER JOIN TABLE_RECIPE_INGREDIENTS ON TABLE_RECIPE_INGREDIENTS.INGREDIENT_ID=TABLE_INGREDIENTS.INGREDIENT_ID " +
            "WHERE RECIPE_INGREDIENTS.RECIPE_ID=" + id;

    db = new MyDBHandler(mContext);
    SQLiteDatabase db = this.getReadableDatabase();
    Cursor c = db.rawQuery(GET_ALL_INGREDIENTS, null);

表格创建:

final String CREATE_TABLE_RECIPES = "CREATE TABLE IF NOT EXISTS " +
        TABLE_RECIPES + "(" +
        RECIPE_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
        RECIPE_NAME + " TEXT, " +
        RECIPE_INSTRUCTIONS + " TEXT " +
        ")";

final String CREATE_TABLE_INGREDIENTS = "CREATE TABLE IF NOT EXISTS " +
        TABLE_INGREDIENTS + "(" +
        INGREDIENT_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
        INGREDIENT_NAME + " TEXT NOT NULL UNIQUE " +
        ")";

final String CREATE_TABLE_RECIPE_INGREDIENTS = "CREATE TABLE IF NOT EXISTS " +
        TABLE_RECIPE_INGREDIENTS + "(" +
        RECIPE_ID + " INTEGER NOT NULL, " +
        INGREDIENT_ID + " INTEGER NOT NULL, " +
        INGREDIENT_QUANTITY + " REAL, " +
        INGREDIENT_UNIT + " TEXT, " +
        "PRIMARY KEY (" + RECIPE_ID + "," + INGREDIENT_ID + "), " +
        "FOREIGN KEY (" + RECIPE_ID + ") REFERENCES " + TABLE_RECIPES + "(" + RECIPE_ID + "), " +
        "FOREIGN KEY (" + INGREDIENT_ID + ") REFERENCES " + TABLE_INGREDIENTS + "(" + INGREDIENT_ID + ")" +
        ")";

public void onCreate(SQLiteDatabase db) {

    db.execSQL(CREATE_TABLE_RECIPES);
    db.execSQL(CREATE_TABLE_INGREDIENTS);
    db.execSQL(CREATE_TABLE_RECIPE_INGREDIENTS);
}

感谢您的阅读,我真的很感激我能得到的任何帮助。

1 个答案:

答案 0 :(得分:1)

TABLE_INGREDIENTS是一个变量。它的值可能不是"TABLE_INGREDIENTS"这是一个字符串文字。所以你可能想要改变

"FROM TABLE_INGREDIENTS " +

"FROM " + TABLE_INGREDIENTS +

(您的SQL中也有许多其他类似的问题。)