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

我并不是要求使用不可靠的方法,比如检查表上的“select *”是否返回错误(这是一个好主意吗?)

原因如下:

在我的程序中,我需要创建并填充一些表,如果它们还不存在的话。

如果它们已经存在,我需要更新一些表。

我是否应该采取其他路径来表示已经创建了相关的表-例如,通过在磁盘上的程序初始化/设置文件中创建/放置/设置某个标志?

或者我的方法有意义吗?


当前回答

R DBI包中的函数dbExistsTable()为R程序员简化了这个问题。请看下面的例子:

library(DBI)
con <- dbConnect(RSQLite::SQLite(), ":memory:")
# let us check if table iris exists in the database
dbExistsTable(con, "iris")
### returns FALSE

# now let us create the table iris below,
dbCreateTable(con, "iris", iris)
# Again let us check if the table iris exists in the database,
dbExistsTable(con, "iris")
### returns TRUE 

其他回答

R DBI包中的函数dbExistsTable()为R程序员简化了这个问题。请看下面的例子:

library(DBI)
con <- dbConnect(RSQLite::SQLite(), ":memory:")
# let us check if table iris exists in the database
dbExistsTable(con, "iris")
### returns FALSE

# now let us create the table iris below,
dbCreateTable(con, "iris", iris)
# Again let us check if the table iris exists in the database,
dbExistsTable(con, "iris")
### returns TRUE 

使用以下代码:

SELECT name FROM sqlite_master WHERE type='table' AND name='yourTableName';

如果返回的数组count等于1,则表示该表存在。否则它就不存在。

在我看来,使用一个简单的SELECT查询是非常可靠的。最重要的是,它可以检查表是否存在于许多不同的数据库类型(SQLite / MySQL)。

SELECT 1 FROM table;

当您可以使用其他可靠的机制来确定查询是否成功时(例如,您可以通过Qt中的QSqlQuery查询数据库),这是有意义的。

c++函数检查db和所有附加的数据库是否存在表和列(可选)。

bool exists(sqlite3 *db, string tbl, string col="1")
{
    sqlite3_stmt *stmt;
    bool b = sqlite3_prepare_v2(db, ("select "+col+" from "+tbl).c_str(),
    -1, &stmt, 0) == SQLITE_OK;
    sqlite3_finalize(stmt);
    return b;
}

编辑:最近发现了sqlite3_table_column_metadata函数。因此

bool exists(sqlite3* db,const char *tbl,const char *col=0)
{return sqlite3_table_column_metadata(db,0,tbl,col,0,0,0,0,0)==SQLITE_OK;}

我想补充Diego Vélez关于PRAGMA声明的回答。

从https://sqlite.org/pragma.html我们可以得到一些有用的函数,可以返回关于我们数据库的信息。 在此,我引用以下内容:

例如,索引中有关列的信息可以使用index_info pragma读取,如下所示: 编译指示index_info(“idx52”); 或者,可以使用以下方法读取相同的内容: SELECT * FROM pragma_index_info('idx52'); 表值函数格式的优点是查询可以只返回PRAGMA列的一个子集,可以包含WHERE子句,可以使用聚合函数,并且表值函数可以只是连接中的几个数据源之一……

Diego的回答给了PRAGMA table_info(table_name)一个选项,但这在您的其他查询中没有多大用处。

所以,要回答OPs问题并改进diego的回答,你可以做

SELECT * FROM pragma_table_info('table_name');

或者更好,

SELECT name FROM pragma_table_list('table_name');

如果你想模仿poorluzer投票最多的答案。