Se pierden datos de SQLite iphone al reiniciar o cerrar la aplicación

I have a question... I have an application using sqlite to save some data.Everything works perfectly, meaning I can add, delete, view data. The data are persistent when the application goes into background. Now, when I remove the application from memory or reboot the iPhone, the database is corrupted and all the data are messed up !

I have a class call dbAccess where all the database actions are define(add, delete, retreive rows). At the end I have a finalize action, that finalize all the statement used and then close the database.

+ (void)finalizeStatements{

NSLog(@"Finalizing the Delete Statements");
if(deleteStmt) {
    NSLog(@"Delete Statement exist... finalization");
    deleteStmt = nil;
NSLog(@"Finalizing the Add Statements");
if(addStmt) {
    NSLog(@"Add Statement exist... finalization");
    addStmt = nil;
NSLog(@"Finalizing the Store Statements");
if(storeStmt) {
    NSLog(@"Store Statement exist... finalization");
    storeStmt = nil;
NSLog(@"Finalizing the Agent Statements");
if(agentStmt) {
    NSLog(@"Agent Statement exist... finalization");
    agentStmt = nil;

NSLog(@"Closing the Database");
if(database) {
    NSLog(@"The database exist... closing it");


This method is called by the application delegate when applicationDidEnterBackground and applicationWillTerminate. An openDatabase method is call when applicationDidBecomeActive.

Any ideas why the database is corrupted ?


preguntado el 28 de agosto de 11 a las 01:08

shadowing, you should accept answers if you feel they have answered your question. You accept by clicking on the tick mark next to the answer you like. -

1 Respuestas

First off, check out fmdb or some other proven wrappers. You can also browse the code.

Not sure if there's enough info to know why it's corrupt. But, you should get return codes from TODOS sqlite3_xxx calls and log at a minimum to understand what's going on or you may just be blasting past an issue.

Also, make sure to call sqlite_errmsg which will offer more clues if the return code is not success.

In my wrapper, I do this in close. It's expected that statements are finalized bu handled if not. I have a statement cache which on clear finalizes each statement. :

- (void)close
    if (_sqlite3)
        [self clearStatementCache];

        int rc = sqlite3_close(_sqlite3);
        NSLog(@"close rc=%d", rc);

        if (rc == SQLITE_BUSY) 
            NSLog(@"SQLITE_BUSY: not all statements cleanly finalized");

            sqlite3_stmt *stmt; 
            while ((stmt = sqlite3_next_stmt(_sqlite3, 0x00)) != 0) 
                NSLog(@"finalizing stmt");

            rc = sqlite3_close(_sqlite3);

        if (rc != SQLITE_OK)
            NSLog(@"close not OK.  rc=%d", rc);

        _sqlite3 = NULL;

Respondido 28 ago 11, 05:08

Thanks Bryanmac, I'll try that. - sombreado

one other thing I thought of - are you handling all the appdelegate app shutting call backs to close the db etc...? - Bryanmac

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.