Flash AS3.0可以直接连接数据库么?

Flash AS3.0可以直接连接数据库么?,第1张

as 可以和xml数据库相连接,不过,as只能读取xml的数据而不能写入数据,这是为了安全考虑的.想要写入数据时就要求与ASP否PHP来相互作用。

xml在flash里面有专门的类,你可以打开帮助看一看

为了使用简单,我做了一个简单的包装类(包含一个数据结构):

package com.easily.ds

{

public class DataBaseData

{

public var host:String

public var port:int

public var username:String

public var password:String

public var database:String

}

}

package com.easily.util

{

import com.easily.ds.DataBaseData

import com.maclema.mysql.Connection

import com.maclema.mysql.Field

import com.maclema.mysql.MySqlResponse

import com.maclema.mysql.MySqlToken

import com.maclema.mysql.ResultSet

import com.maclema.mysql.Statement

import flash.events.Event

import flash.events.EventDispatcher

import mx.rpc.AsyncResponder

/**

* @author Easily

*/

public class DataBase extends EventDispatcher

{

private var mDataBase:DataBaseData

private var mConnection:Connection

public function DataBase(database:DataBaseData)

{

mDataBase = database

}

public function connect():void

{

mConnection = new Connection(mDataBase.host, mDataBase.port,

mDataBase.username, mDataBase.password, mDataBase.database)

mConnection.addEventListener(Event.CONNECT, onConnected)

mConnection.connect()

function onConnected(event:Event):void

{

mConnection.removeEventListener(Event.CONNECT, onConnected)

dispatchEvent(event)

}

}

public function disconnect():void

{

mConnection.disconnect()

}

public function select(sql:String, completeHandler:Function, errorHandler:Function = null):void

{

var st:Statement = mConnection.createStatement()

var token:MySqlToken = st.executeQuery(sql)

var responder:AsyncResponder = new AsyncResponder(resultHandler, faultHandler, token)

token.addResponder(responder)

function resultHandler(result:Object/*ResultSet*/, token:Object/*MySqlToken*/):void

{

var data:Array = []

if (result is ResultSet)

{

var fieldList:Array = result.getColumns()

while (result.next())

{

var item:Object = {}

for each (var field:Field in fieldList)

{

item[field.getName()] = result.getString(field.getName())

}

data.push(item)

}

}

completeHandler(data)

}

function faultHandler(info:Object, token:Object):void

{

if (errorHandler == null) return

errorHandler()

}

}

public function insert(sql:String, completeHandler:Function, errorHandler:Function = null):void

{

var st:Statement = mConnection.createStatement()

var token:MySqlToken = st.executeQuery(sql)

var responder:AsyncResponder = new AsyncResponder(resultHandler, faultHandler, token)

token.addResponder(responder)

function resultHandler(result:Object/*MySqlResponse*/, token:Object/*MySqlToken*/):void

{

completeHandler(result.insertID)

}

function faultHandler(info:Object, token:Object):void

{

if (errorHandler == null) return

errorHandler()

}

}

public function remove(sql:String, completeHandler:Function, errorHandler:Function = null):void

{

var st:Statement = mConnection.createStatement()

var token:MySqlToken = st.executeQuery(sql)

var responder:AsyncResponder = new AsyncResponder(resultHandler, faultHandler, token)

token.addResponder(responder)

function resultHandler(result:Object/*MySqlResponse*/, token:Object/*MySqlToken*/):void

{

completeHandler()

}

function faultHandler(info:Object, token:Object):void

{

if (errorHandler == null) return

errorHandler()

}

}

}

}

其中有3个方法,select,insert,remove,分别对应了SQL语句中的select,insert,delete

用法很简单,先连接:

var databaseData:DataBaseData = new DataBaseData()

databaseData.host = "127.0.0.1"

databaseData.database = "game"

databaseData.password = "123456"

databaseData.port = 3306

databaseData.username = "root"

var dataBase:DataBase = new DataBase(databaseData)

dataBase.addEventListener(Event.CONNECT, onConnected)

dataBase.connect()

function onConnected(event:Event):void

{

dataBase.removeEventListener(Event.CONNECT, onConnected)

dispatchEvent(new Event(Event.COMPLETE))

}

如果不需要用了可以先断开连接:

dataBase.disconnect()

下面是select语句:

var sql:String = "select id,name from `npc`"

dataBase.select(sql, endQuery)

function endQuery(data:Array):void

{

var npcList:Array= []

for each (var item:Object in data)

{

var npc:Object = {id:item.id, name:item.name}

npcList.push(npc)

}

}

insert语句用法会返回一个insertID,也就是插入的那条数据的ID。

你用Flash builder?那怎么会对这个概念原理了解的那么差。.asp是服务端执行文件,如果说和sql交互,那么这个就是个中间文件,as通过http向.asp提交数据,asp处理好再存入数据库,或者取出来,返回给flash。。。


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/sjk/6809961.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-03-28
下一篇 2023-03-28

发表评论

登录后才能评论

评论列表(0条)

保存