Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQLiteOpenHelper vs ContentProvider

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.

like image 667
André Alves Avatar asked Nov 27 '12 14:11

André Alves


2 Answers

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.
  • like image 65
    Alex Bonel Avatar answered Oct 05 '22 10:10

    Alex Bonel


    SQLiteOpenHelper is used to manage database creation and open. It is only a helper class for sqlite database and could be used in anywhere with db operation.

    ContentProvider is one of the 4 basic components of android, the other 3 are Activity, Service and Broadcast. ContentProvider is used to manage access to any structured data set, including sqlite database. The data source of ContentProvider could be a databse but not have to be.

    ContentProvider is often used in providing own data to others applications, just like ContactsProvider and CalendarProvider. And it is more safe to provide some specified interface using ContentProvider, compared with providing a database directly.

    like image 31
    faylon Avatar answered Oct 05 '22 10:10

    faylon