Jan
12
早就想写篇flex与flash怎样读取sqlite数据库的文章了
网上大多都是flex的AIR中直接使用SQL类去连接的例子
咱做的是RIA
所以我就上这方面的东西
Flex 3.4(4.0其实也差不多),amfphp 1.9(唉,竟然是绝唱了),as3,sqlite 3.0,PHP 5.26
FLEX------------------------------------------------------------------------------------------
再来创建个类
基本上完成?no,还要给flex配置个参数,就像c#的config文件一样,这个很重要,不能忘(不过似乎可以用的别的方法)
<?xml version="1.0" encoding="UTF-8"?>
<services-config>
<services>
<service id="amfphp-flashremoting-service" class="flex.messaging.services.RemotingService" messageTypes="flex.messaging.messages.RemotingMessage">
<destination id="AMFPHP">
<channels>
<channel ref="my-amfphp"/>
</channels>
<properties>
<source>*</source>
</properties>
</destination>
</service>
</services>
<channels>
<channel-definition id="my-amfphp" class="mx.messaging.channels.AMFChannel">
<endpoint uri="http://localhost/gateway.php" class="flex.messaging.endpoints.AMFEndpoint"/>
</channel-definition>
</channels>
</services-config>
http://localhost/gateway.php的路径必须正确
下面就来弄amfphp那边的东西
NewsClass.php
X.php
接下来还得创建个SQLITE的数据库才行
不会用命令创建数据库的话可以去下个SQLiteManager,而且支持中文
数据库名:news.db
表名:news
字段:id name
然后把数据放到和php类相同的目录下就可以了
编译 运行 看到了什么?
对 什么都没有
因为没添加数据的原因……期待下篇的sqlite的写入
网上大多都是flex的AIR中直接使用SQL类去连接的例子
咱做的是RIA
所以我就上这方面的东西
Flex 3.4(4.0其实也差不多),amfphp 1.9(唉,竟然是绝唱了),as3,sqlite 3.0,PHP 5.26
FLEX------------------------------------------------------------------------------------------
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" horizontalAlign="center" initialize="init();" >
<mx:Script>
<![CDATA[
import mx.utils.ArrayUtil;
import mx.collections.ArrayCollection;
import mx.controls.Alert;
import flash.events.Event;
import flash.events.MouseEvent;
import mx.events.CloseEvent;
import mx.rpc.events.ResultEvent;
import mx.rpc.events.FaultEvent;
import mx.managers.PopUpManager;
[Bindable]
private var sqlitedata:ArrayCollection;
[Bindable]
public var news:News;
private function init():void
{
myamf.getNews.send();
}
private function getData(evt:ResultEvent):void
{
sqlitedata = new ArrayCollection(ArrayUtil.toArray(evt.result));
}
private function onFault(evt:FaultEvent):void
{
Alert.show(evt.fault.faultString, evt.fault.faultCode.toString());
}
private function onSelected(evt:Event):void
{
news= News(DataGrid(evt.target).selectedItem);
}
]]>
</mx:Script>
<mx:RemoteObject id="getNews" source="NewsClass"
destination="AMFPHP" fault="onFault(event)" showBusyCursor="false">
<mx:method name="getNews" result="getData(event)" fault="onFault(event)" />
</mx:RemoteObject>
<mx:DataGrid id="dg"
dataProvider="{sqlitedata}" width="600" height="300" horizontalCenter="0" y="0" editable="false" enabled="true" variableRowHeight="true">
<mx:columns>
<mx:DataGridColumn dataField="id" headerText="编号" resizable="false" width="40" textAlign="center"/>
<mx:DataGridColumn dataField="name" headerText="标题" resizable="false" sortable="false" width="560"/>
</mx:columns>
</mx:DataGrid>
</mx:Application>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" horizontalAlign="center" initialize="init();" >
<mx:Script>
<![CDATA[
import mx.utils.ArrayUtil;
import mx.collections.ArrayCollection;
import mx.controls.Alert;
import flash.events.Event;
import flash.events.MouseEvent;
import mx.events.CloseEvent;
import mx.rpc.events.ResultEvent;
import mx.rpc.events.FaultEvent;
import mx.managers.PopUpManager;
[Bindable]
private var sqlitedata:ArrayCollection;
[Bindable]
public var news:News;
private function init():void
{
myamf.getNews.send();
}
private function getData(evt:ResultEvent):void
{
sqlitedata = new ArrayCollection(ArrayUtil.toArray(evt.result));
}
private function onFault(evt:FaultEvent):void
{
Alert.show(evt.fault.faultString, evt.fault.faultCode.toString());
}
private function onSelected(evt:Event):void
{
news= News(DataGrid(evt.target).selectedItem);
}
]]>
</mx:Script>
<mx:RemoteObject id="getNews" source="NewsClass"
destination="AMFPHP" fault="onFault(event)" showBusyCursor="false">
<mx:method name="getNews" result="getData(event)" fault="onFault(event)" />
</mx:RemoteObject>
<mx:DataGrid id="dg"
dataProvider="{sqlitedata}" width="600" height="300" horizontalCenter="0" y="0" editable="false" enabled="true" variableRowHeight="true">
<mx:columns>
<mx:DataGridColumn dataField="id" headerText="编号" resizable="false" width="40" textAlign="center"/>
<mx:DataGridColumn dataField="name" headerText="标题" resizable="false" sortable="false" width="560"/>
</mx:columns>
</mx:DataGrid>
</mx:Application>
再来创建个类
package
{
[RemoteClass(alias = "NewsClass")]
[Bindable]
public class News
{
public var id:String;
public var name:String;
public function News()
{
}
}
}
{
[RemoteClass(alias = "NewsClass")]
[Bindable]
public class News
{
public var id:String;
public var name:String;
public function News()
{
}
}
}
基本上完成?no,还要给flex配置个参数,就像c#的config文件一样,这个很重要,不能忘(不过似乎可以用的别的方法)
<?xml version="1.0" encoding="UTF-8"?>
<services-config>
<services>
<service id="amfphp-flashremoting-service" class="flex.messaging.services.RemotingService" messageTypes="flex.messaging.messages.RemotingMessage">
<destination id="AMFPHP">
<channels>
<channel ref="my-amfphp"/>
</channels>
<properties>
<source>*</source>
</properties>
</destination>
</service>
</services>
<channels>
<channel-definition id="my-amfphp" class="mx.messaging.channels.AMFChannel">
<endpoint uri="http://localhost/gateway.php" class="flex.messaging.endpoints.AMFEndpoint"/>
</channel-definition>
</channels>
</services-config>
http://localhost/gateway.php的路径必须正确
下面就来弄amfphp那边的东西
NewsClass.php
<?php
include_once ("X.php");
class NewsClass{
public function getNews ()
{
$db = new PDO("sqlite:news.db");
$conn = $db->prepare('select * from news');
$conn->execute();
$result = $conn->fetchAll();
$arr = array();
for ($i = 0; $i < count($result); $i++)
{
$news= new X();
$news->id = $result[$i][0];
$news->name = $result[$i][1];
$arr[] = $tongji;
}
$conn = null;
$db = null;
return $arr;
}
}>
include_once ("X.php");
class NewsClass{
public function getNews ()
{
$db = new PDO("sqlite:news.db");
$conn = $db->prepare('select * from news');
$conn->execute();
$result = $conn->fetchAll();
$arr = array();
for ($i = 0; $i < count($result); $i++)
{
$news= new X();
$news->id = $result[$i][0];
$news->name = $result[$i][1];
$arr[] = $tongji;
}
$conn = null;
$db = null;
return $arr;
}
}>
X.php
<?php
class X
{
public $id;
public $name;
public $_explicitType = "News";//对应AS中的类名
}
class X
{
public $id;
public $name;
public $_explicitType = "News";//对应AS中的类名
}
接下来还得创建个SQLITE的数据库才行
不会用命令创建数据库的话可以去下个SQLiteManager,而且支持中文
数据库名:news.db
表名:news
字段:id name
然后把数据放到和php类相同的目录下就可以了
编译 运行 看到了什么?
对 什么都没有
因为没添加数据的原因……期待下篇的sqlite的写入



flex4 Flash
学习LCCS 第一步
