Sqlite数据库锁定错误-iOS

Sqlite数据库锁定错误-iOS,ios,objective-c,sqlite,Ios,Objective C,Sqlite,我使用sqlite来插入和更新表视图中的数据。我只能更新数据一次,下次尝试时显示数据库已锁定。即使我正在关闭数据库,请帮助。下面是代码 -(void)saveUserCredential: (NSString *)email :(NSString *)userName :(NSString *)loginTime :(NSString *)source { NSDateFormatter *dateformate=[[NSDateFormatter alloc]init]; [dateforma

我使用sqlite来插入和更新表视图中的数据。我只能更新数据一次,下次尝试时显示数据库已锁定。即使我正在关闭数据库,请帮助。下面是代码

-(void)saveUserCredential: (NSString *)email :(NSString *)userName :(NSString *)loginTime :(NSString *)source
{
NSDateFormatter *dateformate=[[NSDateFormatter alloc]init];
[dateformate setDateFormat:@"yyyy-MM-dd HH:mm"]; // Date formater
NSString *todayDate = [dateformate stringFromDate:[NSDate date]];

const char *dbpath = [databasePath UTF8String];
if (sqlite3_open(dbpath, &database) == SQLITE_OK)
{
    NSString  * query = @"SELECT * from users";
    int rc =sqlite3_prepare_v2(database, [query UTF8String], -1, &statement, NULL);
    if(rc == SQLITE_OK)
    {
        if(sqlite3_step(statement) == SQLITE_ROW)
        {

              NSString *updateSQL = [NSString stringWithFormat:@"update users set email = '%@', username = '%@', source = '%@', created = '%@' where id = '%d'",email, userName, source, todayDate,1];

            const char *update_stmt = [updateSQL UTF8String];
            sqlite3_prepare_v2(database, update_stmt, -1, &statement, NULL );
            //sqlite3_bind_int(statement, 1, 1);
            if (sqlite3_step(statement) == SQLITE_DONE)
            {
                NSLog(@"successfully updated");
            }
            else{

                NSLog(@"Error while updating. '%s'", sqlite3_errmsg(database));
            }

        }
        else
        {
            NSString *insertSQL = [NSString stringWithFormat:@"insert into users (email,username,source,created) values('%@','%@','%@','%@')",email,userName,source,todayDate];

            NSLog(@"INS SQL: %@", insertSQL);
            const char *insert_stmt = [insertSQL UTF8String];
            sqlite3_prepare_v2(database, insert_stmt,-1, &statement, NULL);
            if (sqlite3_step(statement) == SQLITE_DONE)
            {
                NSLog(@"INSERTED");
            }
            else
            {
                NSLog(@"NOT INSERTED");
            }
            NSLog(@"hello ");

        }
        sqlite3_finalize(statement);
        sqlite3_close(database);

    }

}

}
你需要打电话

sqlite3_finalize(statement);
对于每个成功的
sqlite\u prepare\u v2
调用。它应该是平衡的。对每个查询也使用不同的
sqlite3\u stmt

因此,您的代码需要更改如下:

const char *dbpath = [databasePath UTF8String];
if (sqlite3_open(dbpath, &database) == SQLITE_OK)
{
    NSString  * query = @"SELECT * from users";
    int rc =sqlite3_prepare_v2(database, [query UTF8String], -1, &statement, NULL);
    if(rc == SQLITE_OK)
    {
        if(sqlite3_step(statement) == SQLITE_ROW)
        {

              NSString *updateSQL = [NSString stringWithFormat:@"update users set email = '%@', username = '%@', source = '%@', created = '%@' where id = '%d'",email, userName, source, todayDate,1];

            const char *update_stmt = [updateSQL UTF8String];
            sqlite3_stmt *upStmt;
            sqlite3_prepare_v2(database, update_stmt, -1, &upStmt, NULL );
            //sqlite3_bind_int(upStmt, 1, 1);
            if (sqlite3_step(upStmt) == SQLITE_DONE)
            {
                NSLog(@"successfully updated");
            }
            else
            {

                NSLog(@"Error while updating. '%s'", sqlite3_errmsg(database));
            }
            sqlite3_finalize(upStmt);
        }
        else
        {
            NSString *insertSQL = [NSString stringWithFormat:@"insert into users (email,username,source,created) values('%@','%@','%@','%@')",email,userName,source,todayDate];

            NSLog(@"INS SQL: %@", insertSQL);
            const char *insert_stmt = [insertSQL UTF8String];
            sqlite3_stmt *inStmt;
            sqlite3_prepare_v2(database, insert_stmt,-1, &inStmt, NULL);
            if (sqlite3_step(inStmt) == SQLITE_DONE)
            {
                NSLog(@"INSERTED");
            }
            else
            {
                NSLog(@"NOT INSERTED");
            }
            NSLog(@"hello ");
           sqlite3_finalize(inStmt);

        }
        sqlite3_finalize(statement);
        sqlite3_close(database);

    }

}

因此,如果(sqlite3_step(statement)==SQLITE_DONE){NSLog(@“成功更新”);}或者{NSLog(@“更新时出错”。%s',sqlite3_errmsg(数据库));},我需要在这一行后面添加finalize语句它现在没有更新表。@Mac\u Play:我在代码中有一个输入错误,现在已经修复了,请现在检查您是否已经分别添加了2个sqlite3\u stmt来处理插入和更新,对吗?或者你做了其他的改变?