I’m implementing my own Content Provider because I’m gonna synchronize my database with a server. My data is stored in a SQLiteDatabase, and some of my tables have a compound primary key (2 columns, each one a foreign key to another table).
I just started working with Content Providers and I don’t know much how they work, but as I could see googling a bit I need to define the name of the tables and their primary key:
uriMatcher.addURI(PROVIDER_NAME, "books", BOOKS);
uriMatcher.addURI(PROVIDER_NAME, "books/#", BOOK_ID);
As I understand it, if I use the URI …/books/1 it would access the Book with primary key “1”. The thing is, does it work with a compund primary key? If it does, how would the URI be?
Thank you!
You do not need to use a
ContentProviderto synchronize a database with a server.Not readily. Quoting the docs:
You could to add a separate column (typically named
_id) that you will use for theContentProvideras the primary key. If you use anAUTOINCREMENTcolumn, you will not ever have to assign a value yourself, meaning that you can ignore this column in pretty much all respects except whereContentProviderwants an instanceUri.You are welcome to attempt to avoid this, and use a
Urithat looks likecontent://your.authority.goes.here/table/key1/key2or something like that. However, it will not work withCursorAdapterand various other places in the framework may assume the numeric ID pattern.