as3 air 操作sqlite 比操作xml的速度快的多,而且像触发器什么的,会让操作更方便,最近在做一个艺术品库的工具,用air+rebotlegs+sqlite,其中sqlite的封装是借鉴一个网络朋友的,做了一些简单的修改,把源码贴到这,以方便以后再使用,注意如果你的查询的属性有id,在查询结果中可以直接arr[0].id,但是如果你查询的是max(id),那么在结果中也是这样arr[0]["max(id)"],下面是源码 AIRSQLite.as package zjf.mvcs.db { import flash.data.SQLConnection; import flash.data.SQLMode; import flash.data.SQLResult; import flash.data.SQLStatement; import flash.errors.SQLError; import flash.filesystem.File; public class AIRSQLite { // 数据库连接字串 public static var localSQLServer:SQLConnection = new SQLConnection(); public function AIRSQLite() { } // 连接数据库 public static function setLocalSQLServer(dbURL:String):Boolean { try { var dbFile:File = File.applicationDirectory.resolvePath(dbURL); if(localSQLServer.connected) { return true; } if(dbFile.exists){ localSQLServer.open(dbFile, SQLMode.UPDATE); }else{ localSQLServer.open(dbFile); } } catch(error:Error) { return false; } return true; } // 执行SQL语句,返回影响的记录数 public static function executeSql(conn:SQLConnection, sqlString:String, cmdParams:SqlParameter):int { var result:SQLResult = querySQL(conn, sqlString, cmdParams); return result.rowsAffected; } // 执行查询语句,返回结果数组 public static function query(conn:SQLConnection, sqlString:String, cmdParams:SqlParameter):Array { var result:SQLResult = querySQL(conn, sqlString, cmdParams); return result.data; } // 执行查询语句,返回结果 public static function querySQL(conn:SQLConnection, sqlString:String, cmdParams:SqlParameter):SQLResult { var sqlstatement:SQLStatement = new SQLStatement(); sqlstatement.sqlConnection = conn; sqlstatement.text = sqlString; try { if(cmdParams && cmdParams.length > 0) { cmdParams.transParameters(sqlstatement); } sqlstatement.execute(); //cmdParams.clear(); } catch(error:SQLError) { trace(error.details); throw error; } return sqlstatement.getResult(); } // public static function querySQLWithOutParameter(conn:SQLConnection,sqlString:String):Boolean{ var sqlstatement:SQLStatement = new SQLStatement(); sqlstatement.sqlConnection = conn; sqlstatement.text = sqlString; try { sqlstatement.execute(); } catch(error:SQLError) { trace(error.details); throw error; return false; } return true; } // 检测一个记录是否存在 public static function exists(conn:SQLConnection, sqlString:String, cmdParams:SqlParameter):Boolean { var result:Array = query(conn, sqlString, cmdParams); return result.length > 0; } // 获取表某个字段的最大值 public static function getMaxID(conn:SQLConnection, FieldName:String, TableName:String):uint { var sql:String = "SELECT MAX(" + FieldName + ") FROM " + TableName; var result:Array = query(conn, sql, new SqlParameter()); if (result[0]["MAX("+FieldName+")"] != null) return result[0]["MAX("+FieldName+")"]; else return 0; } //总记录数 public static function getRecordNum(conn:SQLConnection, TableName:String, FieldName:String, cmdParams:SqlParameter, wheresql:String = ""):uint { var sql:String = "SELECT COUNT(" + FieldName + ") FROM " + TableName; sql += " "+wheresql; var result:Array = query(conn, sql, cmdParams); return uint(result[0]["COUNT("+FieldName+")"].toString()); } //pageSize 每页数据量 //pageIndex 页数 public static function pageList(conn:SQLConnection, sqlString:String, cmdParams:SqlParameter, pageSize:uint, pageIndex:uint):Array { sqlString += " LIMIT "+(pageSize*(pageIndex-1)).toString()+", "+pageSize.toString(); return query(conn, sqlString, cmdParams); } public static function clearCache(sqlConnection:SQLConnection):void { var stmt:SQLStatement = new SQLStatement(); stmt.sqlConnection = sqlConnection; stmt.text = "DELETE FROM statements"; stmt.execute(); } } } SqlParameter.as package zjf.mvcs.db { import flash.data.SQLStatement; public dynamic class SqlParameter extends Object { public function SqlParameter() { } //给SQLStatement参数赋值 public function transParameters(SQLStatementParameters:SQLStatement):void { for (var i:String in this) { SQLStatementParameters.parameters[i] = this[i]; } } //清除全部属性 public function clear():void { for (var i:String in this) { delete this[i]; } } //获取属性数量 public function get length():uint { var len:uint = 0; for (var i:String in this) { len ++; } return len; } } } 使用就更简单了 package zjf.mvcs.service { import org.robotlegs.mvcs.Actor; import tmic.vr.mvcs.db.AIRSQLite; import tmic.vr.mvcs.db.SqlParameter; import tmic.vr.mvcs.events.LanEvent; import tmic.vr.mvcs.infa.ILanService; import tmic.vr.mvcs.vo.LanVo; public class LanService extends Actor implements ILanService { public function LanService() { super(); } public function addLan(obj:Object):void{ try{ if(!AIRSQLite.setLocalSQLServer(obj.dbUrl)) { trace("连接失败"); return; } var sql:String = "insert into lan (shot,display) values(@shot,@display);"; var parameter:SqlParameter = new SqlParameter(); parameter["@shot"] = obj.shot; parameter["@display"] = obj.dis; if(AIRSQLite.executeSql(AIRSQLite.localSQLServer,sql,parameter)==1){ dispatch(new LanEvent(LanEvent.LAN_INSERT_SUCESS)); showLan(obj.dbUrl); } }catch(e:Error){ trace("sql error !@"+e.toString()); dispatch(new LanEvent(LanEvent.LAN_INSERT_FAILS)); return; } } public function updLan(vo:LanVo,str:String):void{ try{ if(!AIRSQLite.setLocalSQLServer(str)) { trace("连接失败"); return; } var sql:String = "update lan set shot=@shot,display=@display where lid=@lid;"; var parameter:SqlParameter = new SqlParameter(); parameter["@shot"] = vo.shot; parameter["@display"] = vo.dis; parameter["@lid"] = vo.lanId; if(AIRSQLite.executeSql(AIRSQLite.localSQLServer,sql,parameter)==1){ dispatch(new LanEvent(LanEvent.LAN_UPD_SUCC)); showLan(str); } }catch(e:Error){ trace("sql error !@"+e.toString()); dispatch(new LanEvent(LanEvent.LAN_UPD_FAIL)); return; } } public function showLan(str:String):void{ try{ if(!AIRSQLite.setLocalSQLServer(str)) { trace("连接失败"); return; } var sql:String = "select * from lan"; var arr:Array = AIRSQLite.query(AIRSQLite.localSQLServer,sql,null); if(arr == null){ arr = new Array(); } dispatch(new LanEvent(LanEvent.LAN_SHOW,arr)); }catch(e:Error){ trace("sql error !@"+e.toString()); return; } } } }