mardi 3 novembre 2015

Android: SQLite says a column doesn't exist

I am trying to get all the values in a table that have column _parentbook set to a certain value. When I try to retrieve the entries I get the error shown below.

E/SQLiteLog: (1) table recipes has no column named _parentbook
E/SQLiteDatabase: Error inserting _parentbook=Test _recipemethod=Stir in pot for 20 mins _recipeingredients=No bugs, freedom _recipedescription=Test recipe _recipename=Recipe 1 in Test _recipenotes=Do on Android Studio

The error refers to the method below that I use to add a recipe to the database

public void addRecipe(Recipe recipe) {

    ContentValues values = new ContentValues();
    values.put(COLUMN_RECIPE_NAME, recipe.getRecipeTitle());
    values.put(COLUMN_RECIPE_DESCRIPTION, recipe.getRecipeDescription());
    values.put(COLUMN_RECIPE_INGREDIENTS, recipe.getIngredients());
    values.put(COLUMN_RECIPE_METHOD, recipe.getMethod());
    values.put(COLUMN_RECIPE_NOTES, recipe.getNotes());
    //values.put(COLUMN_IMAGE_ID, recipe.getImageId());
    values.put(COLUMN_PARENT_BOOK, recipe.getParentBook());

    SQLiteDatabase db = this.getWritableDatabase();
    db.insert(TABLE_RECIPES, null, values);
    db.close();
}

Code used to initialise TABLE_RECIPES:

String CREATE_TABLE_RECIPES = "CREATE TABLE IF NOT EXISTS " + TABLE_RECIPES + " (" +
        COLUMN_ID_2 + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
        COLUMN_RECIPE_NAME + " TEXT, " +
        COLUMN_RECIPE_DESCRIPTION + " TEXT, " +
        COLUMN_RECIPE_INGREDIENTS + " TEXT, " +
        COLUMN_RECIPE_METHOD + " TEXT, " +
        COLUMN_RECIPE_NOTES + " TEXT, " +
        COLUMN_IMAGE_ID + " INTEGER " +
        COLUMN_PARENT_BOOK + " TEXT" +
        ");";


@Override
public void onCreate(SQLiteDatabase db) {
    Log.e(TAG, "OnCreate() called");
    db.execSQL(CREATE_TABLE_RECIPES);

}

Method for getting the recipes from the table:

List<Recipe> recipes;
public List<Recipe> getRecipes(String bookName) {

    recipes = new ArrayList<>();

    SQLiteDatabase db = getWritableDatabase();
    //String query = "SELECT "+ COLUMN_PARENT_BOOK +" FROM " + TABLE_RECIPES + " WHERE " + COLUMN_PARENT_BOOK + "=" + bookName;
    String query = "SELECT * FROM " + TABLE_RECIPES;// + " WHERE 1";

    // Cursor going to point to a location in the results
    Cursor c = db.rawQuery(query, null);
    // Move it to the first row of your results
    c.moveToFirst();

    if (c.moveToFirst()) {
        do {
            if (c.getString(c.getColumnIndex(COLUMN_RECIPE_NAME)) != null) {
                recipes.add(new Recipe(
                        c.getString(c.getColumnIndex(COLUMN_RECIPE_NAME)),
                        c.getString(c.getColumnIndex(COLUMN_RECIPE_DESCRIPTION)),
                        c.getString(c.getColumnIndex(COLUMN_RECIPE_INGREDIENTS)),
                        c.getString(c.getColumnIndex(COLUMN_RECIPE_METHOD)),
                        c.getString(c.getColumnIndex(COLUMN_RECIPE_NOTES)),
                        // Add image here if required
                        c.getString(c.getColumnIndex(COLUMN_PARENT_BOOK))
                ));
            }
        } while (c.moveToNext());
    }
    db.close();
    //c.close();
    return recipes;
}

I have tried upgrading the database version and looking at other similar questions on StackOverflow, neither helped.

Thanks.

Aucun commentaire:

Enregistrer un commentaire