西西軟件園多重安全檢測(cè)下載網(wǎng)站、值得信賴(lài)的軟件下載站!
軟件
軟件
文章
搜索

首頁(yè)西西教程數(shù)據(jù)庫(kù)教程 → 在ActionScript 3 MySql Driver連接MYSQL數(shù)據(jù)庫(kù)經(jīng)驗(yàn)分享

在ActionScript 3 MySql Driver連接MYSQL數(shù)據(jù)庫(kù)經(jīng)驗(yàn)分享

相關(guān)軟件相關(guān)文章發(fā)表評(píng)論 來(lái)源:西西整理時(shí)間:2011/4/24 7:52:32字體大小:A-A+

作者:西西點(diǎn)擊:407次評(píng)論:0次標(biāo)簽: MySql

  • 類(lèi)型:電子教程大。8.5M語(yǔ)言:中文 評(píng)分:8.3
  • 標(biāo)簽:
立即下載

最近由于制作任務(wù)編輯器,需要連接到數(shù)據(jù)庫(kù)中進(jìn)行一些任務(wù)數(shù)據(jù)的操作,而我們存放任務(wù)數(shù)據(jù)的數(shù)據(jù)庫(kù)中MYSQL,所以就找了一下AS3接連MYSQL數(shù)據(jù)庫(kù)的資料。

找到一個(gè)好東西:ActionScript 3 MySql Driver,用SVN的童鞋可以直接checkout:

http://assql.googlecode.com/svn/trunk/
為了使用簡(jiǎn)單,我做了一個(gè)簡(jiǎn)單的包裝類(lèi)(包含一個(gè)數(shù)據(jù)結(jié)構(gòu)):
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個(gè)方法,select,insert,remove,分別對(duì)應(yīng)了SQL語(yǔ)句中的select,insert,delete

用法很簡(jiǎn)單,先連接:
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));

}


如果不需要用了可以先斷開(kāi)連接:

dataBase.disconnect();

下面是select語(yǔ)句:

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語(yǔ)句用法會(huì)返回一個(gè)insertID,也就是插入的那條數(shù)據(jù)的ID。

    相關(guān)評(píng)論

    閱讀本文后您有什么感想? 已有人給出評(píng)價(jià)!

    • 8 喜歡喜歡
    • 3 頂
    • 1 難過(guò)難過(guò)
    • 5 囧
    • 3 圍觀圍觀
    • 2 無(wú)聊無(wú)聊

    熱門(mén)評(píng)論

    最新評(píng)論

    發(fā)表評(píng)論 查看所有評(píng)論(0)

    昵稱(chēng):
    表情: 高興 可 汗 我不要 害羞 好 下下下 送花 屎 親親
    字?jǐn)?shù): 0/500 (您的評(píng)論需要經(jīng)過(guò)審核才能顯示)