SQLiteOpenHelper vs ContentProvider
Asked Answered
A

2

16

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.

Acuate answered 27/11, 2012 at 14:21 Comment(5)
Can't answer on the best way, but the pragmatic way is to use a Cursor. It still works perfectly well, and you save a lot of development time if you don't need the additional features provided by the ContentProvider.Lecce
@Lecce It would be good to use Cursor as they seem simpler. The problem is that when using startmanagingcursor(cursor) it says it's deprecated. Also I keep reading that loaders are better due to the data synchronization issues...Glitter
SQLiteOpenHelper is a helper class intended to use within ContentProviderDoorn
Also, I'm using API level 8 and the ContentProvider is only available on API level 11. What is the best not true ... Loaders are from API 11 but we have support library v4 ... ContentProviders are from API 1 :)Doorn
You're right! I totally mixed them up! :P So the best solution is to use support libraries?Glitter
B
10

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.
  • Bennybenoit answered 27/11, 2012 at 14:35 Comment(8)
    This is the main purpose of ContentProvider - to export it to other applications. If you don't need to export your data to another applications than you shouldn't consider to use ContentProvider.Bennybenoit
    No... main purpose is to manage access to a structured set of data(i don't see single "export" word there). I can even say ... this is prefered way of accesing data(even SQLite) on android!Doorn
    In my case I don't want to export to other applications. I just want to access database data internally and list it. The problem is that when I am trying to use Cursor and startmanagingcursor(cursor), it says that this is deprecated and that I should replace it by a Loader. And as far as I know, a Loader needs a ContentProvider, that's why I am confused...Glitter
    Edited my answer for better explanation basing on official documentationBennybenoit
    So what should I do if I don't want to use ContentProvider (because I don't need it) but I want to list all the data in the database?Glitter
    if you wana: no auto requery support, cursors leaking, sqlopenhelper leak, manual guarding for multiple threads access to db, no Loaders support then you can use plain SQLite ...Doorn
    Intention of ContentProvider was to serve data to other processes via a defined interface. But things changed with Honeycomb. All the deprecated methods like startManagingCursor tell you to use a Loader now. A CursorLoader does not work without a ContentProvider and Uri based change notifications that cause automatic reloading in a background thread (#1 reason for Loaders) won't work either. There are probably ways around but ContentProvider is definitely the way you should consider now. also androiddesignpatterns.com/2012/07/…Viscid
    So I guess I need a ContentProvider then... Thanks a lot for your answers! :)Glitter
    N
    9

    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.

    Nogas answered 27/11, 2012 at 14:58 Comment(0)

    © 2022 - 2024 — McMap. All rights reserved.