/*************************************************************************************************
* 函數名稱: IntegrityCheck
* 功能描述: 數據庫完整性檢測
* 輸入參數: 無
* 輸出參數: 無
* 返 回 值: 0:完整 / 1:損壞
* 其它說明:
* 修改日期 版本號 修改人 修改內容
* -----------------------------------------------
*
**************************************************************************************************/
#define DB_OK 0 /* 完整 */
#define DB_ERROR 1 /* 損壞 */
sqlite3 *obj_db;
char g_objfile[255] = "DB.db3";
int IntegrityCheck(void)
{
/*打開數據庫*/
sqlite3_open( g_objfile, &obj_db );
BOOL integrityVerified = DB_ERROR;
sqlite3_stmt *integrity = NULL;
// integrity_check檢查包括:亂序的記錄、缺頁、錯誤的記錄、丟失的索引、唯一性約束、非空約束
//if ( sqlite3_prepare_v2( obj_db, "PRAGMA integrity_check;", -1, &integrity, NULL ) == SQLITE_OK )
//quick_check不檢查約束條件,耗時較短
if ( sqlite3_prepare_v2( obj_db, "PRAGMA quick_check;", -1, &integrity, NULL ) == SQLITE_OK )
{
while ( sqlite3_step( integrity ) == SQLITE_ROW )
{
const unsigned char *result = sqlite3_column_text( integrity, 0 );
if ( result && strcmp( ( const char * )result, (const char *)"ok" ) == 0 )
{
integrityVerified = DB_OK;
break;
}
}
sqlite3_finalize( integrity );
}
/*關閉數據庫*/
sqlite3_close( obj_db );
return integrityVerified;
}
更多內容請訪問 www.uusystem.com