0

我不断收到各种形式的错误,例如下面的列,但我在我的代码中看不到任何错误,例如逗号或 ' 在错误的地方等,有没有人知道这里有什么问题,可能很明显。我附上了我认为最相关的部分数据库代码。

android.database.sqlite.SQLiteException:表 MEAL_PLAN 没有名为 plan_recipe 的列(代码 1 SQLITE_ERROR[1]):,编译时:INSERT INTO MEAL_PLAN(plan_name,plan_recipe)值('Plan Name','14')

更新

语法都是正确的,但是否与创建/调用它们的顺序有关,这可能导致此问题,我找不到问题所在。请帮忙

数据库

public class DatabaseManager extends SQLiteOpenHelper {
String recipe;
// Logcat identifier
private static final String LOG = "DatabaseManager";
// Database Version
private static final int DATABASE_VERSION = 1;
// Database Name
private static final String DATABASE_NAME = "Plan2Cook.db";
// Table Names
public static final String TABLE_CATEGORY = "CATEGORY";
public static final String TABLE_RECIPE = "RECIPE";
public static final String TABLE_MEAL_PLAN = "MEAL_PLAN";
public static final String TABLE_QUANTITY = "QUANTITY";
public static final String TABLE_SHOPPING_LIST = "SHOPPING_LIST";
public static final String TABLE_PLAN_RECIPES = "PLAN_RECIPES";
public static final String TABLE_COURSE = "COURSE";
public static final String TABLE_INGREDIENTS = "INGREDIENTS";
public static final String TABLE_MEASUREMENT = "MEASUREMENT";
public static final String TABLE_INGREDIENT_TYPE = "INGREDIENT_TYPE";
public static final String TABLE_ALLERGENS = "ALLERGENS";
public static final String TABLE_RECIPES_ALLERGENS = "RECIPES_ALLERGENS";
public static final String TABLE_FAVOURITES = "FAVOURITES";

// Common column names
private static final String COL_ID = "id";
private static final String COL_MEASUREMENT = "measurement";
private static final String COL_INGREDIENT_TYPE = "ingredient_type";
private static final String COL_DESCRIPTION = "description";
private static final String COL_IMAGE = "image";

// Category column names
private static final String COL_CATEGORY_NAME = "category_name";
// Recipe column names
private static final String COL_RECIPE_NAME = "recipe_name";
private static final String COL_SERVINGS = "servings";
private static final String COL_CALORIES = "calories";
private static final String COL_PREPARATION_TIME = "preparation_time";
private static final String COL_METHOD = "method";
private static final String COL_COURSE = "course";
private static final String COL_CATEGORY = "category";
private static final String COL_COST = "cost";
private static final String COL_IMAGE2 = "image2";
//Quantity column names
private static final String COL_INGREDIENT_QUANTITY = "ingredient_quantity";
private static final String COL_RECIPE = "recipe";
private static final String COL_INGREDIENT = "ingredient";
//Meal Plan column names
private static final String COL_PLAN_NAME = "plan_name";
private static final String COL_PLAN_RECIPE = "plan_recipe";

//Shopping List column names
private static final String COL_QUANTITY = "quantity";
private static final String COL_PLANID = "planID";

//Plan Recipes column names
private static final String COL_DATE = "date";
private static final String COL_DAY_OF_WEEK = "dayOfWeek";
//Course column names
private static final String COL_COURSE_NAME = "course_name";
//Ingredients column names
private static final String COL_INGREDIENT_NAME = "ingredient_name";
//Measurement column names
private static final String COL_MEASUREMENT_NAME = "measurement_name";
//Ingredient Type column names
private static final String COL_TYPE_NAME = "type_name";
//Allergens column names
private static final String COL_ALLERGEN_NAME = "allergen_name";

/* *************************************************************************************
 ************************* CREATE TABLE STATEMENTS **************************************
 ****************************************************************************************
 */
// Create Table Category
private static final String CREATE_TABLE_CATEGORY = "CREATE TABLE "
        + TABLE_CATEGORY + "(" + COL_ID + " INTEGER PRIMARY KEY AUTOINCREMENT," + COL_CATEGORY_NAME
        + " TEXT," + COL_IMAGE + " INTEGER)";


// Create Table Recipe
private static final String CREATE_TABLE_RECIPE = "CREATE TABLE " + TABLE_RECIPE + "(" + COL_ID +
        " INTEGER PRIMARY KEY AUTOINCREMENT," + COL_RECIPE_NAME + " TEXT," + COL_DESCRIPTION
        + " TEXT," + COL_COURSE + " TEXT," + COL_SERVINGS + " INTEGER," + COL_CALORIES + " NUMERIC,"
        + COL_PREPARATION_TIME + " NUMERIC," + COL_METHOD + " TEXT," + COL_CATEGORY + " TEXT,"
        + COL_IMAGE + " INTEGER," + COL_IMAGE2 + " TEXT," + COL_COST + " NUMERIC," + " FOREIGN KEY (" + COL_CATEGORY + ") REFERENCES " +
        TABLE_CATEGORY + "(" + COL_CATEGORY_NAME + "), FOREIGN KEY (" + COL_COURSE + ") REFERENCES " +
        TABLE_COURSE + "(" + COL_COURSE_NAME + "))";

// Create Table Quantity
public static final String CREATE_TABLE_QUANTITY = "CREATE TABLE " + TABLE_QUANTITY + "(" + COL_ID +
        " INTEGER PRIMARY KEY AUTOINCREMENT," + 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 + "))";

// Create Table Shopping List
private static final String CREATE_TABLE_SHOPPING_LIST = "CREATE TABLE " + TABLE_SHOPPING_LIST +
        "(" + COL_ID + " INTEGER PRIMARY KEY AUTOINCREMENT," + COL_INGREDIENT_TYPE + " INTEGER,"
        + COL_QUANTITY + " INTEGER," + COL_MEASUREMENT + " INTEGER," + COL_PLAN_NAME + " TEXT," +
        " FOREIGN KEY (" + COL_INGREDIENT_TYPE + ") REFERENCES " + TABLE_INGREDIENT_TYPE + "(" + COL_ID + "), " +
        "FOREIGN KEY (" + COL_QUANTITY + ") REFERENCES " + TABLE_QUANTITY + "(" + COL_ID + "), " +
        "FOREIGN KEY (" + COL_MEASUREMENT + ") REFERENCES " + TABLE_MEASUREMENT + "(" + COL_ID + "), " +
        "FOREIGN KEY (" + COL_PLAN_NAME + ") REFERENCES " + TABLE_MEAL_PLAN + "(" + COL_PLAN_NAME + "))";
// Create Table Course
private static final String CREATE_TABLE_COURSE = "CREATE TABLE " + TABLE_COURSE + "(" + COL_ID +
        " INTEGER PRIMARY KEY AUTOINCREMENT, " + COL_COURSE_NAME + " TEXT," + COL_IMAGE + " INTEGER)";
// Create Table Ingredients
public static final String CREATE_TABLE_INGREDIENTS = "CREATE TABLE " + TABLE_INGREDIENTS + "("
        + COL_ID + " INTEGER PRIMARY KEY AUTOINCREMENT," + COL_INGREDIENT_NAME + " TEXT,"
        + COL_DESCRIPTION + " TEXT," + COL_MEASUREMENT_NAME + " TEXT," + COL_INGREDIENT_TYPE + " TEXT, " +
        "FOREIGN KEY (" + COL_MEASUREMENT_NAME + ") REFERENCES " + TABLE_MEASUREMENT + "(" + COL_MEASUREMENT_NAME + "), " +
        "FOREIGN KEY (" + COL_INGREDIENT_TYPE + ") REFERENCES " + TABLE_INGREDIENT_TYPE + "(" + COL_TYPE_NAME + "))";
// Create Table Measurement
private static final String CREATE_TABLE_MEASUREMENT = "CREATE TABLE " + TABLE_MEASUREMENT + "(" +
        COL_ID + " INTEGER PRIMARY KEY AUTOINCREMENT," + COL_MEASUREMENT_NAME + " TEXT)";
// Create Table Ingredient Type
private static final String CREATE_TABLE_INGREDIENT_TYPE = "CREATE TABLE " + TABLE_INGREDIENT_TYPE +
        "(" + COL_ID + " INTEGER PRIMARY KEY AUTOINCREMENT," + COL_TYPE_NAME + " TEXT)";

// Create Table Allergens
private static final String CREATE_TABLE_ALLERGENS = "CREATE TABLE " + TABLE_ALLERGENS +
        "(" + COL_ID + " INTEGER PRIMARY KEY AUTOINCREMENT," + COL_ALLERGEN_NAME + " TEXT)";
// Create Table Recipes_Allergens
private static final String CREATE_TABLES_RECIPES_ALLERGENS = "CREATE TABLE " + TABLE_RECIPES_ALLERGENS +
        "(" + COL_ID + " INTEGER PRIMARY KEY AUTOINCREMENT," + COL_ALLERGEN_NAME + " TEXT," +
        COL_RECIPE + " TEXT," + "FOREIGN KEY (" + COL_ALLERGEN_NAME + ") REFERENCES " + TABLE_ALLERGENS +
        "(" + COL_ALLERGEN_NAME +"), " + "FOREIGN KEY (" + COL_RECIPE + ") REFERENCES " + TABLE_RECIPE +
        "(" + COL_RECIPE_NAME + "))";
// Create Table Plan Recipes
private static final String CREATE_TABLE_PLAN_RECIPES = "CREATE TABLE " + TABLE_PLAN_RECIPES + "(" +
        COL_ID + " INTEGER PRIMARY KEY AUTOINCREMENT," + COL_DATE + " DATE," +
        COL_DAY_OF_WEEK + " TEXT," + COL_RECIPE_NAME + " TEXT, FOREIGN KEY (" + COL_RECIPE_NAME + ") " +
        "REFERENCES " + TABLE_RECIPE + "(" + COL_RECIPE_NAME + "))";

// Create Table Meal Plan
private static final String CREATE_TABLE_MEAL_PLAN = "CREATE TABLE " +
        TABLE_MEAL_PLAN + "(" + COL_ID + " INTEGER PRIMARY KEY AUTOINCREMENT," +
        COL_PLAN_NAME + " TEXT," + COL_PLAN_RECIPE + " INTEGER," + " FOREIGN KEY (" + COL_PLAN_RECIPE + ") " +
        "REFERENCES " + TABLE_PLAN_RECIPES + "(" + COL_ID + "))";

// Create Table Favourites
private static final String CREATE_TABLE_FAVOURITES = "CREATE TABLE " + TABLE_FAVOURITES + "(" +
        COL_ID + " INTEGER PRIMARY KEY AUTOINCREMENT," + COL_RECIPE_NAME + " TEXT," +
        "FOREIGN KEY (" + COL_RECIPE_NAME + ") REFERENCES " + TABLE_RECIPE + "(" + COL_RECIPE_NAME + "))";


public final String INSERT_MEASUREMENT =
        "INSERT INTO " + TABLE_MEASUREMENT + "("
                + COL_MEASUREMENT_NAME + ") values ";
public final String INSERT_INGREDIENT_TYPE =
        " INSERT INTO " + TABLE_INGREDIENT_TYPE + "("
                + COL_TYPE_NAME + ") values ";
public final String INSERT_INGREDIENT =
        " INSERT INTO " + TABLE_INGREDIENTS + "("
                + COL_INGREDIENT_NAME + "," + COL_DESCRIPTION + ","
                + COL_MEASUREMENT_NAME + "," + COL_INGREDIENT_TYPE + ") values ";
public final String INSERT_CATEGORY =
        " INSERT INTO " + TABLE_CATEGORY + "("
                + COL_CATEGORY_NAME + "," + COL_IMAGE + ") values ";
public final String INSERT_COURSE =
        " INSERT INTO " + TABLE_COURSE + "("
                + COL_COURSE_NAME + "," + COL_IMAGE + ") values ";
public final String INSERT_RECIPE =
        " INSERT INTO " + TABLE_RECIPE + "("
                + COL_RECIPE_NAME + "," + COL_DESCRIPTION + ","
                + COL_COURSE + "," + COL_SERVINGS + ","
                + COL_CALORIES + "," + COL_PREPARATION_TIME + ","
                + COL_METHOD + "," + COL_CATEGORY + ","
                + COL_IMAGE + ","
                + COL_IMAGE2 + ","
                + COL_COST + ") values";
public final String INSERT_QUANTITY =
        " INSERT INTO " + TABLE_QUANTITY + "("
                + COL_INGREDIENT_QUANTITY + ","
                + COL_RECIPE + ","
                + COL_INGREDIENT + ") values";
public final String INSERT_ALLERGEN =
        " INSERT INTO " + TABLE_ALLERGENS + "("
        + COL_ALLERGEN_NAME + ") values";
public final String INSERT_RECIPES_ALLERGENS =
        " INSERT INTO " + TABLE_RECIPES_ALLERGENS + "("
        + COL_ALLERGEN_NAME + ","
        + COL_RECIPE + ") values";
public final String INSERT_FAVOURITE =
        " INSERT INTO " + TABLE_FAVOURITES + "("
        + COL_RECIPE_NAME + ") values";
public final String INSERT_PLAN_RECIPE =
        " INSERT INTO " + TABLE_PLAN_RECIPES + "("
        + COL_DATE + ","
        + COL_DAY_OF_WEEK + ","
        + COL_RECIPE_NAME + ") values";
public final String INSERT_PLAN =
        " INSERT INTO " + TABLE_MEAL_PLAN + "("
        + COL_PLAN_NAME + ","
        + COL_PLAN_RECIPE + ") values";


/**
 * Create a helper object to create, open, and/or manage a database.
 * This method always returns very quickly.  The database is not actually
 * created or opened until one of {@link #getWritableDatabase} or
 * {@link #getReadableDatabase} is called.
 *
 * @param context to use for locating paths to the the database
 * @param factory to use for creating cursor objects, or null for the default
 * @param version number of the database (starting at 1); if the database is older,
 *                {@link #onUpgrade} will be used to upgrade the database; if the database is
 *                newer, {@link #onDowngrade} will be used to downgrade the database
 */

public DatabaseManager(Context context) {
    super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
public DatabaseManager(@Nullable Context context, @Nullable String name, @Nullable SQLiteDatabase.CursorFactory factory, int version) {
    super(context, name, factory, version);
}

@Override
public void onCreate(SQLiteDatabase db) {

    db.execSQL(CREATE_TABLE_CATEGORY);
    db.execSQL(CREATE_TABLE_COURSE);
    db.execSQL(CREATE_TABLE_INGREDIENT_TYPE);
    db.execSQL(CREATE_TABLE_INGREDIENTS);
    db.execSQL(CREATE_TABLE_MEASUREMENT);
    db.execSQL(CREATE_TABLE_RECIPE);
    db.execSQL(CREATE_TABLE_QUANTITY);
    db.execSQL(CREATE_TABLE_PLAN_RECIPES);
    db.execSQL(CREATE_TABLE_MEAL_PLAN);
    db.execSQL(CREATE_TABLE_ALLERGENS);
    db.execSQL(CREATE_TABLES_RECIPES_ALLERGENS);
    db.execSQL(CREATE_TABLE_SHOPPING_LIST);
    db.execSQL(CREATE_TABLE_FAVOURITES);
    Log.d("Tables created","yes");
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

    // on upgrade drop older tables
    db.execSQL("DROP TABLE IF EXISTS " + TABLE_CATEGORY);
    db.execSQL("DROP TABLE IF EXISTS " + TABLE_COURSE);
    db.execSQL("DROP TABLE IF EXISTS " + TABLE_INGREDIENT_TYPE);
    db.execSQL("DROP TABLE IF EXISTS " + TABLE_INGREDIENTS);
    db.execSQL("DROP TABLE IF EXISTS " + TABLE_MEAL_PLAN);
    db.execSQL("DROP TABLE IF EXISTS " + TABLE_MEASUREMENT);
    db.execSQL("DROP TABLE IF EXISTS " + TABLE_PLAN_RECIPES);
    db.execSQL("DROP TABLE IF EXISTS " + TABLE_QUANTITY);
    db.execSQL("DROP TABLE IF EXISTS " + TABLE_RECIPE);
    db.execSQL("DROP TABLE IF EXISTS " + TABLE_SHOPPING_LIST);

}

// Insert into Course String
public String getInsertCourse(String course_name, int image) {
    return INSERT_COURSE + " ( " +
            "'" + course_name + "'," +
            "'" + image + "')";
}

// Insert into favourite String
public String getInsertFavourite(String recipe_name) {
    return INSERT_FAVOURITE + " ( " +
            "'" + recipe_name + "')";
}
// Insert into Measurement String
public String getInsertMeasurementString(String measurement_name) {
    return INSERT_MEASUREMENT + " ( " +
            "'" + measurement_name + "')";
}

// Insert into ingredient type string
public String getInsertIngredientType(String ingredient_type) {
    return INSERT_INGREDIENT_TYPE + " ( " +
            "'" + ingredient_type + "')";
}

// Insert into ingredients string
public String getInsertIngredients(String ingredient_name, String description, String measurement_name, String ingredient_type) {
    return INSERT_INGREDIENT + " ( " +
            "'" + ingredient_name + "'," +
            "'" + description + "'," +
            "'" + measurement_name + "'," +
            "'" + ingredient_type + "')";
}

// Insert into category string
public String getInsertCategory(String category_name, int image) {
    return INSERT_CATEGORY + " ( " +
            "'" + category_name + "'," +
            "'" + image + "')";
}

// Insert into recipe string

public String getInsertRecipe(String recipe_name, String description, String course, Integer servings, Double calories,
                              Integer preparation_time, String method, String category, Integer image, String image2, Double cost) {
    return INSERT_RECIPE + " ( " +
            "'" + recipe_name + "'," +
            "'" + description + "'," +
            "'" + course + "'," +
            "'" + servings + "'," +
            "'" + calories + "'," +
            "'" + preparation_time + "'," +
            "'" + method + "'," +
            "'" + category + "'," +
            "'" + image + "'," +
            "'" + image2 + "'," +
            "'" + cost + "')";
}

// Insert into quantity table string

public String getInsertQuantity(Double ingredient_quantity, String recipe, String ingredient) {
    return INSERT_QUANTITY + " ( " +
            "'" + ingredient_quantity + "'," +
            "'" + recipe + "'," +
            "'" + ingredient + "')";
}
// Insert into allergen table string


public String getInsertAllergenName(String allergen_name) {
    return INSERT_ALLERGEN + " ( " +
            "'" + allergen_name + "')";
}

// Insert into recipes allergens string
public String getINSERT_RECIPES_ALLERGENS(String allergen_name, String recipe) {
    return INSERT_RECIPES_ALLERGENS + " ( " +
            "'" + allergen_name + "'," +
            "'" + recipe + "')";
}
// Insert into plan recipes string
public String getINSERT_PLAN_RECIPE(Integer date, String dayOfWeek, String recipe_name ) {
    return INSERT_PLAN_RECIPE + " ( " +
            "'" + date + "'," +
            "'" + dayOfWeek + "'," +
            "'" + recipe_name + "')";
}
public String getInsertPlan (String plan_name, Integer plan_recipe) {
    return INSERT_PLAN + " ( " +
            "'" + plan_name + "'," +
            "'" + plan_recipe + "')";
}


public String RECIPE_SEARCH = " SELECT + A.ingredient_quantity, B.measurement_name, B.ingredient_name, B.description " +
        "FROM" + TABLE_QUANTITY + "AS A JOIN " + TABLE_INGREDIENTS +
        " AS B ON A.ingredient = B.ingredient_name";
public void createPlanRecipe(Integer date, String dayOfWeek, String recipe_name) {
    SQLiteDatabase db = this.getWritableDatabase();
    db.execSQL(getINSERT_PLAN_RECIPE(date, dayOfWeek, recipe_name));
}
public void createPlan(String plan_name, Integer plan_recipe) {
    SQLiteDatabase db = this.getWritableDatabase();
    Cursor c = db.rawQuery("select last_insert_rowid() as id", null);
    if (c.moveToFirst()) {plan_recipe = c.getInt(c.getColumnIndex("id"));}
    db.execSQL(getInsertPlan(plan_name, plan_recipe));
}

public void addIngredients(double ingredient_quantity, String recipe, String name, String ingredient,
                           String description, String measurement_name, String ingredient_type) {
    SQLiteDatabase db = this.getWritableDatabase();
    db.execSQL(getInsertQuantity(ingredient_quantity, recipe, name));
    db.execSQL(getInsertIngredients(ingredient, description, measurement_name, ingredient_type));
}

public void addRecipe (String recipe_name, String description, String course, int servings, Double calories,
                       Integer preparation_time, String method, String category, Integer image, String image2, Double cost, String allergen_name) {
    SQLiteDatabase db = this.getWritableDatabase();
    db.execSQL(getInsertRecipe(recipe_name, description, course, servings, calories, preparation_time, method, category, image, image2, cost));
    db.execSQL(getINSERT_RECIPES_ALLERGENS(allergen_name, recipe_name));
}

public void addFavourite(String search_name) {
    SQLiteDatabase db = this.getWritableDatabase();
    db.execSQL(getInsertFavourite(search_name));
}
// Delete from
public void delete(String TABLE) {
    SQLiteDatabase db = this.getWritableDatabase();
    db.execSQL("delete from " + TABLE);

}

// Close Database
public void closeDB() {
    SQLiteDatabase db = this.getReadableDatabase();
    if (db != null && db.isOpen())
        db.close();
}

}

4

0 回答 0