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 Deleting Data

Why "%s=%s" in the String.format(..) method?

In this video, why has Evan Anger used the format "%s=%s" for ID values where an additional data type conversion was required (from int to String) instead of using the format "%s=%d" like in the previous cases?

1 Answer

Harry James
Harry James
14,780 Points

I believe Evan was just showing an alternate method of doing this.

If you were to change your code to

        database.delete(MemeSQLiteHelper.ANNOTATIONS_TABLE,
                String.format("%s=%d", MemeSQLiteHelper.COLUMN_FOREIGN_KEY_MEME,
                        memeId), null);
        database.delete(MemeSQLiteHelper.MEMES_TABLE,
                String.format("%s=%d", BaseColumns._ID,
                        memeId), null);

then it would also work fine.


However, this would not work:

        // ** ** ** This would cause an error because the second parameter is not a String (%s) ** ** **
        database.delete(MemeSQLiteHelper.ANNOTATIONS_TABLE,
                String.format("%s=%s", MemeSQLiteHelper.COLUMN_FOREIGN_KEY_MEME,
                        memeId), null);
        database.delete(MemeSQLiteHelper.MEMES_TABLE,
                String.format("%s=%s", BaseColumns._ID,
                        memeId), null);

or this:

        // ** ** ** This would cause an error because the second parameter is not a integer/decimal (%d) ** ** **
        database.delete(MemeSQLiteHelper.ANNOTATIONS_TABLE,
                String.format("%s=%d", MemeSQLiteHelper.COLUMN_FOREIGN_KEY_MEME,
                        String.valueOf(memeId)), null);
        database.delete(MemeSQLiteHelper.MEMES_TABLE,
                String.format("%s=%d", BaseColumns._ID,
                        String.valueOf(memeId)), null);

I can understand the confusion this could cause but, as long as you understand that the formatters have to match the parameters, it doesn't matter what you put :)