언어 설정

Menu
Sites
Language
How to obtain SQLite generated ID after executing CREATE statement

Hello all,

I have my SQLite db setup in such a way that my ID column is a primary key and it gets generated automatically by the db incrementally after each INSERT.  The code looks something like this

String sqlStatement;
DbStatement* pStmt = null;
DbEnumerator* retVal = 0;
Database* db;
result r = E_SUCCESS;

sqlStatement.Append(L"INSERT INTO Track (Description, Title, Distance, Status) VALUES (?,?,?,?)");

db = BootstrapManager::getInstance()->getDatabase();
pStmt = db->CreateStatementN(sqlStatement);
pStmt->BindString(0, *__pDescription);
pStmt->BindString(1, *__pTitle);
pStmt->BindDouble(2, __distance);
pStmt->BindInt(3, __status);

AppLog("Performing transaction with statement: [%d]", statement);
db->BeginTransaction();
retVal = db->ExecuteStatementN(*statement);
db->CommitTransaction();
 
There is one additional column in the Track table called ID but as it is SQLite primary key it gets autogenerated so I don't do manual INSERT on it.  Everything works fine but I'm executing this during a construction phase of an object and I need to set a private int __trackerId field on the object after the execution of the transaction but there is no apparent way of getting it from the DbEnumerator that results from the execution.  And since I do not know the id I cannot do a SELECT either.  Doing a SELECT using the other values is not possible because they are not guaranteed to be unique (even the combination).  Any suggestion is appreciated.
 
Regards Jirka
Edited by: Brock Boland on 17 3월, 2014 Reason: Paragraph tags added automatically from tizen_format_fix module.

Responses

2 댓글
youngsik yoon
i didn't try. it seems db->GetLastInsertRowId() is what you are looking for.
Jirka Prazak
Thanks, I have actually added SELECT last_insert_rowid(); to the statement to get something back but this seems cleaner. Will try as well. -J