Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Android Android Data Persistence CRUD Operations with SQLite Updating Existing Data

Once again, assume you have a variable named 'database'. Update the database using the 'values' and 'clause' variables a

Once again, assume you have a variable named 'database'. Update the database using the 'values' and 'clause' variables and "PETS_TABLE" as the table name. Use the documentation for help on how to call the appropriate method to update this data.

CodeChallenge.java
ContentValues values = new ContentValues();
values.put("name", "Rover");
values.put("type", "cat");
values.put("breed", "grumpy");

String clause = String.format("%s=%s", "name","Rover");

/* Add your code here! 
 * Assume you already have a variable named 'database'.
 */
//public void update(String table, ContentValues values, String whereClause, String[] whereArgs)
//{ database.update("PETS_TABLE", updateAnnotations, String.format("%s=%d","name","Rover", null); }//
database.update(PETS_TABLE, ('name','type', 'breed') +  VALUES ('Rover','cat', 'grumpy'), "where _id = 1", null);

please help im stuck

1 Answer

Harry James
Harry James
14,780 Points

Hey John!

We've already got most of the variables set for us, all we have to do is use them!


First of all, you were close with the first argument for the update() method. We just need to wrap our database in quotes "" as it's not a variable!

The next two arguments are both variables. For the second argument, we just put in our ContentValues object - the key/value pairs that we're using for the database. We don't need to repeat any code again - it's all saved in the object.

In the next argument, we put our where clause. The clause you've used would only update one record in the database - the record with the _id of 1. Now it may be that Rover is at _id 1 in the database but we don't know this. Instead, we want to use that where clause we finished in the first part of the challenge. This reads, in English, "where name is equal to Rover, make this change".

Finally, the last argument you can leave as you have. We're going to null this as it's used if we want any further arguments on our where clause - the clause we have right now will do the job without any arguments so we pass null.

Now, our update statement reads "In PETS_TABLE, update the keys with the values we've stored in the values object where name is equal to Rover". So, we're going to update Rover's name, type and breed, awesome!


Hopefully, you should now have a working update method! Try your best at completing the challenge with what I've told you but if you get stuck along the way and need a bit more help, let me know. Also, if there's anything you don't understand about this then I'd be more than happy to explain it to you :)

thanx james i got it now

Harry James
Harry James
14,780 Points

Glad to hear you got it! If my answer helped, be sure to mark it as the Best Answer so that other forum users know that this question has been answered :)