Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7549101
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T09:51:59+00:00 2026-05-30T09:51:59+00:00

I’ve an sqlite3 database for an iOs application, in order to provide an offline

  • 0

I’ve an sqlite3 database for an iOs application, in order to provide an offline support to the system.

It works perfectly. But sometimes the .db file becomes corrupted. And doesn’t return results.

If I check the SELECT instruction throught the command line I get the next Error message:

sqllite Error: database disk image is malformed

Although is not desirable it becomes corrupt. The database is just an auxiliary system, would be enough to being able to detect from iOS application that the file is corrupted and restart the file.

But using the sqlite3 statements I didn’t get any exception. The code looks like:

sqlRaw = @"SELECT ... ";
const char *sql = [sqlRaw cStringUsingEncoding:NSUTF8StringEncoding];
if (sqlite3_prepare_v2(database, sql, -1, &date_statement, NULL) != SQLITE_OK) {
    NSAssert1(0, @"Error: failed to prepare statement with message '%s'.", sqlite3_errmsg(database));
}

NSMutableArray *entities = [[NSMutableArray alloc] initWithCapacity:0];
while (sqlite3_step(date_statement) == SQLITE_ROW) {
    NSData *entityXml = [[NSData alloc] initWithBytes:sqlite3_column_blob(date_statement, 0) 
                                                   length:sqlite3_column_bytes(date_statement, 0)];
    [entities addObject:entityXml];
}
sqlite3_finalize(date_statement);

When the application executes the previous code, simply returns an empty array, no exception is thrown.

Anybody knows how to check from the sqlite3 statements the .db file status?

Maybe is better to user other storage system. Any recommendation?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T09:52:00+00:00Added an answer on May 30, 2026 at 9:52 am

    Finally I decided to check from the application the database status, and if the database has become corrupted, the just replace for a new empty database.

    - (BOOL) isDatabaseCorrupted {
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *documentsDirectory = [paths objectAtIndex:0];
        NSString *path = [documentsDirectory stringByAppendingPathComponent:cache_final_path];
        BOOL bResult = FALSE;
        if (sqlite3_open([path UTF8String], &database) == SQLITE_OK) {  
            @try {
                NSString *sqlRaw = @"PRAGMA integrity_check;";
                const char *sql = [sqlRaw cStringUsingEncoding:NSUTF8StringEncoding];
                if (sqlite3_prepare_v2(database, sql, -1, &check_statement, NULL) == SQLITE_OK) {
                    int success = sqlite3_step(check_statement);
                    NSLog(@"SQL integrity_check result is %d", success);
                    NSString *response = nil;
                    switch (success) {
                        case SQLITE_ERROR:
                            bResult = TRUE;
                            break;
                        case SQLITE_DONE:
                            NSLog(@"Result is simple DONE of the sqllite3 on isDatabaseCorrupted");
                            break;
                        case SQLITE_BUSY:
                            NSLog(@"Result is simple BUSY of the sqllite3 on isDatabaseCorrupted");
                            break;
                        case SQLITE_MISUSE:
                            NSLog(@"Bad utilization of the sqllite3 on isDatabaseCorrupted");
                            break;
                        case SQLITE_ROW:
                            response = [NSString stringWithUTF8String:(char *)sqlite3_column_text(check_statement, 0)];
                            if ([[[response lowercaseString] stringByTrimmingCharactersInSet:[NSCharacterSet 
                                                                                              whitespaceAndNewlineCharacterSet]] 
                                 isEqualToString:@"ok"]){
                                bResult = FALSE;
                            } else {
                                NSLog(@"ATTENTION: integrity_check response %@", response);
                                bResult = TRUE;
                            }
                            break;
                        default:
                            break;
                    }
    
                    sqlite3_finalize(check_statement);
                }
    
            }
            @catch (NSException *exception) {
                [TSLogger log:[NSString stringWithFormat:@"Exception %@", [exception description]] 
                 withSeverity:severity_error];
                return TRUE;
            }        
        }
    
        sqlite3_close(database);
        return bResult;
    }
    

    The results was defined in order of the expected response on the sqlite3 documentation.

    Thank you,

    Ivan

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a reasonable size flat file database of text documents mostly saved in
I'm working with an upstream system that sometimes sends me text destined for HTML/XML
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
Seemingly simple, but I cannot find anything relevant on the web. What is the
I have a French site that I want to parse, but am running into
I want use html5's new tag to play a wav file (currently only supported
In my XML file chapters tag has more chapter tag.i need to display chapters

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.