C++ 中SQLITE3 判断表是否存在

     在网上搜了好多关于 在C++ 中操作sqlite3 如何判断表是否存在,但是要么集中在  "select count(*) from sqlite_master where type='table' and name='ABC;" 下,要么是根据 “SELECT * FROM ABC” 下,根据返回值来判断。 今天经试验,可使用如下方法进行处理,不知是否正确,请指正.

int sqlite3_callback_func( void* pHandle,int iRet, char** szSrc, char** szDst )
{
//...
if ( 1 == iRet )
{
int iTableExist = atoi(*(szSrc));  //此处返回值为查询到同名表的个数,没有则为0,否则大于0

// szDst 指向的内容为"count(*)"
}

       //..
 
return 0; //返回值一定要写,否则下次调用 sqlite3_exec(...) 时会返回 SQLITE_ABORT
}


int  CheckTableExist()
{
char* sErrMsg = NULL;
char sTemp[] = "select count(*) from sqlite_master where type='table' and name='ABC';";


//void *pHandle = ***;
int iRc = sqlite3_exec(s_db, sTemp, &sqlite3_callback_func, p_Handle, &sErrMsg);
//回调函数无返回值,则此处第一次时返回SQLITE_OK, 第n次会返回SQLITE_ABORT


return iRc;
}

猜你喜欢

转载自blog.csdn.net/wildangel817/article/details/44594355