flex sqlite demo

<?xml version="1.0" encoding="utf-8"?>
<s:WindowedApplication xmlns:fx="http://ns.adobe.com/mxml/2009" 
  xmlns:s="library://ns.adobe.com/flex/spark" 
  xmlns:mx="library://ns.adobe.com/flex/mx" 
  width="1000" height="500"
  creationComplete="init(event)">
<fx:Script>
<![CDATA[
import mx.collections.ArrayCollection;
import mx.controls.Alert;
import mx.events.FlexEvent;

private var conn:SQLConnection;

private var stmt:SQLStatement;

private var file:File;

[Bindable]
private var fileSize:Number;

protected function init(event:FlexEvent):void
{
file = File.applicationStorageDirectory.resolvePath("myTestdb.db");

conn = new SQLConnection();

stmt = new SQLStatement();

try
{
stmt.sqlConnection = conn;
conn.open(file);
queryHandler();
}
catch(err:SQLError)
{
conn.rollback();
Alert.show(err.message);
}
}

protected function openDir(event:MouseEvent):void
{
if(file)
file.parent.browse([new FileFilter("数据库文件","*.db")]);
}

protected function addHandler(event:MouseEvent):void
{
conn.begin();
stmt.text = "create table if not exists emp(empId integer primary key autoincrement,firstName text,lastName text,salary  NUMERIC CHECK (salary > 0),createDate  date)";
stmt.execute(); 

//
stmt.text="insert into emp (firstName, lastName, salary,createDate) values ('test', 'this is a long string', 1110 ,datetime('now'))";
stmt.execute(); 
conn.commit();  
queryHandler();
}

[Bindable]
private var list:ArrayCollection;

protected function queryHandler(event:MouseEvent = null):void
{
conn.begin();
stmt.text="select * from emp";
stmt.execute(); 
conn.commit();  

var result:SQLResult = stmt.getResult();
if(result.complete)
{
list = new ArrayCollection(result.data);
}
fileSize = file.size;
}

public function deleteHandler(empId:int):void
{
conn.begin();
stmt.text = "delete from emp where empId = "+ empId;
stmt.execute(); 
conn.commit();  
queryHandler();
}

]]>
</fx:Script>

<s:layout>
<s:VerticalLayout horizontalAlign="center" paddingBottom="10" paddingLeft="10" paddingRight="10" paddingTop="10"/>
</s:layout>

<s:HGroup width="100%" horizontalAlign="center" verticalAlign="middle">
<s:Button label="打开数据库文件所在目录" click="openDir(event)"/>
<s:Button label="查询数据" click="queryHandler(event)"/>
<s:Button label="新增数据" click="addHandler(event)"/>
<s:Label text="数据库文件大小:{fileSize}B"/>
</s:HGroup>

<s:DataGrid dataProvider="{list}" width="900">
<s:columns>
<s:ArrayList>
<s:GridColumn dataField="empId" headerText="ID"/>
<s:GridColumn dataField="firstName" headerText="姓"/>
<s:GridColumn dataField="lastName" headerText="名"/>
<s:GridColumn dataField="salary" headerText="薪水"/>
<s:GridColumn dataField="createDate" headerText="创建时间"/>
<s:GridColumn  resizable="false" headerText="操作">
<s:itemRenderer>
<fx:Component>
<s:GridItemRenderer width="100%" height="100%">
<s:HGroup width="100%" height="100%" horizontalAlign="center" verticalAlign="middle">
<s:Label text="删除" buttonMode="true" textDecoration="underline" color="blue" click="outerDocument.deleteHandler(data.empId);"/>
</s:HGroup>
</s:GridItemRenderer>
</fx:Component>
</s:itemRenderer>
</s:GridColumn>
</s:ArrayList>
</s:columns>
</s:DataGrid>
</s:WindowedApplication>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值