sqlite3_finalize后返回插入的Id

发布于 2024-11-04 08:48:41 字数 1315 浏览 1 评论 0原文

这是我用来将记录插入表中的代码...我所需要的只是这个函数来捕获并返回记录的插入 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;}

Here is the code I am using to insert a record into the table... all I need is this function to capture and return back the inserted id of the record... is this possible? This code is working fine and returns YES for successful insert and NO for failures... but now im trying to return an integer insert id... but cant seem to figure out a way...

-(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;}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夏夜暖风 2024-11-11 08:48:41

尝试这个函数sqlite3_last_insert_rowid(database);

try this function sqlite3_last_insert_rowid(database);

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文