Exact Difference between "Content-Provider" and "SQLite Database"
Asked Answered
R

9

88

i have done SQLite database programming for Android, but i dont know anything about Content-Provider except this: "As i have referred Android Developer page , Android SDK explained about "Content-provider" as it is used to store and retrieve data."

But then,

  1. What is the exact difference between "Content-Provider" and "SQLite Database"?
  2. Which is best to store data, when ?

Any example or helps !!

Ruthy answered 28/7, 2010 at 6:30 Comment(0)
R
139

I found one major difference, as follows:

Storing your data in a database is one good way to persist your data, but there's a caveat in Android-databases created in Android are visible only to the application that created them. That is to say, a SQLite database created on Android by one application is usable only by that application, not by other applications.

So, if you need to share data between applications, you need to use the content provider model as recommended in Android. This article presents the basics of content providers and how you can implement one.

I found this article at this link

Really nice information provided.

Ruthy answered 29/7, 2010 at 12:50 Comment(4)
Seems like the link is dead now... dont see the article anymore. Would like to see the article you are referencing if you find it again.Spent
on feb 11 2012 the link http://www.devx.com/wireless/Article/41133 works,Somali
What if we provide mechanisms in the process to share the data across multiple applications in a thread safe manner?Medamedal
Another added advantage is that content providers use a single thread for every operation, so then multiple threads cannot modify the database like in an sqlite caseIrvingirwin
M
58

What is the exact difference between "Content-Provider" and "SQLite Database"?

ContentProvider is a facade -- an API you can implement that exposes databases to other processes. It can be implemented in a way where the data is stored in a SQLite database, but it does not have to be.

Which is best to store data, when ?

That is impossible to answer in the abstract. Generally speaking, unless something is requiring you to use a ContentProvider, just use a database.

Magnesite answered 28/7, 2010 at 6:38 Comment(1)
I prefer using a ContentProvider as it's a very nice abstraction over SQL. You can also play nice with CursorAdapter and automatic requeries.Alee
I
27

I have made many good apps with thousands of users using them which simply used SQLite methods. But that was a while ago and I had to manually write lots of code which now can easily be taken care of by ContentProvider. Back then I was not in favour of using Content Providers because it seemed to only add complexity in the code.

However for last couple of years, as Android has evolved, I have moved to ContentProvider as it saves time and allows you do to more. I now use it extensively. Once you have a Content Provider class written, your life becomes much easier. With ContentProvider I can much easily deal with Cursor Loaders, Loader Callbacks and Bulk Inserts for which I had to write everything manually in the past and still it didn't work as efficiently. Especially when updating the list view, which is now automatically updated thanks to just one notifychange() method. This means now I don't have to type my own listeners and manually updating the content in list views and adapters. Plus, I don't need to worry about opening and closing of databases or worry about memory leaks. That's all handled by the Content Provider. The only problem which once in a while I face is that that you cannot do some complex queries in ContentProviders. In this case you can still use raw queries and use the old fashioned manual interaction with sqlite.

If you have previously written your own DbAdapter, Helper and Observer, you can safely carry them on to your new apps without spending time to convert everything to ContentProvider. But based on my experience, I would highly recommend to move to ContentProvider. It'll take some time to get used to it, but once you have got experience with it, you'll stay with it.

UPDATE 2017 I have now switched to Realm, a much better way to use databases on any platform. Spend a few hours learning it, and save countless hours in your app development career.

Ivetteivetts answered 4/12, 2012 at 20:41 Comment(2)
I was thinking to convert my code to content provider but now I think of keeping it.Irs
Now You can also Add the 'Room' library of AndroidHavard
E
8

1. Content Providers are not Thread Safe

By default content providers are not thread safe. If you have multiple threads using a content provider you can see many different exceptions being thrown and other data inconsistencies. The easiest way to fix this is to use the synchronized keyword on each of the public methods exposed by the content provider.

In this way only one thread at a time can access these methods.

2. Play nice when doing lots of writes

I have the need in the new Serval Maps application to import data from binary files into the database used internally by the application. In order to do this and play nice with the rest of the application it is best to:

Spawn a new thread to undertake the import so other threads are not adversely impacted, in particularly the thread in charge of updating the UI; and Pause briefly at the end of the each import to give other threads which need to use the synchronized methods more of a chance.

3. Content providers force you to think laterally sometimes

The way that content providers in Android work is to provide a layer of abstraction between the rest of your code and the underlying database. This is mainly due to the fact, as far as I can tell, that content providers can access data from places other than databases.

This means that you can’t execute raw SQL queries on the underlying database and you need to specify the various components of a SQL query using variables passed to the various methods such as the query method. If you have a task that doesn’t fit into the way that SQL is handled by a content provider you have two options:

Think laterally about the query, maybe you can get the data that you need by alternative queries and accessing the results from the cursor; and Use a URI for accessing the data normally and a special URI that is matched to a specific query for those tasks that don’t have alternatives.

Earflap answered 21/11, 2014 at 5:32 Comment(1)
as per ContentProvider six abstract methods of ContentProvider that have to be implemented can be called by multiple threads at once, so they must be implemented as thread-safe. Abstract class ContentProvider is not the reason of exceptions of ones particular code but the implementation. Follow Processes and Threads to implement thread safe methods of ContentProvider.Rivers
E
5

Content Providers are used when you want to share your data across applications.

If you have a database attached with an application and you want another application to use some data, you can implement a content provider that exposes the data

Exceptive answered 28/7, 2010 at 6:49 Comment(0)
B
3

The main difference is: when your app needs to share information to another apps, use Content-Provider. SQLite only storage data for the app who creates it

Biquarterly answered 4/12, 2012 at 18:2 Comment(0)
U
3

I read this answer while looking for same doubt, so thought of sharing it. it states -

It's good practice to provide the extra level of abstraction over your data to make it easier to change internally. What if you decide to change the underlying database structure at a later time? If you use a ContentProvider you can contain all the structural changes within it, where as if you don't use one, you are forced to change all areas of the code that are affected by the structural changes. Besides, it's nice to be able to re-use the same standard API for accessing data rather than littering your code with low-level access to the database.

So, using a content provider would be a good idea.

Unprincipled answered 12/5, 2015 at 11:19 Comment(0)
P
3

Think of advanced Content Management Systems. Each object (page, image, news article, event item, etc.) has a content, an address, user permissions, and ways to interact with it from different parts of the system. Content Providers do that for Android. You can now share files or images you may have stored in your application. You can also create custom sharable objects, like bussiness contacts, editable notes, etc. And specify security and the default application to deal with such object when you open them from any other application.

Pinfold answered 28/6, 2015 at 13:34 Comment(0)
B
1

One difference is that Content Providers have platform support for Content Observers. Your going to need to implement your own Observable pattern for a SQLite database.

How to automatically re-query with LoaderManager

ContentObserver for SQLite?

Burnsed answered 28/12, 2012 at 16:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.