The exact difference between the "Content-Provider" and the "SQLite Database"

I did SQLite to program the databases for Android, but I don’t know anything about the Content Provider, except for this: "As I said the Android Developer Page, the Android SDK explained" Content-provider "because it is used to store and retrieve data".

But then

  • What is the difference between the "Content-Provider" and the "SQLite Database"?
  • What is the best way to store data when?

Any example or help!

+82
android android-contentprovider sqlite
Jul 28 '10 at 6:30
source share
9 answers

I found one significant difference:

Saving your data in a database is one of the good ways to save data , but in Android databases created on Android, there is a visible clause only for the application that created them. That is, the SQLite database created on Android by one application can be used only by this application, and 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 introduces the basics of content providers and how to implement them.

I found this article on this link

Really nice information.

+130
Jul 29 '10 at 12:50
source share

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

ContentProvider is a facade - an API that you can implement that provides databases to other processes. It can be implemented in such a way that the data is stored in a SQLite database, but this is not necessary.

What is the best way to store data when?

It is impossible to answer abstractly. Generally speaking, unless something requires the use of a ContentProvider , just use the database.

+53
Jul 28 '10 at 6:38
source share

I have made many good applications with thousands of users using them that simply used SQLite methods. But it was a long time ago, and I had to manually write a lot of code, which can now be easily taken care of by ContentProvider. At that time, I was not a proponent of using Content Providers because it seemed to only complicate the code.

However, over the past couple of years, when Android evolved, I switched to ContentProvider, as it saves time and allows you to do more. Now I use it widely. When you have the Content Provider class, your life will become much simpler. With ContentProvider, I can easily deal with Cursor Loaders, Loader Callbacks and Bulk Inserts, for which I had to write everything manually in the past, and yet it did not work as efficiently. Especially when updating the list, which is now automatically updated thanks to a single method notifychange (). This means that now I do not need to introduce my own listeners and manually update the content in the form of a list and adapters. In addition, I do not need to worry about opening and closing databases or worry about memory leaks. All of this is handled by the Content Provider. The only problem I am facing is that you cannot execute complex queries in ContentProviders. In this case, you can still use raw queries and use the old-fashioned manual interaction with sqlite.

If you previously wrote your own DbAdapter, Helper and Observer, you can safely transfer them to your new applications without wasting time converting everything into ContentProvider. But based on my experience, I highly recommend switching to ContentProvider. It will take some time to get used to this, but as soon as you have experience, you will remain with it.

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

+22
Dec 04
source share

1. Content providers are not thread safe

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

Thus, access to these methods can only get one thread at a time.

2. Play nicely with a lot of recordings

I have a need for a new Serval Maps application to import data from binary files into the database used inside the application. To do this and play well with the rest of the application, the best thing is:

Create a new thread for import so that other threads do not suffer, especially in the thread responsible for updating the user interface; and also Suspend briefly at the end of each import to give other threads that should use synchronized methods more.

3. Content providers make you think sometimes

The way content providers on Android work provides a level of abstraction between the rest of your code and the underlying database. This is mainly due to how much I can say that content providers can access data from places other than databases.

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

Think towards the query, perhaps you can get the data you need for alternative queries and access the results from the cursor; and Use a URI for regular data access and a special URI that matches a specific query for tasks that have no alternatives.

+8
Nov 21 '14 at 5:32
source share

Content providers are used when you want to share your data between applications.

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

+5
Jul 28 '10 at 6:49
source share

The main difference: when your application needs to exchange information with other applications, use the Content-Provider. SQLite only data storage for the application that creates it

+3
Dec 04 2018-12-12T00:
source share

I read this answer , looking for the same doubts, so I thought about sharing. he claims -

It’s good practice to provide an extra layer of abstraction over your data to facilitate internal change. What if you decide to change the basic structure of the database later? If you use a ContentProvider, you can contain all the structural changes inside it, where, as if you hadn't used it, you are forced to change all areas of the code that are affected by the structural changes. It’s also nice to be able to reuse the same standard API to access data, rather than clog your code with low-level database access.

So using a content provider would be a good idea.

+3
May 12 '15 at 11:19
source share

Think of advanced content management systems. Each object (page, image, news article, event element, etc.) has content, address, user permissions and ways to interact with it from different parts of the system. Content providers do this for Android. Now you can share files or images that you could save in your application. You can also create custom shared objects such as business contacts, editable notes, etc. And specify the security and default application for working with such an object when you open them from any other application.

+3
Jun 28 '15 at 13:34
source share

One of the differences is that content providers support a platform for content observers. You will need to implement your own Observable pattern for the SQLite database.

How to automatically retry using LoaderManager

ContentObserver for SQLite?

+1
Dec 28
source share



All Articles