SQLite3 C 接口:如何确定表的存在 [英] SQLite3 C interface: how to determine existence of a table

查看:26
本文介绍了SQLite3 C 接口:如何确定表的存在的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用来自 C++ 的 SQLite3 C 接口,并且需要能够确定给定表的存在.有没有直接使用 SQL 的方法?示例:

I'm using SQLite3 C interface from C++ and need to be able to determine existence of a given table. Is there a means for this without using SQL directly? Example:

bool exists = sqlite3_table_exists("myTable");

有没有类似功能的函数?

Is there any function with similar functionality?

推荐答案

要检查表是否存在,请检查以下查询是否返回任何行:

To check if table exists, check if any rows are returned by the following query:

SELECT name FROM sqlite_master WHERE type='table' AND name='your_table_name_here'

运行查询 &检查行是否存在,这是代码:

To run the query & check if rows exist, here is the code:

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

这篇关于SQLite3 C 接口:如何确定表的存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆