Sqlite3:“数据库被锁定”错误

时间:2011-01-03 06:49:55

标签: cocoa sqlite

在我的cocoa应用程序中,我在资源文件夹中维护一个SQLite数据库,并尝试在其中执行一些select,delete操作,但一段时间后它开始给我“数据库已锁定”错误。

我用于选择删除操作的方法如下:

//检索数据的方法

if (sqlite3_open([databasePath UTF8String], &database) != SQLITE_OK) 
        {
            sqlite3_close(database);
            NSAssert(0, @"Failed to open database");
        }

        NSLog(@"mailBodyFor:%d andFlag:%d andFlag:%@",UId,x,Ffolder);
        NSMutableArray *recordsToReturn = [[NSMutableArray alloc] initWithCapacity:2];
        NSString *tempMsg;


        const char *sqlStatementNew;
        NSLog(@"before switch");
        switch (x) {
            case 9:
            //  tempMsg=[NSString stringWithFormat:@"SELECT * FROM users_messages"];
                tempMsg=[NSString stringWithFormat:@"SELECT message,AttachFileOriName as oriFileName,AttachmentFileName as fileName FROM users_messages WHERE id = (select message_id from users_messages_status where id= '%d')",UId];
                NSLog(@"mail body query - %@",tempMsg);
                break;
            default:
                break;
        }
        sqlStatementNew = [tempMsg cStringUsingEncoding:NSUTF8StringEncoding];
        sqlite3_stmt *compiledStatementNew;

        NSLog(@"before if statement");
        if(sqlite3_prepare_v2(database, sqlStatementNew, -1, &compiledStatementNew, NULL) == SQLITE_OK) {
            NSLog(@"the sql is finalized");
            while(sqlite3_step(compiledStatementNew) == SQLITE_ROW) {
                NSMutableDictionary *recordDict = [[NSMutableDictionary alloc] initWithCapacity:3];

                NSString *message;
                if((char *)sqlite3_column_text(compiledStatementNew, 0)){
                    message = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatementNew, 0)];
                }
                else{
                    message = @"";
                }
                NSLog(@"message - %@",message);

                NSString *oriFileName;
                if((char *)sqlite3_column_text(compiledStatementNew, 1)){
                    oriFileName = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatementNew, 1)];
                }
                else{
                    oriFileName = @"";
                }
                NSLog(@"oriFileName - %@",oriFileName);

                NSString *fileName;
                if((char *)sqlite3_column_text(compiledStatementNew, 2)){
                    fileName = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatementNew, 2)];
                }
                else{
                    fileName = @"";
                }
                NSLog(@"fileName - %@",fileName);

                [recordDict setObject:message forKey:@"message"];
                [recordDict setObject:oriFileName forKey:@"oriFileName"];
                [recordDict setObject:fileName forKey:@"fileName"];

                [recordsToReturn addObject:recordDict];
                [recordDict release];
            }

            sqlite3_finalize(compiledStatementNew);
            sqlite3_close(database);
            NSLog(@"user messages return -%@",recordsToReturn);

            return recordsToReturn;
        }
        else{
            NSLog(@"Error while creating retrieving mailBodyFor in messaging '%s'", sqlite3_errmsg(database));
            sqlite3_close(database);

        }

//删除数据的方法

if (sqlite3_open([databasePath UTF8String], &database) != SQLITE_OK) 
    {
        sqlite3_close(database);
        NSAssert(0, @"Failed to open database");
    }

    NSString *deleteQuery = [[NSString alloc] initWithFormat:@"delete from users_messages_status where id IN(%@)",ids];
    NSLog(@"users_messages_status msg deleteQuery - %@",deleteQuery);

    sqlite3_stmt *deleteStmnt;
    const char *sql = [deleteQuery cStringUsingEncoding:NSUTF8StringEncoding];

    if(sqlite3_prepare_v2(database, sql, -1, &deleteStmnt, NULL) != SQLITE_OK){
        NSLog(@"Error while creating delete statement. '%s'", sqlite3_errmsg(database));
    }   
    else{
        NSLog(@"successful deletion from users_messages");
    }

    if(SQLITE_DONE != sqlite3_step(deleteStmnt)){
        NSLog(@"Error while deleting. '%s'", sqlite3_errmsg(database));
    }

    sqlite3_close(database);

这个序列出现了问题

  1. 检索数据
  2. '数据库已锁定'执行删除操作时出错。
  3. 当我重试执行第一步时......它现在会出现同样的错误。
  4. 有人可以建议我:

    1. 如果我做错了什么或错过了一些检查?
    2. 当它发出锁定错误时,有没有办法解锁?
    3. 谢谢,

      Miraaj

1 个答案:

答案 0 :(得分:1)

每次访问时都打开/关闭数据库非常罕见。

如果是您的预期方案,那么使用普通文件会好得多。

启动时打开数据库,退出时关闭。这可以解决你所有的问题。