如何从SQLite3中新创建的记录中获取主键?

时间:2011-11-19 01:17:01

标签: ios cocoa-touch sqlite

我正在插入一条代码类似于下面的代码。插入后,如何从新创建的记录中获取主键字段值?

  NSString *sql1 = [NSString stringWithFormat:@"INSERT INTO mySets (myimage) VALUES ('4')"];
    const char *sqlstring = [sql1 UTF8String];
        if (sqlite3_prepare_v2(appDelegate.myDB, sqlstring, -1, &update_statement, NULL) == SQLITE_OK) {
            int success = sqlite3_step(update_statement);
            if (success != SQLITE_ERROR) {
                int createdSet = sqlite3_column_int(update_statement,0 );
            }
        }

1 个答案:

答案 0 :(得分:1)

sqlite3_int64 sqlite3_last_insert_rowid(sqlite3*);

http://www.sqlite.org/c3ref/last_insert_rowid.html

您可能还想检查sqlite3_errormsg:

if (sqlite3_step(statement) != SQLITE_DONE)
{
    NSLog(@"error: %@", sqlite3_errmsg(_db));
}