android - 为什么内容提供者使用 URI 而不是数据库?

标签 android sqlite

<分区>

我们可以在不使用 Uri 的情况下将数据插入数据库。但是当我们使用 contentResolver 时,我们需要 Uri 来做任何事情。

getContentResolver().insert(CONTENT_URI, values); 但是在数据库查询执行中我们不需要任何 Uri。像 db.execSQL(String query);。这是为什么?

最佳答案

虽然我认为这个问题即将结束,但我认为您要问的是为什么在使用内容解析器时必须使用 CONTENT_URI,而在使用数据库时却不用?

Content Provider

这是因为内容解析器提供的功能不仅仅是数据库

Content providers store and retrieve data and make it accessible to all applications. They're the only way to share data across applications; there's no common storage area that all Android packages can access.

Android ships with a number of content providers for common data types (audio, video, images, personal contact information, and so on). You can see some of them listed in the android.provider package. You can query these providers for the data they contain (although, for some, you must acquire the proper permission to read the data).

When a query is initiated, the Android system identifies the content provider that's the target of the query and makes sure that it is up and running.

数据库

对于数据库,任何应用程序都无法通过内容提供程序访问它(除非您编写一个)。使用 db.execSQL,您只查询您的数据库而不是内容提供者,因此您不需要提供 CONTENT_URI。您的 db 对象是数据存储。

关于android - 为什么内容提供者使用 URI 而不是数据库?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8245737/

相关文章:

android - 使用 OnTouchListener 和 OnLongClickListener 相互干扰

java - 降低功耗

android - 尝试访问继承字段时出现 NullPointerException

Android - 如何使 Activity 加载更快?

java - Android ListView 不会填充 SQLite DB 中的所有条目

c# - SQLite 与 Web 服务 c#

c# - 如何使用 EF 在 SQLite 数据库中检索和设置 user_version

android - Android 应用程序是否可以自动安装和运行更新?

sql - 如何根据WHERE条件计算值连续出现的次数?

sqlite - Couchbase Lite 如何使用 SQlite 处理 NoSQL 数据