Using Projection Maps

A content provider acts like an intermediary between an abstract set of columns and a real set of columns in a database, yet these column sets might differ. While constructing queries, you must map between the where-clause columns that a client specifies and the real database columns. You set up this projection map with the help of the SOLiteOueryBuilder class.

Here is what the Android SDK documentation says about the mapping method public void setProjectionMap(Map columnMap) available on the OueryBuilder class:

Sets the projection map for the query. The projection map maps from column names that the caller passes into query to database column names. This is useful for renaming columns as well as disambiguating column names when doing joins. For example you could map "name" to "". If a projection map is set it must contain all column names the user may request, even if the key and value are the same.

Here is how our BookProvider content provider sets up the projection map:

sBooksProjectionMap = new HashMap<String, String>(); sBooksProjectionMap.put(BookTableMetaData. _ID, BookTableMetaData._ID);

//name, isbn, author sBooksProjectionMap.put(BookTableMetaData.BOOK_NAME

, BookTableMetaData.BOOK_NAME); sBooksProjectionMap.put(BookTableMetaData.BOOK_ISBN

, BookTableMetaData.BOOK_ISBN); sBooksProjectionMap.put(BookTableMetaData.BOOK_AUTHOR

, BookTableMetaData.BOOK_AUTHOR);

//created date, modified date sBooksProjectionMap.put(BookTableMetaData.CREATED_DATE

, BookTableMetaData.CREATED_DATE); sBooksProjectionMap.put(BookTableMetaData.MODIFIED_DATE

, BookTableMetaData.MODIFIED_DATE);

And then the query builder uses the variable sBooksProjectionMap like this:

queryBuilder.setTables(NOTES_TABLE_NAME); queryBuilder.setProjectionMap(sNotesProjectionMap);

0 0

Post a comment