sqlite3_finalize后返回插入的Id
这是我用来将记录插入表中的代码...我所需要的只是这个函数来捕获并返回记录的插入 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试这个函数
sqlite3_last_insert_rowid(database);
try this function
sqlite3_last_insert_rowid(database);