sqlite如何判断表是否存在(C++/VC)

 1 BOOL CDBAccess::IsTableExsit(const char* pTableName)
 2 {
 3 if (NULL == m_pSqlite3 || NULL == pTableName)
 4 {
 5 return TRUE;
 6 }
 7 sqlite3_stmt* pSqliteStmt = NULL;
 8 char* pErrMsg = NULL;
 9 char chSQL[SQL_MAXLENGTH];
10 //这里本来想用绑定的,如select count(*) from sqlite_master where type = 'table' and name = ?,但是总是结果总是不对
11 //不知道为啥这里不能用绑定,只能用sprintf了
12 _snprintf_s(chSQL, SQL_MAXLENGTH, "select count(*) from sqlite_master where type = 'table' and name = '%s'", pTableName);
13 
14 int nRet = sqlite3_prepare_v2(m_pSqlite3, chSQL, -1, &pSqliteStmt, NULL);
15 CHECK_SQLITE_RET(nRet, TRUE);
16 
17 nRet = sqlite3_step(pSqliteStmt);
18 if (SQLITE_OK != nRet && SQLITE_DONE != nRet && SQLITE_ROW != nRet)
19 {
20 return TRUE;
21 }
22 
23 int bIsTableExist = sqlite3_column_int(pSqliteStmt, 0);
24 
25 sqlite3_finalize(pSqliteStmt);
26 
27 return bIsTableExist;
28 }

 

posted on 2013-12-14 23:59  翟小祥  阅读(1908)  评论(0编辑  收藏  举报

导航