-1

I am working with SQLite and I am having trouble deleting data.
First and foremost, this is how I add data to the database:

public void addRecipe (QueryVars Recipee){
    SQLiteDatabase db = this.getWritableDatabase();

    ContentValues values = new ContentValues();
    values.put(KEY_Recipe, Recipee.getRecipe());

    db.insert(TABLE_Recipes, null, values);
    db.close();
}

And this is how I get data from the database:

public List<QueryVars> getAllBooks() {
    List<QueryVars> recipes = new LinkedList<QueryVars>();

    String query = "SELECT  * FROM " + TABLE_Recipes;

    SQLiteDatabase db = this.getWritableDatabase();
    Cursor cursor = db.rawQuery(query, null);

    QueryVars Recipe = null;
    if (cursor.moveToFirst()) {
        do {
            Recipe = new QueryVars();
            // Recipe.setId(Integer.parseInt(cursor.getString(0)));
            Recipe.setRecipe(cursor.getString(1));
            recipes.add(Recipe);
        } while (cursor.moveToNext());
    }

    return recipes;
}

Saving and querying for data is working perfectly fine, but when I try to delete rows with the following code it just doesn't work.

public void deleteRecipes(QueryVars Recipe) {
    SQLiteDatabase db = this.getWritableDatabase();
    db.delete(TABLE_Recipes, KEY_ID + " = ?", new String[] { String.valueOf(Recipe.getId()) });
    db.close();
}

This is the query I use to create the table:

private static final String CREATE_BOOK_TABLE = 
        "CREATE TABLE Recipes ("
            + "id INTEGER PRIMARY KEY AUTOINCREMENT, "
            + "Recipe TEXT"
        + ")";

And the constants I use in my code above are defined like this:

private static final TABLE_Recipes = "Recipes";
private static final String KEY_ID = "id"; 
private static final String KEY_Recipe = "Recipe"; 
private static final String[] COLUMNS = {KEY_ID,KEY_Recipe}; 
0

2 Answers 2

0

There are two issues in your code which could potentially be the source of the error, but both have the same cause: You are letting SQLite generate the ids of your Recipe objects but you are never setting that id to your objects.

  1. When you add something to the database the add() method returns the id which was generated for that row. You can just set this id to the Recipe object otherwise that Recipe object won't have an id until you reload it from the database.

    public void addRecipe (QueryVars Recipee){
        SQLiteDatabase db = this.getWritableDatabase();
    
        ContentValues values = new ContentValues();
        values.put(KEY_Recipe, Recipee.getRecipe());
    
        final long id = db.insert(TABLE_Recipes, null, values);
        Recipee.setId(id);
    
        db.close();
    }
    
  2. When you are reading the Recipe objects from the database you are not setting the id value on the object, so no Recipe object you read from the database has an id which means you cannot delete them from the database. The fix is again pretty simple:

    public List<QueryVars> getAllBooks() {
        List<QueryVars> recipes = new LinkedList<QueryVars>();
    
        String query = "SELECT  * FROM " + TABLE_Recipes;
    
        SQLiteDatabase db = this.getWritableDatabase();
        Cursor cursor = db.rawQuery(query, null);
    
        final int idIndex = cursor.getColumnIndex(KEY_ID);
        final int recipeIndex = cursor.getColumnIndex(KEY_Recipe);
    
        QueryVars Recipe = null;
        if (cursor.moveToFirst()) {
            do {
                Recipe = new QueryVars();
                Recipe.setId(cursor.getLong(idIndex));
                Recipe.setRecipe(cursor.getString(recipeIndex));
                recipes.add(Recipe);
            } while (cursor.moveToNext());
        }
    
        return recipes;
    }
    

    This uses getColumnIndex() to reliably get the correct index of each column and then reads the id and the recipe from the cursor and sets them to the Recipe object.


Please note that your Recipe object needs to have a long id! int ids are not compatible with the SQLiteDatabase!

0

You don't capture the database-generated id of your recipes and the id is zero. It doesn't match any rows in the delete.

Uncomment the

// Recipe.setId(Integer.parseInt(cursor.getString(0)));

(consider using cursor.getInt() instead)

Possibly also store the return value of insert() as the recipe id.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.