使用 SQLiteOpenHelper 的 Android 和 SQLite

发布于 2024-09-05 01:12:19 字数 571 浏览 9 评论 0 原文

我有一个 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.

Reference Material

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

北斗星光 2024-09-12 01:12:19

有没有典型的写作解决方案
一个助手也可以看到表
存在吗?

SELECT COUNT(*) FROM sqlite_master WHERE type='table' AND name='...' ORDER BY name;

(用您的表名称替换 ...

如果返回 1,则您的表存在。如果返回 0,则您的表不存在。

然而,助手不会帮助你。出于测试目的,我建议摆脱整个数据库,因为在生产中,您也不会动态创建单独的表。

Is there a typical solution to writing
a helper to also see that the table(s)
exists?

SELECT COUNT(*) FROM sqlite_master WHERE type='table' AND name='...' ORDER BY name;

(substituting in your table's name for ...)

If that returns 1, your table exists. If that returns 0, 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.

烟酒忠诚 2024-09-12 01:12:19

有时候,我想删除一个表
这样我就可以确保它正在建造
正确地,从头开始。

我有一个仪器测试,它会删除并重新创建所有表。多做一点工作,但一旦完成就会更有帮助。

有没有典型的写作解决方案
一个助手也可以看到表
存在吗?

除了 CommonsWare 建议之外,还有一个选项可以使用 shell 命令来获取此信息。

adb -e shell sqlite3 -batch ${DB_FILE} .table
adb -e shell sqlite3 -batch ${DB_FILE} .schema

Occasionally, I want to drop a table
so that I can ensure it's being built
properly, from scratch.

I have a instrumentation test which drops and recreates all tables. A little more work but once done a lot more helpful.

Is there a typical solution to writing
a helper to also see that the table(s)
exists?

In addition to what CommonsWare suggested there is also a option to use shell command to get this informations.

adb -e shell sqlite3 -batch ${DB_FILE} .table
adb -e shell sqlite3 -batch ${DB_FILE} .schema
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文