内容提供者和 sqlite 数据库、uri 语法
几天来我一直未能成功尝试通过内容提供商访问数据库。
我认为我不理解用于指向数据库的 Uri 的语法。也许我对Android开发者文档中关于Uri格式的理解不正确。
我有一个名为“testdatabase.db”的数据库、一个名为“table1”的表和一个名为 DbProvider
的类,该类是 ContentProvider
的子类。数据库已成功创建,我可以使用 adb shell 对其进行查询,但是当我尝试从客户端进行查询时,我收到 IllegalArgumentException Unknown uri: content://com.test.dbprovider/testdatabase.db
有人可以解释一下我的情况下 Uri 的正确格式是什么吗?提供者的权限部分的语法是什么?可以将权限命名为例如 com.test.foo
并且它仍然可以工作,或者点后的最后一部分应该是类名或类似的名称吗?
我还在 AndroidManifest.xml
文件中添加了必要的提供者标签。
I have been unsuccessfully trying to access database trough content provider for couple of days now.
I think that I don't understand the syntax of the Uri that is used to point to the database. Maybe I don't understand correctly the Android developer's documentation about the format of Uri.
I have a database which name is "testdatabase.db", table which name is "table1" and a class named DbProvider
that subclasses ContentProvider
. The database is created successfully and I can make queries to it using adb shell, but when I try to make queries from client, I got IllegalArgumentException Unknown uri: content://com.test.dbprovider/testdatabase.db
Could somebody please explain what is the correct format of Uri in my case? And what is the syntax of authority part of provider? Can an authority be named for example com.test.foo
and it should still work or should the last part after dot be a class name or something like that?
I have also made the necessary provider tags to AndroidManifest.xml
file.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您收到异常是因为您的内容提供商不知道您指定的 uri。由于您还没有显示您的代码,我无法说出到底出了什么问题。检查内容提供程序中的 UriMatcher 实例(您正在使用 UriMatcher,对吗?如果没有,请检查 docs。)
我想您对内容提供商中的 uri 没有什么误解。通常,您不会在 uri 中使用甚至公开数据库名称(尽管如果您愿意,也可以这样做)。 uri 应该是数据库中记录的路径。在您的示例中,您可以使用类似
content://com.text.dbprovider/table1
的内容。权限应该是内容提供者的完全限定类名。有关内容提供商 uri 的更多信息,请参见此处。我建议您检查 NotePad 内容提供程序实现 在样本中。
You are getting the exception because your content provider doesn't know the uri you specified. Since you haven't shown your code I can't say what is exactly wrong. Check the instance of UriMatcher in your content provider (You are using UriMatcher, right? If not check the docs.)
I guess you little misunderstood the uri in content provider. Normally, you don't use or even expose the database name in the uri (although you can if you like to). The uri is supposed to be a path to records in your database. In your example you could use something like
content://com.text.dbprovider/table1
. The authority should be fully-qualified class name of the content provider. More about content providers' uris here.I suggest you check the NotePad content provider implementation in samples.
DB是使用权限来引用的。所以
com.test.dbprovider.testdatabase
。不要输入“.db”还要确保您的权限与您的包中指定的完全一致。因此,如果您的包是
com.test.contentprovider
,请不要随机添加“dbprovider”。确保 android 清单中的权限相同。
摘要
content://com.text.dbprovider.testdatabase/table1
The DB is referred to using the authority. so
com.test.dbprovider.testdatabase
. do not put ".db"also ensure your authority is exactly as specified as your package. so if you package is
com.test.contentprovider
do not add "dbprovider" randomly.ensure the authority is identical in the android manifest.
summary
content://com.text.dbprovider.testdatabase/table1