2012-07-07 44 views
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零?我把事情搞亂了!?柔性本地數據庫lastInsertId = 0

+0

通過替換解決 變種lastInsertId:INT = insertReq.getResult()的數據[0] .ID; 與 var lastInsertId:int = parseInt(insertReq.getResult()。data [0] .id); – vicx 2012-07-07 20:57:29

回答

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