<分区>
我们可以在不使用 Uri 的情况下将数据插入数据库。但是当我们使用 contentResolver 时,我们需要 Uri 来做任何事情。
像 getContentResolver().insert(CONTENT_URI, values);
但是在数据库查询执行中我们不需要任何 Uri。像 db.execSQL(String query);
。这是为什么?
<分区>
我们可以在不使用 Uri 的情况下将数据插入数据库。但是当我们使用 contentResolver 时,我们需要 Uri 来做任何事情。
像 getContentResolver().insert(CONTENT_URI, values);
但是在数据库查询执行中我们不需要任何 Uri。像 db.execSQL(String query);
。这是为什么?
最佳答案
虽然我认为这个问题即将结束,但我认为您要问的是为什么在使用内容解析器时必须使用 CONTENT_URI,而在使用数据库时却不用?
这是因为内容解析器提供的功能不仅仅是数据库
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/