[Tips]Flex中取得SQLite的最后插入记录的ID SELECT last_insert_rowid()

在Insert之后,使用SELECT last_insert_rowid()可直接得到最后一次插入的记录的id

如果之前没有进行任何Insert的操作,则返回0;

使用方法:

//插入成功后的响应, 同时运行SQL语句, 以获得其ID;
private function onInsertCatSuccess(e:SQLEvent):void {
    //该语句用来执行SELECT last_insert_rowid()
     //AppContext.getInstance().dbConn为数据库连接,Statements.getLastInserRowID为SELECT last_insert_rowid(),null为sql语句参数,后面两个函数分别为成功与失败的响应
    SQLUtils.createAndExecuteStatement(AppContext.getInstance().dbConn, Statements.getLastInserRowID, null, onGetCatIDSuccess, onGetCatIDFail);
} 

下面是成功拿到id后的响应函数:

		//获得ID后, 新建CAt对象,加入到上级目录...
		private function onGetCatIDSuccess(e:SQLEvent):void {
			var result:SQLResult = e.target.getResult();
			catToBeAdded.id = result.data[0]["last_insert_rowid()"];
			LogUtils.catLog.info("获得ID为: " + catToBeAdded.id);
		}

其中result.data[0]["last_insert_rowid()"]便是lastinse_rowid;

This entry was posted in Flex and tagged , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>