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.cnblogs.com/tianjifa/p/9273712.html