package { import flash.data.SQLConnection; 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; public class DBUtil { /** * 数据库操作类别 **/ private const typeArr:Array = ['select','insert','update','delete','create','alter','drop']; /** * 连接 **/ private var conn:SQLConnection; /** * 声明 **/ private var stmt:SQLStatement; /** * 结果集合 **/ private var rsAC:ArrayCollection; public function DBUtil() { } /** * 获得连接 **/ private function getConn():SQLConnection { if(this.conn == null){ conn = new SQLConnection(); conn.addEventListener(SQLEvent.OPEN, sqlOpen); conn.addEventListener(SQLErrorEvent.ERROR, sqlError); } return this.conn; } /** * 关闭连接 **/ private function closeConn():void { if(this.conn != null){ this.conn.close(); this.conn = null; } } /** * 获得声明 **/ private function getStatement():SQLStatement { if(this.stmt == null) { stmt = new SQLStatement(); stmt.sqlConnection = getConn(); stmt.addEventListener(SQLErrorEvent.ERROR, sqlError); } return this.stmt; } /** * 关闭声明 **/ private function closeStatement():void { if(this.stmt != null) { this.stmt.cancel(); this.stmt = null; } } /** * 打开或新建数据库 **/ private function openOrCreateDataBase(dbName:String):void { var dbFile:File = File.applicationStorageDirectory.resolvePath(dbName + ".db"); try { getConn().open(dbFile); trace("the database was opened or created successfully"); } catch(error:SQLError) { closeConn(); trace("Error message:"+error.message); trace("Details:"+error.details); } } /** * 执行sql语句 * dbName:数据库名 * type:数据库操作类别 * sql:sql语句 **/ public function executeSql(dbName:String,type:String,sql:String):* { var typeIndex:int = typeArr.indexOf(type); if(typeIndex != -1) { openOrCreateDataBase(dbName); switch(typeIndex) { case 0: getStatement().addEventListener(SQLEvent.RESULT, selectResult); break; case 1: getStatement().addEventListener(SQLEvent.RESULT, executeResult); break; case 2: getStatement().addEventListener(SQLEvent.RESULT, executeResult); break; case 3: getStatement().addEventListener(SQLEvent.RESULT, executeResult); break; case 4: getStatement().addEventListener(SQLEvent.RESULT, executeResult); break; case 5: getStatement().addEventListener(SQLEvent.RESULT, executeResult); break; } getStatement().text = sql; getStatement().execute(); } return this.rsAC; } private function selectResult(event:SQLEvent):void { var rs:SQLResult = getStatement().getResult(); if(rs!=null && rs.data!=null && rs.data.length>0){ this.rsAC = new ArrayCollection(rs.data); } closeStatement(); closeConn(); } private function executeResult(event:SQLEvent):void { closeStatement(); closeConn(); trace("executed successfully"); } private function sqlOpen(event:SQLEvent):void { trace("the SQLConnection was opened or created successfully"); } private function sqlError(event:SQLErrorEvent):void { closeStatement(); closeConn(); trace("Error message:"+event.error.message); trace("Details:"+event.error.details); } } }