flex本地数据库lastInsertId = 0

时间:2012-07-07 20:48:09

标签: database actionscript-3 sqlite air last-insert-id

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为零?!我搞砸了什么东西!?

1 个答案:

答案 0 :(得分:3)

您无需额外执行查询以检索最后插入的行的id。为此,有.lastInsertRowID类的内置SQLResult属性。只需使用它:

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);