“内容提供商”与“内容提供商”之间的确切区别和“SQLite数据库”

发布于 2024-09-11 16:48:27 字数 353 浏览 2 评论 0原文

我已经为 Android 完成了 SQLite 数据库编程,但我对此一无所知 内容提供商除外:“正如我所提到的 Android 开发者页面 ,Android SDK 解释了“Content-provider”,因为它用于存储和检索数据。”

但是,

  1. “Content-Provider”和“SQLite Database”之间的确切区别是什么?
  2. 什么时候最适合存储数据?

任何例子或帮助!

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 !!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

梦醒灬来后我 2024-09-18 16:48:27

我发现了一个主要区别,如下所示:

将数据存储在数据库中是保存数据的一种好方法,但 Android 中有一个警告 - 在 Android 中创建的数据库是可见的 仅适用于创建它们的应用程序。也就是说,一个应用程序在 Android 上创建的 SQLite 数据库只能由该应用程序使用,而不能被其他应用程序使用。

因此,如果您需要在应用程序之间共享数据,则需要使用 Android 中推荐的内容提供程序模型。本文介绍了内容提供程序的基础知识以及如何实现实施一。

我在这个链接找到了这篇文章,

提供了非常好的信息。

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.

〆一缕阳光ご 2024-09-18 16:48:27

两者之间的确切区别是什么
“内容提供者”和“SQLite
数据库”?

ContentProvider 是一个外观——您可以实现的一个 API,它将数据库公开给其他进程。它可以以将数据存储在 SQLite 中的方式实现数据库,但并非必须如此。

什么时候最适合存储数据?

这是不可能抽象地回答的。一般来说,除非需要使用 ContentProvider,否则只需使用数据库即可。

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.

怪我鬧 2024-09-18 16:48:27

我已经制作了许多优秀的应用程序,有成千上万的用户使用它们,这些应用程序只使用 SQLite 方法。但那是不久前的事了,我不得不手动编写大量代码,现在 ContentProvider 可以轻松处理这些代码。当时我并不赞成使用内容提供程序,因为它似乎只会增加代码的复杂性。

然而,在过去几年中,随着 Android 的发展,我已经转向 ContentProvider,因为它可以节省时间并允许您做更多事情。我现在广泛使用它。一旦编写了 Content Provider 类,您的生活就会变得更加轻松。借助 ContentProvider,我可以轻松处理游标加载器、加载器回调和批量插入,过去我必须手动编写所有内容,但仍然无法高效工作。特别是在更新列表视图时,现在只需一个 notificationchange() 方法即可自动更新。这意味着现在我不必键入自己的侦听器并手动更新列表视图和适配器中的内容。另外,我不需要担心数据库的打开和关闭或内存泄漏。这一切都由内容提供商处理。我偶尔遇到的唯一问题是您无法在 ContentProvider 中执行一些复杂的查询。在这种情况下,您仍然可以使用原始查询并使用与 sqlite 的老式手动交互。

如果您之前编写过自己的 DbAdapter、Helper 和 Observer,则可以安全地将它们带到您的新应用程序中,而无需花时间将所有内容转换为 ContentProvider。但根据我的经验,我强烈建议转向 ContentProvider。需要一些时间来适应它,但一旦你有了使用它的经验,你就会坚持下去。

2017 年更新
我现在已切换到Realm,这是在任何平台上使用数据库的更好方法。花几个小时学习它,可以在您的应用程序开发生涯中节省无数时间。

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.

旧人 2024-09-18 16:48:27

1. 内容提供程序不是线程安全的

默认情况下,内容提供程序不是线程安全的。如果您有多个线程使用内容提供程序,您会看到抛出许多不同的异常以及其他数据不一致的情况。解决此问题的最简单方法是在内容提供程序公开的每个公共方法上使用同步关键字。

这样,一次只有一个线程可以访问这些方法。

2. 在进行大量写入时保持良好状态

我需要在新的 Serval Maps 应用程序中将二进制文件中的数据导入到应用程序内部使用的数据库中。为了做到这一点并与应用程序的其余部分良好配合,最好:

生成一个新线程来进行导入,这样其他线程就不会受到不利影响,特别是负责更新 UI 的线程;和
在每次导入结束时短暂暂停,以便为需要使用同步方法的其他线程提供更多机会。

3. 内容提供程序有时会迫使您进行横向思考

Android 中的内容提供程序的工作方式是在其余代码和底层数据库之间提供一个抽象层。据我所知,这主要是因为内容提供商可以从数据库以外的地方访问数据。

这意味着您无法在底层数据库上执行原始 SQL 查询,并且需要使用传递给各种方法(例如查询方法)的变量来指定 SQL 查询的各个组成部分。如果您的任务不适合内容提供程序处理 SQL 的方式,您有两个选择:

横向考虑查询,也许您可​​以通过替代查询并访问结果来获取所需的数据。光标;和
使用 URI 来正常访问数据,并使用与特定查询相匹配的特殊 URI 来处理那些没有替代方案的任务。

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.

眼眸 2024-09-18 16:48:27

当您想要跨应用程序共享数据时,可以使用内容提供程序。

如果您有一个与应用程序附加的数据库,并且希望另一个应用程序使用某些数据,则可以实现公开数据的内容提供程序

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

禾厶谷欠 2024-09-18 16:48:27

主要区别是:当您的应用程序需要与其他应用程序共享信息时,请使用Content-Provider。 SQLite 仅为创建它的应用程序存储数据

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

一刻暧昧 2024-09-18 16:48:27

我在寻找同样的疑问时阅读了这个答案,所以想分享它。
它指出——

最好的做法是对数据提供额外的抽象级别,以便更容易进行内部更改。如果您决定稍后更改底层数据库结构怎么办?如果您使用 ContentProvider,则可以在其中包含所有结构更改,而就像您不使用 ContentProvider 一样,您将被迫更改受结构更改影响的代码的所有区域。此外,能够重用相同的标准 API 来访问数据,而不是用对数据库的低级访问来乱扔代码,这真是太好了。

因此,使用内容提供商将是一个好主意。

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.

画▽骨i 2024-09-18 16:48:27

想想先进的内容管理系统。每个对象(页面、图像、新闻文章、事件项等)都有内容、地址、用户权限以及从系统的不同部分与其交互的方式。内容提供商为 Android 执行此操作。您现在可以共享您可能存储在应用程序中的文件或图像。您还可以创建自定义可共享对象,例如业务联系人、可编辑注释等。并指定安全性和当您从任何其他应用程序打开此类对象时处理此类对象的默认应用程序。

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.

玩心态 2024-09-18 16:48:27

一个区别是内容提供商具有对内容观察者的平台支持。您将需要为 SQLite 数据库实现您自己的 Observable 模式。

如何使用LoaderManager自动重新查询

用于 SQLite 的 ContentObserver?

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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文