AIR 操作 sqlite 测试

demo: http://www.sandy1219.com/air/localDatabase.rar<textarea cols="68" rows="20" name="code" class="c-sharp:collapse:showcolumns">package { 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&lt;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()) } } }</textarea>

你可能感兴趣的:(AIR 操作 sqlite 测试)