SQLiteOpenHelper vs ContentProvider

点点圈 提交于 2019-12-03 08:58:27

问题


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.


回答1:


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.



  • 回答2:


    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.



    来源:https://stackoverflow.com/questions/13586262/sqliteopenhelper-vs-contentprovider

    易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
    该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!