ICode9

精准搜索请尝试: 精确搜索
首页 > 数据库> 文章详细

检查sqlite数据库完整性

2021-05-30 15:54:37  阅读:201  来源: 互联网

标签:sqlite const database 数据库 完整性 sqlite3 NULL integrity check


  

最近遇到一个问题,用户数据丢失,拿到用户数据库文件以后,发现数据库损坏。

database disk image is malformed

因此希望可以找到一种方法,可以检测出来数据库是否损坏,经过google,找到了一种方法,先记录下来。

+ (BOOL)checkIntegrity {
  NSString *databasePath = [self databaseFilePath];
  
  // File not exists = okay
  if ( ! [[NSFileManager defaultManager] fileExistsAtPath:databasePath] ) {
    return YES;
  }
  
  const char *filename = ( const char * )[databasePath cStringUsingEncoding:NSUTF8StringEncoding];
  sqlite3 *database = NULL;
  
  if ( sqlite3_open( filename, &database ) != SQLITE_OK ) {
    sqlite3_close( database );
    return NO;
  }

  BOOL integrityVerified = NO;
  sqlite3_stmt *integrity = NULL;
  
  if ( sqlite3_prepare_v2( database, "PRAGMA integrity_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 = YES;
        break;
      }
    }
    
    sqlite3_finalize( integrity );
  }
  
  sqlite3_close( database );
  
  return integrityVerified;
}

原链接

PRAGMA schema.integrity_check;
PRAGMA schema.integrity_check(N)

This pragma does an integrity check of the entire database. The integrity_check pragma looks for out-of-order records, missing pages, malformed records, missing index entries, and UNIQUE and NOT NULL constraint errors. If the integrity_check pragma finds problems, strings are returned (as multiple rows with a single column per row) which describe the problems. Pragma integrity_check will return at most N errors before the analysis quits, with N defaulting to 100. If pragma integrity_check finds no errors, a single row with the value 'ok' is returned.

重点在这句话as multiple rows with a single column per row)
这样子,可以通过c代码来实现
根据文档,也可以使用quick_check来检查。

PRAGMA schema.quick_check;
PRAGMA schema.quick_check(N)

The pragma is like integrity_check except that it does not verify UNIQUE and NOT NULL constraints and does not verify that index content matches table content. By skipping UNIQUE and NOT NULL and index consistency checks, quick_check is able to run much faster than integrity_check. Otherwise the two pragmas are the same.

区别在于integrity_check检查了
- out-of-order records(乱序的记录)
- missing pages(缺页)
- malformed records(错误的记录)
- missing index entries(丢失的索引)
- UNIQUE constraint(唯一性约束)
- NOT NULL (非空约束)
而且耗时较多。
quick_check不检查约束条件,耗时较短


标签:sqlite,const,database,数据库,完整性,sqlite3,NULL,integrity,check
来源: https://blog.51cto.com/u_15223551/2833593

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有