Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
536 views
in Technique[技术] by (71.8m points)

sqlite - 如何在SQLite中检查表是否存在?(How do I check in SQLite whether a table exists?)

How do I, reliably , check in SQLite, whether a particular user table exists?

(我如何可靠地在SQLite中检查特定的用户表是否存在?)

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?).

(我不是在要求不可靠的方法,例如检查表上的“ select *”是否返回错误(这甚至是个好主意吗?)。)

The reason is like this:

(原因是这样的:)

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?

(还是我的方法有意义?)

  ask by PoorLuzer translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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}';

Where {table_name} is the name of the table to check.

(其中{table_name}是要检查的表的名称。)

Documentation section for reference: Database File Format.

(供参考的文档部分: 数据库文件格式。)

2.6.

(2.6。)

Storage Of The SQL Database Schema

(SQL数据库架构的存储)

  • This will return a list of tables with the name specified;

    (这将返回具有指定名称的表的列表;)

    that is, the cursor will have a count of 0 (does not exist) or a count of 1 (does exist)

    (也就是说,游标的计数为0(不存在)或计数为1(存在))


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...