Sqlite數據庫完整性檢測


/*************************************************************************************************
* 函數名稱:  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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM