Created
September 29, 2011 17:01
-
-
Save zrzka/1251278 to your computer and use it in GitHub Desktop.
sqlite3 integrity check
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
+ (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; | |
} |
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
Note that this call:
cStringUsingEncoding:NSUTF8StringEncoding
should really befileSystemRepresentation
instead