1
var insertReq:SQLStatement = new SQLStatement(); 
insertReq.addEventListener(SQLErrorEvent.ERROR, dbErrorHandler);
insertReq.sqlConnection = conn; 
insertReq.text = "INSERT INTO table1 (inputTime) VALUES (DATETIME('now'))";
insertReq.execute();
insertReq.text = "SELECT last_insert_rowid() as ID";
insertReq.execute();
var lastInsertId:int = insertReq.getResult().data[0].id;
trace(lastInsertId);

我创建了一个本地数据库,我正在尝试插入一些数据,但我得到了lastInsertId零?!我在哪里搞砸了!?

4

1 回答 1

3

您不需要额外执行查询来检索最后插入的行的 id 。为此目的, SQLResult类有一个内置的.lastInsertRowID属性。像这样使用它:

var insertReq:SQLStatement = new SQLStatement(); 
insertReq.addEventListener(SQLErrorEvent.ERROR, dbErrorHandler);
insertReq.sqlConnection = conn; 
insertReq.text = "INSERT INTO table1 (inputTime) VALUES (DATETIME('now'))";
insertReq.execute();
var lastInsertId:int = insertReq.getResult().lastInsertRowID;
trace(lastInsertId);
于 2012-07-07T21:04:05.467 回答