Skip to content
Advertisement

SQLite Database not getting updated

So Ive created an app where the user can input details about the movies he has watched such as name,cast,rating…ect and the details are stored in a database inside a table names which is initialized in the DataBaseHelper class as

in the below segment of code Ive created a list view and displayed the names of the movies with a checkbox in front of each name. where the check box if ticked mean that its a favorite else not a favorite…initially the column in the table which holds if the movie is a favorite is set to “no” when ticked and button pressed I want all the movie names with the tick on to update to “yes” in the database.

DisplayActivity class with the list view

Method in DataBaseHelper class to update the favorites column

when I try it like this, the columns wont update….Please help

Advertisement

Answer

Assuming that itemSelected is created correctly inside the onClick() listener, I suggest that you use a char like "|" as a delimiter instead of "n" for the movie titles and remove "Selected items : n" from the start of itemSelected.
Also move myDb.updateFavorites(itemSelected,"yes"); out of the for loop, so that the updateFavorites() is called only once for all selected movies:

Then use the update() method to update the table with the operator LIKE in the WHERE clause:

Note that I changed the return type of updateFavorites() from boolean to int() because db.update() returns the number of updated rows.

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement