我正在使用C++的SQLite3 C接口,需要能够确定给定表的存在.有没有直接使用SQL的方法?例:
bool exists = sqlite3_table_exists("myTable");
Run Code Online (Sandbox Code Playgroud)
有没有类似功能的功能?
要检查表是否存在,请检查以下查询是否返回任何行:
SELECT name FROM sqlite_master WHERE type='table' AND name='your_table_name_here'
Run Code Online (Sandbox Code Playgroud)
要运行查询并检查行是否存在,以下是代码:
sqlite3_stmt *pSelectStatement = NULL;
int iResult = SQLITE_ERROR;
bool ret = false;
iResult = sqlite3_prepare16_v2(m_pDb, query, -1, &pSelectStatement, 0);
if ((iResult == SQLITE_OK) && (pSelectStatement != NULL))
{
iResult = sqlite3_step(pSelectStatement);
//was found?
if (iResult == SQLITE_ROW) {
ret = true;
sqlite3_clear_bindings(pSelectStatement);
sqlite3_reset(pSelectStatement);
}
iResult = sqlite3_finalize(pSelectStatement);
}
Run Code Online (Sandbox Code Playgroud)