最近由於制作任務編輯器,需要連接到數據庫中進行一些任務數據的操作,而我們存放任務數據的數據庫中MYSQL,
所以就找了一下AS3接連MYSQL數據庫的資料。
擴張閱讀:
ActionScript簡稱“AS”——動作腳本,是遵循 ECMAscript第四版 的 Adobe Flash Player 運行時環境的編程語言。
它在 Flash 內容和應用程序中實現交互性、數據處理以及其他功能。 ActionScript是Flash的腳本語言,與JavaScript相似,
ActionScript是一種編程語言,新出的AS3.0使用OOP(面對對象編程),增加更強的報錯能力,指定類型也更明確。
找到一個好東西: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;
- }
- }
- 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。