AIR数据处理的方法

 

AIR连接sqllite的方法,这里主要是做了数据的插入和删除的方法

<?xml version="1.0" encoding="utf-8"?>
<mx:WindowedApplication xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute"
preinitialize="init()"
fontSize="12" backgroundColor="#FFFFFF" width="600" height="700">
<mx:Script>
<![CDATA[
import flash.data.SQLConnection;
import flash.events.SQLErrorEvent;
import flash.events.SQLEvent;
import flash.filesystem.File;

private var conn:SQLConnection;
private var initComplete:Boolean = false;
private var sqlStat:SQLStatement;

private function init():void{
//新建sqlConnection
conn = new SQLConnection();
//不知道flash什么时候才能做成多线程..这样写起来真的很累
conn.addEventListener(SQLEvent.OPEN, openHandler);
conn.addEventListener(SQLErrorEvent.ERROR, errorHandler);
//获取当前目录
var dbFile:File = File.applicationResourceDirectory.resolvePath("DBSample.db");
//打开sql连接
conn.open(dbFile);
}

//连接后对数据库进行初始化,初始化脚本可以是*.sql,这里就不做这种处理了
private function openHandler(event:SQLEvent):void
{
//初始化sqlStatement对象
sqlStat = new SQLStatement();
sqlStat.sqlConnection = conn;
var sql:String = "CREATE TABLE IF NOT EXISTS employees (" +
" empId INTEGER PRIMARY KEY AUTOINCREMENT, " +
" firstName TEXT, " +
" lastName TEXT, " +
" salary NUMERIC CHECK (salary > 0)" +
")";
sqlStat.text = sql;
sqlStat.addEventListener(SQLEvent.RESULT, statResult);
sqlStat.addEventListener(SQLErrorEvent.ERROR, createError);
sqlStat.execute();
}
private function statResult(event:SQLEvent):void
{
//很无耻地强迫获取数据时更新数据
var sqlresult:SQLResult = sqlStat.getResult();
if(sqlresult.data == null){
getResult();
return;
}
datafiled.dataProvider = sqlresult.data;
}
//获取数据函数
private function getResult():void{
var sqlquery:String = "SELECT * FROM employees"
excuseUpdate(sqlquery);
}
private function createError(event:SQLErrorEvent):void
{
trace("Error code:", event.error.code);
trace("Details:", event.error.message);
}

private function errorHandler(event:SQLErrorEvent):void
{
trace("Error code:", event.error.code);
trace("Details:", event.error.message);
}
//数据更新接口
private function excuseUpdate(sql:String):void{
sqlStat.text = sql;
sqlStat.execute();
}
//插入操作
private function insertemp():void{
var sqlupdate:String = "Insert into employees(firstName,lastName,salary) values('" +
firstName.text +
"','" +
lastName.text +
"','" +
salary.text +
"')";
debug.text+=sqlupdate+"\n"
excuseUpdate(sqlupdate)
}
//删除操作
private function deleteemp():void{
var sqldelete:String = "delete from employees where empId='" +
datafiled.selectedItem.empId +
"'";
excuseUpdate(sqldelete);
debug.text+=sqldelete+"\n"
}

]]>
</mx:Script>
<mx:TextArea x="21" y="10" width="402" height="179" id="debug"/>
<mx:DataGrid x="21" y="197" id="datafiled">
<mx:columns>
<mx:DataGridColumn headerText="ID" dataField="empId"/>
<mx:DataGridColumn headerText="firstName" dataField="firstName"/>
<mx:DataGridColumn headerText="lastName" dataField="lastName"/>
<mx:DataGridColumn headerText="salary" dataField="salary"/>
</mx:columns>
</mx:DataGrid>
<mx:Form x="21" y="471">
<mx:FormItem label="firstName">
<mx:TextInput id="firstName"/>
</mx:FormItem>
<mx:FormItem label="lastName">
<mx:TextInput id="lastName"/>
</mx:FormItem>
<mx:FormItem label="salary">
<mx:TextInput id="salary"/>
</mx:FormItem>
</mx:Form>
<mx:Button x="300" y="503" label="添加" click="insertemp()"/>
<mx:Button x="300" y="533" label="删除" click="deleteemp()"/>
</mx:WindowedApplication>

 

posted on 2011-11-14 11:21  AS3.0  阅读(581)  评论(0编辑  收藏  举报

导航