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 trialDalton Coble
6,489 PointsAm I the only one that thinks this is weird? Updating existing data code challenge
"Now we need to update the previous row we inserted because we made a mistake. Rover is a grumpy cat, not a mixed-breed dog! Start by completing the where clause (in the clause variable) by replacing the comment with the appropriate name."
I am not sure what the "name" is.
ContentValues values = new ContentValues();
values.put("name", "Rover");
values.put("type", "cat");
values.put("breed", "grumpy");
String clause = String.format("%s=%s", "name", "");
/* Add your code here!
* Assume you already have a variable named 'database'.
*/
2 Answers
Ken Alger
Treehouse TeacherJody;
The challenge is looking for the key value pair here. Let's take a look at what's going on inside String.format
, it is about 1:37 in the video before the challenge.
The "%s=%s"
statement is saying that we will be providing a String value, in this example it is "name"
and we want to assign it to another String value. For this particular challenge we are told that the name of the cat is Rover, so that's what we need to put in there. Our clause
would then look like:
String clause = String.format("%s=%s", "name", "Rover");
Does that make any sense?
Ken
Urgent Nkala
8,772 PointsThis will be for your Task 2 database.update("PETS_TABLE",values,clause,null);
Dalton Coble
6,489 PointsDalton Coble
6,489 Pointscould you help me on the second question too I just cannot recall where he did that.
question: 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.
Here is my code: 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! database.PETS_TABLE // then what */
Ken Alger
Treehouse TeacherKen Alger
Treehouse TeacherJody;
Typically you would want to start a new forum post so that others can find the answer if they are stuck. The link provided in the Task 2 challenge points us to the SQLiteDatabase documentation for Android and specifically the update method, whose generic convention is:
If you take a look in the video around the 5:18 mark he shows the basic syntax for updating a SQLiteDatabase.
Post back if you are still stuck.
Ken
Dalton Coble
6,489 PointsDalton Coble
6,489 PointsI tried this for the second question but it wouldn't work:
ContentValues values = new ContentValues(); values.put("name", "Rover"); values.put("type", "cat"); values.put("breed", "grumpy");
String clause = String.format("%s=%s", "name", "Rover");
public void update(String table, ContentValues values, String whereClause, String[] whereArgs) { database.update(SQLiteHelper.PETS_TABLE, updateAnnotations, String.format("%s=%d", BaseColumns._ID, annotation.getId()), null); }
TERRENCE MAVHUNGA
6,053 PointsTERRENCE MAVHUNGA
6,053 Points@ken Alger, hie im not getting it can you pliz help me by explaining in another way .thnx