在sqlite3_finalize之后返回插入的Id

时间:2011-04-30 05:49:00

标签: iphone ios4 sqlite

以下是我用于将记录插入表中的代码...我需要的是此函数来捕获并返回记录的插入ID ...这可能吗?这段代码工作正常,成功插入返回YES,失败返回NO ...但现在我试图返回一个整数插入ID ......但似乎无法找到方法......

-(BOOL)insertEvent:(Event *)objEvent { Utils *objUtils = [[Utils alloc] init];

sqlite3_stmt *insertStmt = nil;
sqlite3 *aDatabase = nil;

int ret = sqlite3_enable_shared_cache(1);

if (sqlite3_open([databasePath UTF8String], &aDatabase) == SQLITE_OK)
{
    if(insertStmt == nil)
    {           
        NSString *aString = [NSString stringWithFormat:@"INSERT INTO tblEvent ('eventstartdate','eventdescription') VALUES ('%@','%@')",objEvent.eventstartdate, objEvent.eventdescription];
        const char *sql = [aString UTF8String];
        if(sqlite3_prepare_v2(aDatabase, sql, -1, &insertStmt, NULL) != SQLITE_OK)
        {
            NSAssert1(0, @"Error while creating insertTip add statement. '%s'", sqlite3_errmsg(aDatabase));
            return NO;
        }
        [objUtils release];
    }

    if(SQLITE_DONE != sqlite3_step(insertStmt)) {
        NSAssert1(0, @"Error while inserting into TipsTracker. '%s'", sqlite3_errmsg(aDatabase));
        return NO;
    }
    sqlite3_reset(insertStmt);

    if (insertStmt)
    {
        sqlite3_finalize(insertStmt);
        insertStmt = nil;
    }
}
sqlite3_close(aDatabase);
aDatabase = nil;
return YES;}

1 个答案:

答案 0 :(得分:0)

尝试此功能sqlite3_last_insert_rowid(database);