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

Am 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.

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", "");

/* Add your code here! 
 * Assume you already have a variable named 'database'.
 */

2 Answers

Ken Alger
STAFF
Ken Alger
Treehouse Teacher

Jody;

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

could 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
Ken Alger
Treehouse Teacher

Jody;

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:

public void update(String table, ContentValues values, String whereClause, String[] whereArgs)

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

I 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); }

@ken Alger, hie im not getting it can you pliz help me by explaining in another way .thnx

This will be for your Task 2 database.update("PETS_TABLE",values,clause,null);