锁定老帖子 主题:一个基于sqlite3的air应用demo
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
|
|
---|---|
作者 | 正文 |
发表时间:2010-03-22
最后修改:2010-03-23
在Demo开始之前,先找了找AIR的资料,如下:
二、开始代码工作: 因为就是一个CURD的demo,所有做的有点粗糙,一个mxml搞定。如下: 1.初始化部分 主要是连接数据库,通过myTestdb.db创建。
<mx:WindowedApplication xmlns:mx="http://www.adobe.com/2006/mxml" fontSize="12" layout="absolute" width="650" height="650" creationComplete="init();initApp();" viewSourceURL="srcview/index.html"> <mx:Script> <![CDATA[ import mx.core.Window; import mx.collections.ArrayCollection; import mx.controls.Alert; import mx.managers.PopUpManager; private var con:SQLConnection; private var createStmt:SQLStatement; private function init():void { var file:File = File.applicationStorageDirectory.resolvePath("myTestdb.db"); //先通过应用程序的存储路径找到myTestdb.db 如果没有自动创建一个同名的db文件 con = new SQLConnection(); var stmt:SQLStatement = new SQLStatement(); try { con.open(file); stmt.sqlConnection = con; createStmt = new SQLStatement(); createStmt.sqlConnection = con; var sql:String = "CREATE TABLE IF NOT EXISTS emp (" + "empId INTEGER PRIMARY KEY AUTOINCREMENT, " + "firstName TEXT, " +//文本字符串格式 "lastName TEXT, " + "salary NUMERIC CHECK (salary > 0)"+")"; createStmt.text = sql; createStmt.execute(); } catch(error:SQLError) { Alert.show(error.message); Alert.show(error.details); } } </mx:Script> 2.查询列表
[Bindable] private var list:ArrayCollection; private function initApp():void { list = new ArrayCollection(); var stmt:SQLStatement = new SQLStatement(); stmt.sqlConnection = con; stmt.text = "select empId,firstName,lastName,salary from emp order by empId desc"; stmt.execute(); var result:SQLResult = stmt.getResult(); if(result.data!=null) { var numResults:int =result.data.length; var output:String=""; for (var i:int = 0; i < numResults; i++) { var row:Object = result.data[i]; list.addItem({empId:row.empId,firstName:row.firstName, lastName:row.lastName,salary:row.salary}); } this.lemp.text = "查询的结果如下:"; } } 3. 更新用户
//更新 public function updateEmp():void { var stmt:SQLStatement = new SQLStatement(); stmt.sqlConnection = con; var sql:String = "update emp set firstName=:firstName,lastName=:lastName,salary=:salary where empId=:empId"; var firstname:String = this.showdata.selectedItem.firstName; var empid:String = this.showdata.selectedItem.empId; var lastname:String = this.showdata.selectedItem.lastName; var salary:String = this.showdata.selectedItem.salary; stmt.parameters[":firstName"]=firstname; stmt.parameters[":lastName"]=lastname; stmt.parameters[":salary"]=salary; stmt.parameters[":empId"]=empid; stmt.text = sql; stmt.execute(); initApp();//刷新数据 var result:SQLResult = stmt.getResult(); var count:Number = result.rowsAffected; Alert.show("成功修改了:"+count+" 行.."); } 4. 增加用户
//增加 private function addUser():void { //adduserwin = new addUserWin(); //adduserwin = addUserWin(PopUpManager.createPopUp(this,addUserWin,false,null)); //PopUpManager.centerPopUp(adduserwin); this.form.visible = true; this.afirstName.setFocus(); } private function addConfirm():void { var stmt:SQLStatement = new SQLStatement(); stmt.sqlConnection = con; var firstName:String = this.afirstName.text; var lastName:String = this.alastName.text; var salary:Number = Number(this.asalary.text); if(firstName ==''|| lastName ==''|| salary.toString() =='') { Alert.show("请填写完整信息!!","注意"); return; } var sql:String = "INSERT INTO emp(firstName,lastName,salary) VALUES (@firstName, @lastName, @salary)"; stmt.text = sql; stmt.parameters["@firstName"]=firstName; stmt.parameters["@lastName"]=lastName; stmt.parameters["@salary"]=salary; stmt.execute(); initApp(); var result:SQLResult = stmt.getResult(); var count:Number = result.rowsAffected; this.afirstName.text = ''; this.alastName.text = ''; this.asalary.text = ''; this.afirstName.setFocus(); Alert.show("添加员工:"+firstName+lastName+"成功!!影响:"+count+"行..","提示"); } 5. 清空
private function release():void { this.form.visible = false; this.afirstName.text = ''; this.alastName.text = ''; this.asalary.text = ''; this.afirstName.setFocus(); } 6.搜索功能
//搜索 private var responder:Responder; private function search():void { list = new ArrayCollection(); var search:String = this.soInput.text; var stmt:SQLStatement = new SQLStatement(); stmt.sqlConnection = con; var sql:String = "select empId,firstName,lastName,salary from emp where lastName like :str"; stmt.text = sql; stmt.parameters[":str"]= "%"+search+"%"; stmt.execute(); var result:SQLResult = stmt.getResult(); if(result.data!=null) { var numResults:int =result.data.length; var output:String=""; for (var i:int = 0; i < numResults; i++) { var row:Object = result.data[i]; list.addItem({empId:row.empId,firstName:row.firstName,lastName:row. lastName,salary:row.salary}); } this.lemp.text = "查询的结果如下:"; }else { Alert.show("抱歉!没有搜索到您想要的结果!!","提示"); return; } }
第二部分: UI界面设计 做的粗糙,但是基本的样子是初来了。代码如下:
<mx:Button click="initApp()" label="查询所有员工" x="212" y="136" fontSize="12"/> <mx:Button click="del()" label="删除员工" x="361" y="42" fontSize="12"/> <mx:TextInput id="empid" width="100" x="242" y="44" backgroundColor="#EAE5E5"/> <mx:Label text="请输入员工编号:" x="126" y="43" fontSize="13" color="#EE4E16" fontWeight="bold"/> <mx:HRule id="hr" width="100%"/> <mx:DataGrid id="showdata" dataProvider="{list}" x="113" y="212" textAlign="center" editable="true" color="#894803" fontSize="12" alternatingItemColors="[#C7C0C0, #FFFFFF]"> <mx:columns> <mx:DataGridColumn dataField="empId" headerText="员工编号" width="80" editable="false"/> <mx:DataGridColumn dataField="firstName" headerText="员工姓氏" editable="true" width="80"/> <mx:DataGridColumn dataField="lastName" headerText="员工名字" editable="true" width="80"/> <mx:DataGridColumn dataField="salary" headerText="员工工资" editable="true" width="100"/> <mx:DataGridColumn headerText="操作" width="80"> <mx:itemRenderer> <mx:Component> <mx:LinkButton label="更新" click="outerDocument.updateEmp()" fontSize="11"/> </mx:Component> </mx:itemRenderer> </mx:DataGridColumn> </mx:columns> </mx:DataGrid> <mx:Label x="242" y="178" id="lemp" name="lemp" text="员工列表" width="150" fontSize="15" color="#6E3002" fontWeight="bold" textAlign="center"/> <mx:Button x="335" y="134" label="增加员工" fontSize="12" click="addUser();"/> <mx:HRule width="100%" horizontalCenter="0" verticalCenter="-152"/> <mx:Form id="form" name="form" visible="false" fontSize="13" horizontalCenter="0" backgroundColor="#E6E2E2" verticalCenter="214"> <mx:FormItem label="员工姓氏:" borderColor="#AEBFCA" borderStyle="none" borderThickness="1"> <mx:TextInput id="afirstName" name="afirstName" width="160" color="#000000"/> </mx:FormItem> <mx:FormItem label="员工名字:" borderColor="#AEBFCA" borderStyle="none" borderThickness="1"> <mx:TextInput id="alastName" name="alastName" width="160" displayAsPassword="false" borderColor="#B7BABC"/> </mx:FormItem> <mx:FormItem label="员工工资:" borderColor="#AEBFCA" borderStyle="none" borderThickness="1"> <mx:TextInput id="asalary" name="asalary" width="160" displayAsPassword="false" borderColor="#B7BABC"/> </mx:FormItem> <mx:HBox width="225"> <mx:Spacer width="50"/> <mx:Button label="确定" fontWeight="normal" click="addConfirm();"/> <mx:Spacer width="15"/> <mx:Button label="取消" click="release()" fontWeight="normal"/> </mx:HBox> </mx:Form> <mx:Label x="127" y="87" text="搜索员工名字:" width="110" height="21" fontSize="13" color="#F80718" fontWeight="bold"/> <mx:TextInput x="244" y="88" width="218" id="soInput" backgroundColor="#ECEBEB"/> <mx:Button x="470" y="88" label="GO" cornerRadius="10" color="#AD6A0A" fontSize="12" click="search();"/> 声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
推荐链接
|
|
返回顶楼 | |
发表时间:2010-03-29
最后修改:2010-03-29
通过这个applicationStorageDirectory.resolvePath创建的文件是虚拟的文件???
事实上不存在?? ------------------------ 已经知道了,汗,原来是存放在APPLICATION下面的。。 |
|
返回顶楼 | |
发表时间:2010-03-29
chinamail 写道 通过这个applicationStorageDirectory.resolvePath创建的文件是虚拟的文件???
事实上不存在?? ------------------------ 已经知道了,汗,原来是存放在APPLICATION下面的。。 恩 对 呵呵 |
|
返回顶楼 | |
浏览 4814 次