Search code examples
androidsqliteandroid-contentproviderandroid-cursorloaderandroid-loadermanager

Android and SQLite - the least complicated solution


Hy,

I have been reading about content providers, Loaders and loaderManagers for whole week now and sadly I must admit, I'm no closer to working solution. And also, I think integrating full blown loadermanager-cursorLoader system would be perhaps an overkill for my needs. So if any of you could assess if this is true (or just excuse from my side :)), maybe direct me in right direction or even provide working example/implementation steps for my specific case, that would be great!

Background:

  • I need to assure minimum compatibility up to API 10. App uses complex pre-populated SQLite backend with more than 30 tables in different relations to each other. And I load it by extending SQLiteAssetHelper class (not default SQLiteOpenHelper).
  • BUT, interaction between user and database is almost exclusively on level of reading and displaying data, with very few (and far between) examples of some creates/updates/deletes (for, let's say some notes), so no need for synchronous and locking RDB. Most of the time user just selects button/listview position which should re-query RDB and update views accordingly.
  • I also do not need to provide data (and don't wish to) to other apps. So, I guess, no explicit need for content provider (although I am aware I could forbid sharing of content provider in AndroidManifest).

So my questions are:

  1. Taking in to account background I provided, what would be right approach for minimum working solution that would enable querying RDB based on variable provided by button/list position that was pressed by user, and then displaying results of that query in new view/updating current view.
  2. Silly one, but it seems like I can't find definitive answer to this: Per developer guidelines, each table should provide primary key column named "_ID" for cursors to work. Here is the sillines -> What does that mean, exactly? I found guides that suggested naming them lowercase, like "_id", some tutorials used columns named "id", or "ID" and some even named them "columNameId"... Is it possible to name primary key column like: "columnname_id"? Or for my specific case, my primary key columns are named "ID_column_name", should I rename them all and make appropriate changes to my database? If there is no possible way that names stay the same, which is the most supported naming I can/must choose (ID, id, _id, _ID, ...)?

Thank you all!


Solution

    1. Loaders are used to move potentially long-running database queries into another thread (and to keep the data when the activity is recreated, e.g. on screen rotations).

      When your query is fast, you can just run it directly from the UI thread.

      Android's built-in CursorLoader requires a content provider, but you can write your own loader that accesses the database directly: Usage CursorLoader without ContentProvider.

    2. Cursor adapters require that query has a column named _id containing unique values. You could rename some other unique column to _id while querying (SELECT SomethingElse AS _id ...), but when you have a primary key, you could just as well store it as _id in the database.

      (SQLite ignores case in column names.)