问题描述
我如何可靠地检查 SQLite 中是否存在特定的用户表?
How do I, reliably, check in SQLite, whether a particular user table exists?
我不是要求不可靠的方法,例如检查表上的select *"是否返回错误(这是否是一个好主意?).
I am not asking for unreliable ways like checking if a "select *" on the table returned an error or not (is this even a good idea?).
原因是这样的:
在我的程序中,我需要创建并填充一些不存在的表.
In my program, I need to create and then populate some tables if they do not exist already.
如果它们已经存在,我需要更新一些表.
If they do already exist, I need to update some tables.
我是否应该采用其他路径来表示相关表已经创建 - 例如,通过在磁盘上的程序初始化/设置文件中创建/放置/设置某个标志或其他方式?
Should I take some other path instead to signal that the tables in question have already been created - say for example, by creating/putting/setting a certain flag in my program initialization/settings file on disk or something?
或者我的方法有意义吗?
Or does my approach make sense?
推荐答案
我错过了那个常见问题条目.
I missed that FAQ entry.
无论如何,为了将来参考,完整的查询是:
Anyway, for future reference, the complete query is:
SELECT name FROM sqlite_master WHERE type='table' AND name='{table_name}';
其中 {table_name}
是要检查的表的名称.
Where {table_name}
is the name of the table to check.
文档部分供参考:数据库文件格式.2.6.SQL 数据库架构的存储
- 这将返回具有指定名称的表列表;也就是说,游标的计数为 0(不存在)或计数为 1(存在)
这篇关于如何在 SQLite 中检查表是否存在?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!