Question

I'd like to update some item in a list. It works fine and also I'd like to do this with the database row. There are no errors but query is not working because after relaunching application there are old data. What is wrong? Ofcourse I've got more columns than I want to change

This is how I'm invoking the query:

if(!done)
    {

list.get(longPressedPossition).setRating(tmpBundleIntent.getInt("nowaOcena"));
list.get(longPressedPossition).setNameOfItem(tmpBundleIntent.getString("nowaNazwa"));
dataBaseManager.updateTodo(list.get(longPressedPossition));
                            adapter.notifyDataSetChanged();
                            done = true;

    }

And the method in a class:

public void updateTodo(GifModel model) {
        String where = SqliteHelper.NAME + "=" +"'"+ model.getNameOfIteme()+ "'";

        ContentValues updateTodoValues = new ContentValues();
        updateTodoValues.put(SqliteHelper.NAME, model.getNameOfIteme());
        updateTodoValues.put(SqliteHelper.RATING, model.getRating());
        database.update(SqliteHelper.TABLE_NAME, updateTodoValues, where, null);
    }
Was it helpful?

Solution

You are trying to update the name of the item by looking for the item using its new name which is not set yet (in the where clause). So the update has no effect because no item matches the where clause. Use the row id instead to identify the item.

Also:

  • You should pass all the string arguments of a where clause in the whereArgs array as last parameter instead of null, so that the string will be escaped properly to avoid SQL injection problems. In the where clause string, replace the arguments with a question mark: '?'.
  • You should not update a database on the UI thread because it is an expensive operation, but do it in an AsyncTask instead.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top