Check sqlite database integrity and sqlite database integrity

Source: Internet
Author: User

Check sqlite database integrity and sqlite database integrity

Recently, a problem occurs: user data is lost. After obtaining the user database file, the database is damaged.

database disk image is malformed

Therefore, we hope to find a method to check whether the database is damaged. After google, we found a method to record it 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.

Focus on this sentenceAs multiple rows with a single column per row)
In this way, the c code can be used to implement
You can also use quick_check to check according to the document.

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 is that integrity_check checks
-Out-of-order records (unordered records)
-Missing pages (missing pages)
-Malformed records (error record)
-Missing index entries (missing index)
-UNIQUE constraint (uniqueness constraint)
-Not null (non-empty constraint)
It takes a lot of time.
Quick_check does not check constraints and takes a short time

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.