Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Android Android Data Persistence CRUD Operations with SQLite Updating Existing Data

SQLite tables

Harry James your name is synonymous with being a database fundi. What am I doing wrong?

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

database.update("PETS_TABLE", values , "where _id = 1", null);
Harry James
Harry James
14,780 Points

Hey Malcolm - Do you mind letting me know what the challenge task you're on is (I have my subscription paused right now so can't check the challenge myself!) by pasting the question here and I'll take a look :)

4 Answers

Harry James
Harry James
14,780 Points

Thanks Malcolm and sorry for getting back to you a bit late!

In Android, the third argument of database.update() is the whereClause. You've already got this clause, so change the third argument to clause (The variable).

You don't need to specify the "where" as this is filled in automatically by Android. To the database, this will look like UPDATE WHERE name=rover (%s=%s) with values.


I think this will make it pass but let me know if not :)

Hi Harry

Challenge Task 2 of 2

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.

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'. */

this is what I typed

database.update("PETS_TABLE", values , "where _id = 1", null);

Hi Harry.

LIke so

database.update("PETS_TABLE", values , clause, null);

Got it. Thank you thank you.

That does the clause even mean?

Harry James
Harry James
14,780 Points

Yep that's it!

The clause is really just the command you're telling the database.

To explain this better, when we do database.update(), it will run the command in the database "UPDATE WHERE <your clause goes here>". So the clause is the condition that must be true for the values to be updated.


Hope that helps but if you don't quite get it, let me know and I'll try to rephrase it :)

Ah nice. Thanks man. May the force be with you ;)

Harry James
Harry James
14,780 Points

No problemo - any time :)