Android “内容提供者”和“SQLite 数据库”之间的确切区别
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3350408/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Exact Difference between "Content-Provider" and "SQLite Database"
提问by Paresh Mayani
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."
我已经为 Android 完成了 SQLite 数据库编程,但我对内容提供程序一无所知:“正如我提到的Android 开发人员页面,Android SDK 解释了“内容提供程序”,因为它用于存储和检索数据。”
But then,
但是之后,
- What is the exact difference between "Content-Provider" and "SQLite Database"?
- Which is best to store data, when ?
- “内容提供者”和“SQLite 数据库”之间的确切区别是什么?
- 哪个最适合存储数据,什么时候?
Any example or helps !!
任何例子或帮助!
回答by Paresh Mayani
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.
将数据存储在数据库中是一种保存数据的好方法,但在 Android 中需要注意的是,在 Android 中创建的数据库visible
仅适用于创建它们的应用程序。也就是说,一个应用程序在 Android 上创建的 SQLite 数据库只能被该应用程序使用,而不能被其他应用程序使用。
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.
因此,如果您need to share data between applications, you need to use the content provider model as recommended in Android.
在本文中介绍了内容提供程序的基础知识以及如何实现内容提供程序。
I found this article at this link
我在这个链接上找到了这篇文章
Really nice information provided.
提供的信息非常好。
回答by CommonsWare
What is the exact difference between "Content-Provider" and "SQLite Database"?
“内容提供者”和“SQLite 数据库”之间的确切区别是什么?
ContentProvider
is a facade -- an API you can implement that exposes databases to other processes. It canbe implemented in a way where the data is stored in a SQLite database, but it does not have to be.
ContentProvider
是一个外观——一个你可以实现的 API,它将数据库暴露给其他进程。它可以通过将数据存储在 SQLite 数据库中的方式来实现,但并非必须如此。
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.
这是不可能抽象地回答的。一般来说,除非有什么要求您使用 ,否则ContentProvider
只需使用数据库。
回答by zeeshan
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.
我制作了许多优秀的应用程序,成千上万的用户使用它们,这些应用程序只是使用 SQLite 方法。但那是前一段时间,我不得不手动编写大量代码,现在 ContentProvider 可以轻松处理这些代码。当时我不赞成使用内容提供者,因为它似乎只会增加代码的复杂性。
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.
然而,在过去的几年里,随着 Android 的发展,我转向了 ContentProvider,因为它可以节省时间并允许您做更多的事情。我现在广泛使用它。一旦你编写了一个 Content Provider 类,你的生活就会变得容易得多。使用 ContentProvider,我可以很容易地处理游标加载器、加载器回调和批量插入,过去我必须手动编写所有内容,但仍然没有那么有效。特别是在更新列表视图时,由于只有一个 notifychange() 方法,它现在会自动更新。这意味着现在我不必键入自己的侦听器并手动更新列表视图和适配器中的内容。另外,我不需要担心打开和关闭数据库或担心内存泄漏。这一切都由内容提供者处理。我偶尔遇到的唯一问题是您无法在 ContentProviders 中进行一些复杂的查询。在这种情况下,您仍然可以使用原始查询并使用老式的手动交互与 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.
如果您之前已经编写了自己的 DbAdapter、Helper 和 Observer,您可以安全地将它们带到您的新应用程序中,而无需花时间将所有内容转换为 ContentProvider。但根据我的经验,我强烈建议改用 ContentProvider。习惯它需要一些时间,但是一旦你有了它的经验,你就会坚持下去。
UPDATE 2017I 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.
更新 2017我现在已经切换到Realm,这是在任何平台上使用数据库的更好方法。花几个小时学习它,并在您的应用程序开发生涯中节省无数时间。
回答by chanu
1. Content Providers are not Thread Safe
1. 内容提供者不是线程安全的
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.
默认情况下,内容提供程序不是线程安全的。如果您有多个线程使用内容提供程序,您会看到许多不同的异常被抛出和其他数据不一致。解决此问题的最简单方法是在内容提供者公开的每个公共方法上使用 synchronized 关键字。
In this way only one thread at a time can access these methods.
这样一次只有一个线程可以访问这些方法。
2. Play nice when doing lots of writes
2. 进行大量写入时表现良好
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:
我需要在新的 Serval Maps 应用程序中将数据从二进制文件导入到应用程序内部使用的数据库中。为了做到这一点并与应用程序的其余部分一起玩,最好:
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.
生成一个新线程来进行导入,这样其他线程不会受到不利影响,尤其是负责更新 UI 的线程;并在每次导入结束时短暂暂停,以便为需要使用同步方法的其他线程提供更多机会。
3. Content providers force you to think laterally sometimes
3. 内容提供者有时迫使你横向思考
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.
Android 中内容提供程序的工作方式是在其余代码和底层数据库之间提供一个抽象层。这主要是因为,据我所知,内容提供者可以从数据库以外的地方访问数据。
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:
这意味着您无法在底层数据库上执行原始 SQL 查询,您需要使用传递给各种方法(例如查询方法)的变量来指定 SQL 查询的各个组件。如果您的任务不符合内容提供者处理 SQL 的方式,您有两个选择:
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.
横向考虑一下查询,也许你可以通过替代查询和访问游标的结果来获取你需要的数据;使用 URI 来正常访问数据,并使用与特定查询匹配的特殊 URI 来处理没有替代方案的任务。
回答by Mina Samy
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
如果您有一个应用程序附加的数据库,并且您希望另一个应用程序使用某些数据,则可以实现一个公开数据的内容提供程序
回答by daniel uribe ayvar
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
主要区别在于:当您的应用程序需要与其他应用程序共享信息时,请使用 Content-Provider。SQLite 只为创建它的应用程序存储数据
回答by Darpan
I read this answerwhile 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.
为您的数据提供额外的抽象级别以使其更容易在内部更改是一种很好的做法。如果您决定稍后更改底层数据库结构怎么办?如果您使用 ContentProvider,您可以在其中包含所有结构更改,而如果您不使用一个,您将被迫更改受结构更改影响的所有代码区域。此外,能够重用相同的标准 API 来访问数据而不是通过对数据库的低级别访问来乱扔代码是很好的。
So, using a content provider would be a good idea.
因此,使用内容提供程序将是一个好主意。
回答by Roberto
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.
想想高级内容管理系统。每个对象(页面、图像、新闻文章、事件项等)都有内容、地址、用户权限以及从系统的不同部分与其交互的方式。内容提供者为 Android 做到这一点。您现在可以共享您可能存储在应用程序中的文件或图像。您还可以创建自定义的可共享对象,如业务联系人、可编辑的笔记等。并指定安全性和默认应用程序,以便在您从任何其他应用程序打开这些对象时处理这些对象。
回答by user1538028
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.
一个区别是内容提供者具有对内容观察者的平台支持。您将需要为 SQLite 数据库实现自己的 Observable 模式。