使用 SQLiteOpenHelper 的 Android 和 SQLite
我有一个 SQLite 数据库,以及该数据库中的几个表。我正在为数据库中的每个表开发一个 DBAdapter。 (参考 Reto Meier 的专业 Android 2 应用程序开发,清单 7.1)。
我正在使用 adb shell 从命令行与数据库交互,并看到数据库正在按我的预期填充。有时,我想删除一个表,以便我可以确保它从头开始正确构建。
问题是 SQLiteOpenHelper 仅检查数据库是否存在。是否有一个典型的解决方案来编写一个助手来查看表是否存在?基本上,一旦我删除一个表,助手就会检查数据库是否存在并假设一切正常。
此外,上面参考中使用的 CREATE_DATABASE 字符串仅创建一个表。我是否应该考虑使用 DBAdapter 作为我所有表的适配器?这对我来说似乎不太干净。
I have a SQLite database, and several tables within that datbase. I am developing a DBAdapter for each table within the database. (reference Reto Meier's Professional Android 2 Application Development, Listing 7.1).
I am using the adb shell to interface with the database from the command line and see that the database is being populated as I expect. Occasionally, I want to drop a table so that I can ensure it's being built properly, from scratch.
The problem is that SQLiteOpenHelper only checks to see if the database exists. Is there a typical solution to writing a helper to also see that the table(s) exists? Basically once I drop a table, the helper checks to see that the database exists and assumes all is well.
Also, the CREATE_DATABASE string used in the reference above only creates the one table. Should I consider using the DBAdapter for an adapter to ALL of my tables? That doesn't seem as clean to me.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
(用您的表名称替换
...
)如果返回
1
,则您的表存在。如果返回0
,则您的表不存在。然而,助手不会帮助你。出于测试目的,我建议摆脱整个数据库,因为在生产中,您也不会动态创建单独的表。
(substituting in your table's name for
...
)If that returns
1
, your table exists. If that returns0
, your table does not exist.However, the helper won't help you with that. For testing purposes, I recommend getting rid of the entire database, since in production, you won't be creating individual tables on the fly, either.
我有一个仪器测试,它会删除并重新创建所有表。多做一点工作,但一旦完成就会更有帮助。
除了 CommonsWare 建议之外,还有一个选项可以使用 shell 命令来获取此信息。
I have a instrumentation test which drops and recreates all tables. A little more work but once done a lot more helpful.
In addition to what CommonsWare suggested there is also a option to use shell command to get this informations.