Check SQLite database integrity
Huahuahu? 2021-07-20 04:26:49

  

I have a problem recently , Loss of user data , After getting the user database file , Database corruption found .

database disk image is malformed

So hopefully we can find a way , It can detect whether the database is damaged , after google, Found a way , Write it down first .

+ (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;
}

Original link

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.

The point is that as multiple rows with a single column per row)
It looks like , Can pass c Code to implement
According to the document , You can also use quick_check To 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.

The difference lies in integrity_check Checked
- out-of-order records( Disordered records )
- missing pages( Page missing )
- malformed records( Wrong record )
- missing index entries( Missing index )
- UNIQUE constraint( Uniqueness constraint )
- NOT NULL ( Non empty constraint )
And it takes a lot of time .
quick_check Don't check constraints , Less time consuming


Please bring the original link to reprint ,thank
Similar articles

2021-08-09

2021-08-09