Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: access SQLite database via Content Provider or implement DAO?

I'm wondering which is the best approach to access my application database: use a Content Provider, or implement my DAO by hand? From my latest investigations, seems that Content Provider, even for app internal use, is preferable, but I don't know exactly what are the drawbacks of each approach. Can you give some feedback about this?

like image 516
user1135437 Avatar asked Nov 24 '12 16:11

user1135437


3 Answers

I prefer to use ContentProvider if you have concerns of closing or locking of db. Check Simple Content Provider for db operations

like image 172
Labeeb Panampullan Avatar answered Nov 06 '22 09:11

Labeeb Panampullan


From Google Docs.

Before You Start Building

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.

You don't need a provider to use an SQLite database if the use is entirely within your own application.

But then I got a bit confused when reading this and some other posts. Does it make sense to use content provider event if it is meant to be used only by your own app?

like image 10
MRodrigues Avatar answered Nov 06 '22 09:11

MRodrigues


From the ContentProvider documentation:

Content providers are one of the primary building blocks of Android applications, providing content to applications. They encapsulate data and provide it to applications through the single ContentResolver interface. A content provider is only required if you need to share data between multiple applications. For example, the contacts data is used by multiple applications and must be stored in a content provider. If you don't need to share data amongst multiple applications you can use a database directly via SQLiteDatabase.

Seems to me that, if you're not going to share data with other applications, you do not need a content provider.

Link: http://developer.android.com/reference/android/content/ContentProvider.html

like image 5
Lu Araujo Avatar answered Nov 06 '22 10:11

Lu Araujo