我正在使用以下代码将一行插入数据库。由于某种原因,sqlite3_last_insert_rowid 返回插入的第一行的正确行 ID,但之后它总是返回 0。
在插入之间可能发生了其他数据库操作,例如删除一行。
_scoresDB 是一个实例变量。
这是我的插入代码:
-(void)insertScore:(OKScore*)score
{
const char *dbpath = [[self dbPath] UTF8String];
if(sqlite3_open(dbpath, &_scoresDB) == SQLITE_OK) {
// Setup the SQL Statement
if(insertScoreStatement == nil) {
//OKLog(@"Preparing statement for cache score");
const char *insertSQL = "INSERT INTO OKCACHE(leaderboardID,scoreValue,metadata,displayString,submitted) VALUES(?,?,?,?,?);";
if(sqlite3_prepare_v2(_scoresDB, insertSQL, -1, &insertScoreStatement, NULL) != SQLITE_OK) {
OKLog(@"Failed to prepare score insert statement with message: '%s'", sqlite3_errmsg(_scoresDB));
return;
}
}
// Bind the score values to the statement
sqlite3_bind_int(insertScoreStatement, 1, [score OKLeaderboardID]);
sqlite3_bind_int64(insertScoreStatement, 2, [score scoreValue]);
sqlite3_bind_int(insertScoreStatement, 3, [score metadata]);
if([score displayString]) {
sqlite3_bind_text(insertScoreStatement, 4, [[score displayString] UTF8String], -1, SQLITE_TRANSIENT);
} else {
sqlite3_bind_null(insertScoreStatement, 4);
}
sqlite3_bind_int(insertScoreStatement, 5, (int)[score submitted]);
//Execute the SQL statement
if(sqlite3_step(insertScoreStatement) == SQLITE_DONE) {
int scoreID = sqlite3_last_insert_rowid(_scoresDB);
[score setOKScoreID:scoreID];
OKLog(@"Cached score : %@",score);
} else {
OKLog(@"Failed to store score in cache wihth error message: %s",sqlite3_errmsg(_scoresDB));
}
sqlite3_reset(insertScoreStatement);
sqlite3_clear_bindings(insertScoreStatement);
sqlite3_close(_scoresDB);
} else {
OKLog(@"Could not open cache DB insertScore");
}
}