demo: http://www.sandy1219.com/air/localDatabase.rarpackage { import flash.data.sqlConnection; import flash.data.sqlMode; import flash.data.sqlResult; import flash.data.sqlStatement; import flash.errors.sqlError; import flash.events.sqlErrorEvent; import flash.events.sqlEvent; import flash.filesystem.File; import mx.collections.ArrayCollection; import mx.controls.Alert; public class sqlManager { private static var _instance:sqlManager; public static function getIntance():sqlManager { if(_instance == null) { _instance = new sqlManager(); } return _instance; } public static var dataList:ArrayCollection = new ArrayCollection();; private var conn:sqlConnection; public function connect():void { conn = new sqlConnection(); conn.addEventListener(sqlEvent.OPEN,openHandle); conn.addEventListener(sqlErrorEvent.ERROR,errorHandle); var db:File = File.applicationDirectory.resolvePath('flex.db');trace(db.url) conn.open(db,sqlMode.UPDATE,'false'); } private function openHandle(e:sqlEvent):void { trace("the database was opened successfully"); } private function errorHandle(event:sqlErrorEvent):void { trace("Details:",event.error.message); } public function insertData():void { try { var stmt:sqlStatement = new sqlStatement(); stmt.addEventListener(sqlEvent.RESULT,insertResult); stmt.addEventListener(sqlErrorEvent.ERROR,errorResult); stmt.sqlConnection = conn; stmt.text = "INSERT INTO flex ('name') VALUES ('"+ String(Math.random()*10000).slice(0,8) +"')"; stmt.execute(); } catch(error:sqlError) { trace(error.toString()) Alert.show(error.details); } } private function insertResult(e:sqlEvent):void { trace(e.toString()) var stmt:sqlStatement = sqlStatement(e.currentTarget); var result:sqlResult = stmt.getResult(); var primaryKey:Number = result.lastInsertRowID; trace('insert data at: ' + primaryKey) reflash(); } private function errorResult(e:sqlErrorEvent):void { trace(e.toString()) } public function reflash():void { try { var stmt:sqlStatement = new sqlStatement(); stmt.addEventListener(sqlEvent.RESULT,reflashResult); stmt.addEventListener(sqlErrorEvent.ERROR,reflasError); stmt.sqlConnection = conn; stmt.text = "SELECT * from flex"; stmt.execute(); } catch(error:sqlError) { trace(error.toString()) } } private function reflashResult(e:sqlEvent):void { var res:sqlResult = sqlStatement(e.currentTarget).getResult(); dataList.removeAll(); var ls:Array = res.data ; for(var i:int=0;i<ls.length;i++) { trace(ls[i]) dataList.addItem(ls[i]); } } private function reflasError(e:sqlErrorEvent):void { trace(e.toString()) } public function delItem(id:*):void { try { var stmt:sqlStatement = new sqlStatement(); stmt.addEventListener(sqlEvent.RESULT,delResult); stmt.addEventListener(sqlErrorEvent.ERROR,delError); stmt.sqlConnection = conn; stmt.text = "delete from flex where id = " + id; stmt.execute(); } catch(error:sqlError) { trace(error.toString()) } } private function delResult(e:sqlEvent):void { trace(e.toString()) var stmt:sqlStatement = sqlStatement(e.currentTarget); var result:sqlResult = stmt.getResult(); var primaryKey:Number = result.lastInsertRowID; trace('insert data at: ' + primaryKey) reflash(); } private function delError(e:sqlErrorEvent):void { trace(e.toString()) } } }