Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 20th, 2012  |  syntax: None  |  size: 0.98 KB  |  hits: 13  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. + (BOOL)checkIntegrity {
  2.   NSString *databasePath = [self databaseFilePath];
  3.  
  4.   // File not exists = okay
  5.   if ( ! [[NSFileManager defaultManager] fileExistsAtPath:databasePath] ) {
  6.     return YES;
  7.   }
  8.  
  9.   const char *filename = ( const char * )[databasePath cStringUsingEncoding:NSUTF8StringEncoding];
  10.   sqlite3 *database = NULL;
  11.  
  12.   if ( sqlite3_open( filename, &database ) != SQLITE_OK ) {
  13.     sqlite3_close( database );
  14.     return NO;
  15.   }
  16.  
  17.   BOOL integrityVerified = NO;
  18.   sqlite3_stmt *integrity = NULL;
  19.  
  20.   if ( sqlite3_prepare_v2( database, "PRAGMA integrity_check;", -1, &integrity, NULL ) == SQLITE_OK ) {
  21.    
  22.     while ( sqlite3_step( integrity ) == SQLITE_ROW ) {
  23.       const unsigned char *result = sqlite3_column_text( integrity, 0 );
  24.       if ( result && strcmp( ( const char * )result, (const char *)"ok" ) == 0 ) {
  25.         integrityVerified = YES;
  26.         break;
  27.       }
  28.     }
  29.    
  30.     sqlite3_finalize( integrity );
  31.   }
  32.  
  33.   sqlite3_close( database );
  34.  
  35.   return integrityVerified;
  36. }