西西软件园多重安全检测下载网站、值得信赖的软件下载站!
软件
软件
文章
搜索

首页西西教程数据库教程 → 在ActionScript 3 MySql Driver连接MYSQL数据库经验分享

在ActionScript 3 MySql Driver连接MYSQL数据库经验分享

相关软件相关文章发表评论 来源:西西整理时间:2011/4/24 7:52:32字体大小:A-A+

作者:西西点击:407次评论:0次标签: MySql

  • 类型:电子教程大小:8.5M语言:中文 评分:8.3
  • 标签:
立即下载

最近由于制作任务编辑器,需要连接到数据库中进行一些任务数据的操作,而我们存放任务数据的数据库中MYSQL,所以就找了一下AS3接连MYSQL数据库的资料。

找到一个好东西:ActionScript 3 MySql Driver,用SVN的童鞋可以直接checkout:

http://assql.googlecode.com/svn/trunk/
为了使用简单,我做了一个简单的包装类(包含一个数据结构):
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;

}

}

view sourceprint?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语句:

view sourceprint?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。

    相关评论

    阅读本文后您有什么感想? 已有人给出评价!

    • 8 喜欢喜欢
    • 3 顶
    • 1 难过难过
    • 5 囧
    • 3 围观围观
    • 2 无聊无聊

    热门评论

    最新评论

    发表评论 查看所有评论(0)

    昵称:
    表情: 高兴 可 汗 我不要 害羞 好 下下下 送花 屎 亲亲
    字数: 0/500 (您的评论需要经过审核才能显示)