获取sqlite3_last_insert_rowid()目标c后,sqlite DB显示SQLITE_BUSY

时间:2015-07-31 10:17:23

标签: ios objective-c sqlite

我正在尝试在sqlite DB成员表中插入一个成员。插入值后,如果我采用sqlite3_last_insert_rowid(),我无法插入另一个成员。语句显示SQLITE_BUSY.Here是我的代码。请任何人帮忙。

-(NSInteger) saveMember:(TMMember *)member {
const char *dbPath = [databasePath UTF8String];
if (sqlite3_open(dbPath, &database) == SQLITE_OK)
{
    NSString *insertSQL = [NSString stringWithFormat:@"insert into members (memberName, memberAmount,shareFlag) values(\"%@\", \"%f\",%d)",member.memberName,member.amount,[[NSNumber numberWithBool:member.shareFlag]intValue]];
    const char *insert_stmt = [insertSQL UTF8String];
    sqlite3_prepare_v2(database, insert_stmt,-1, &statement, NULL);
    if(sqlite3_step(statement) == SQLITE_DONE)
    {
        NSInteger lastRowId = sqlite3_last_insert_rowid(database);
        member.memberId = lastRowId;
        NSLog(@"inserted member id = %ld",lastRowId);
        NSLog(@"member is added");
    }
    sqlite3_finalize(statement);
    statement = nil;
}
sqlite3_reset(statement);
sqlite3_close(database);
return 0;

}

1 个答案:

答案 0 :(得分:1)

当sqlite已经在处理另一条语句,而您正在尝试执行另一条语句时,就会出现此错误。因此,数据库将被锁定,直到您完成该语句为止。

有关更多信息。阅读:SQLite Exception: SQLite Busy