SQLiteOpenHelper vs ContentProvider

André Alves picture André Alves · Nov 27, 2012 · Viewed 9.2k times · Source

I'm new to Android development. I'm trying to create an application that reads from the internal database (SQLite) and list all the data in a list (I'm using listView).

So far I got a class called DatabaseHandler that extends SQLiteOpenHelper and that is doing all the database operations (select data, insert data, delete data, ...).

But now that I want to list the values, I am reading in some websites that I have to use a Loader instead of Cursor, and therefore a ContentProvider. So far I understand that ContentProvider provides controlled access to the database.

My question is: does the ContentProvider do the same as SQLiteOpenHelper?

Also, I'm using API level 8 and the ContentProvider is only available on API level 11. What is the best way to solve this?

Thanks in advance.

Answer

Alex Bonel picture Alex Bonel · Nov 27, 2012

My question is: does the ContentProvider do the same as SQLiteOpenHelper?

ContentProvider is implemented by application's developer if he or she would allow other developers to access to application's database in their application - simply put for sharing. It's like a server of some database and it's client is ContentResolver who knows ContentProvider's authority. For example if you need to get some contacts from your device, than you should use ContentProvider of Contacts database and more concretely it's Contract classes.

If you know an authority of appropriate ContentProvider you may comunicate with it using a ContentResolver object.

In other cases you should interact with database through SQL abstract model which is represented by android.database and android.database.sqlite classes.

And also - ContentProvider available from the primary API level as one of the main component of application.

Update

From the official documentation:

Before you start building a provider, do the following:

Decide if you need a content provider. You need to build a content provider if you want to provide one or more of the following features:

  • You want to offer complex data or files to other applications.
  • You want to allow users to copy complex data from your app into other apps.
  • You want to provide custom search suggestions using the search framework.