Skip to content

Instantly share code, notes, and snippets.

@zrzka
Created September 29, 2011 17:01
Show Gist options
  • Save zrzka/1251278 to your computer and use it in GitHub Desktop.
Save zrzka/1251278 to your computer and use it in GitHub Desktop.
sqlite3 integrity check
+ (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;
}
@mikeabdullah
Copy link

Note that this call: cStringUsingEncoding:NSUTF8StringEncoding should really be fileSystemRepresentation instead

@wangxuying
Copy link

excute this setence:PRAGMA integrity_check, still reture the result:The database disk image is malformed, how can i do next to juge the error and reserve it?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment