在iphone上删除数据sqlite - 数据库被锁定

时间:2012-06-10 06:43:45

标签: iphone objective-c database sqlite

当我尝试从我的Sqlite数据库中删除数据时,我的应用程序崩溃了。

这是错误:Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Error while deleting. 'database is locked'

这是我在数据库中的所有代码:

#import "Database.h"

static sqlite3 *database = nil;
static sqlite3_stmt *deleteStmt = nil;
static sqlite3_stmt *addStmt = nil;

@implementation Database

@synthesize id_poi, pathDb, arrPoi;

- (id) initWithData:(NSDictionary *) data {

    [super init];
    NSLog(@"%@",[data objectForKey:@"pathDb"]);
    NSLog(@"id_poi = %@",[data objectForKey:@"id_poi"]);
    //setto l'id nello stato dell'oggetto
    id_poi = [data objectForKey:@"id_poi"];
    pathDb = [data objectForKey:@"pathDb"];

    return self;
}

- (void) deletePoi {

    if (sqlite3_open([pathDb UTF8String], &database) == SQLITE_OK) {
        const char *sql = "delete from Poi where id_poi = ?";
        sqlite3_stmt *deleteStmt;
        if(sqlite3_prepare_v2(database, sql, -1, &deleteStmt, NULL) == SQLITE_OK) {
            //When binding parameters, index starts from 1 and not zero.
            sqlite3_bind_int(deleteStmt, 1, [id_poi integerValue]);

            if (SQLITE_DONE != sqlite3_step(deleteStmt)) {
                NSAssert1(0, @"Error while deleting. '%s'", sqlite3_errmsg(database));
                [[WPActivityIndicator sharedActivityIndicator] hide];
                return;
            }
        } else {
            NSAssert1(0, @"Error while creating delete statement. '%s'", sqlite3_errmsg(database));   
        }
        sqlite3_finalize(deleteStmt);
    } else {
        sqlite3_close(database); //Even though the open call failed, close the database connection to release all the memory.
    }


    [[WPActivityIndicator sharedActivityIndicator] hide];
}

- (BOOL) checkIdPoi {

    // lista temporanea
    NSMutableArray *listaTemp = [[NSMutableArray alloc] init];
    // Oggetto che contiene i vari elementi
    NSMutableDictionary *dictionary;

    NSMutableString *str_id_poi;//id della persona

    if (sqlite3_open([pathDb UTF8String], &database) == SQLITE_OK) {
        // query che ricava i valori
        const char *sql = "select id_poi from Poi where id_poi = ?";
        sqlite3_stmt *selectstmt;

        if(sqlite3_prepare_v2(database, sql, -1, &selectstmt, NULL) == SQLITE_OK) {

            sqlite3_bind_int(selectstmt, 1 , [id_poi integerValue]);
            while(sqlite3_step(selectstmt) == SQLITE_ROW) {
                // ricaviamo i valori letti dalla query
                str_id_poi = [NSMutableString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt, 0)];

                // inseriamo tutti i valori letti in un unico oggetto
                dictionary = [[NSMutableDictionary alloc] initWithObjectsAndKeys:str_id_poi, @"id_poi", nil];
                //NSLog(@"str_id_poi = %@",str_id_poi);
                [listaTemp addObject:dictionary];
                [dictionary release];
                return TRUE;
            }
        } else {
            NSAssert1(0, @"Error while read data. '%s'", sqlite3_errmsg(database));
            return FALSE;
        }
    }
    else {
        sqlite3_close(database);
        return FALSE;
    }

    return FALSE;

}

- (void) addPoi {

    //NSLog(@"%@",pathDb);

    if (sqlite3_open([pathDb UTF8String], &database) == SQLITE_OK) {

        const char *sql = "insert into Poi(id_poi) Values(?)";
        sqlite3_stmt *addStmt;
        if(sqlite3_prepare_v2(database, sql, -1, &addStmt, NULL) == SQLITE_OK) {

            NSString *str_id_poi = [[NSString alloc] initWithFormat:@"%d", [id_poi integerValue]];
            sqlite3_bind_text(addStmt, 1, [str_id_poi UTF8String], -1, SQLITE_TRANSIENT);

            if(SQLITE_DONE != sqlite3_step(addStmt)) {
                NSAssert1(0, @"Error while inserting data. '%s'", sqlite3_errmsg(database));
                [[WPActivityIndicator sharedActivityIndicator] hide];
                return;
            } else {
                //SQLite provides a method to get the last primary key inserted by using sqlite3_last_insert_rowid
                //id_poi = sqlite3_last_insert_rowid(database);
            }

            //Reset the add statement.
            sqlite3_reset(addStmt);


        } else {
            NSAssert1(0, @"Error while creating add statement. '%s'", sqlite3_errmsg(database));            
        }
        sqlite3_finalize(addStmt); 

    } else {
        sqlite3_close(database); //Even though the open call failed, close the database connection to release all the memory.
    }

    sqlite3_close(database);

    [[WPActivityIndicator sharedActivityIndicator] hide];

}

// Carica i valori dal database passato come parametro
-(NSMutableArray*)getIdPOI {

    // lista temporanea
    NSMutableArray *listaTemp = [[NSMutableArray alloc] init];
    // Oggetto che contiene i vari elementi
    NSMutableDictionary *dictionary;

    NSMutableString *str_id_poi;//id della persona

    if (sqlite3_open([pathDb UTF8String], &database) == SQLITE_OK) {
        // query che ricava i valori
        const char *sql = "select id_poi from Poi";
        sqlite3_stmt *selectstmt;

        if(sqlite3_prepare_v2(database, sql, -1, &selectstmt, NULL) == SQLITE_OK) {

            while(sqlite3_step(selectstmt) == SQLITE_ROW) {
                // ricaviamo i valori letti dalla query
                str_id_poi = [NSMutableString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt, 0)];

                // inseriamo tutti i valori letti in un unico oggetto
                dictionary = [[NSMutableDictionary alloc] initWithObjectsAndKeys:str_id_poi, @"id_poi", nil];
                //NSLog(@"str_id_poi = %@",str_id_poi);
                [listaTemp addObject:dictionary];
                [dictionary release];
            }

            //Reset the add statement.
            sqlite3_reset(selectstmt);

        } else {
            NSAssert1(0, @"Error while read data. '%s'", sqlite3_errmsg(database));
        }

        //ADD THIS LINE TO YOUR CODE
        sqlite3_finalize(selectstmt);
    }
    else
        sqlite3_close(database);

    sqlite3_close(database);

    return listaTemp;
}


+ (void) finalizeStatements {
    if(database) sqlite3_close(database);
    if(deleteStmt) sqlite3_finalize(deleteStmt);
    if(addStmt) sqlite3_finalize(addStmt);
}


-(void)dealloc {
    [pathDb release];
    [arrPoi release];

    [super dealloc];
}

@end

有什么建议吗?

2 个答案:

答案 0 :(得分:2)

当为sqlite db触发多个查询时,可能会发生此类错误。执行任何查询时,需要同步对数据库的所有调用。

将所有数据库代码包装在@synchronized块中,如下所示:

@synchronized(self)
{
   // database query code goes here
}

NB。此示例假定所有数据库查询都是从1类内部执行的。如果没有,则应将“self”替换为与数据库连接的所有类共享的公共对象实例。或者,您可以在主线程上运行所有数据库语句。

答案 1 :(得分:1)

这通常在数据库打开然后未正确关闭时发生。这可能是因为您从其他地方打开了相同的数据库实例。(就像一个不同的类或SQLite管理器)。

通常建议创建一个与数据库通信的单例实例,而不是一次又一次地打开和关闭数据库..希望这会有所帮助。