這篇文章主要介紹“怎么把AS3連接到MySQL數(shù)據(jù)庫上”,在日常操作中,相信很多人在怎么把AS3連接到MySQL數(shù)據(jù)庫上問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”怎么把AS3連接到MySQL數(shù)據(jù)庫上”的疑惑有所幫助!接下來,請跟著小編一起來學(xué)習吧!
成都創(chuàng)新互聯(lián)公司成立于2013年,我們提供高端重慶網(wǎng)站建設(shè)公司、成都網(wǎng)站制作、網(wǎng)站設(shè)計、網(wǎng)站定制、成都營銷網(wǎng)站建設(shè)、成都小程序開發(fā)、微信公眾號開發(fā)、seo優(yōu)化排名服務(wù),提供專業(yè)營銷思路、內(nèi)容策劃、視覺設(shè)計、程序開發(fā)來完成項目落地,為成都食品包裝袋企業(yè)提供源源不斷的流量和訂單咨詢。
找到一個好東西:ActionScript3MySqlDriver,用SVN的童鞋可以直接checkout:
http://assql.googlecode.com/svn/trunk/為了使用簡單,我做了一個簡單的包裝類(包含一個數(shù)據(jù)結(jié)構(gòu)):
packagecom.easily.ds{publicclassDataBaseData{publicvarhost:String;publicvarport:int;publicvarusername:String;publicvarpassword:String;publicvardatabase:String;}}
packagecom.easily.util{importcom.easily.ds.DataBaseData;importcom.maclema.mysql.Connection;importcom.maclema.mysql.Field;importcom.maclema.mysql.MySqlResponse;importcom.maclema.mysql.MySqlToken;importcom.maclema.mysql.ResultSet;importcom.maclema.mysql.Statement;importflash.events.Event;importflash.events.EventDispatcher;importmx.rpc.AsyncResponder;/***@authorEasily*/publicclassDataBaseextendsEventDispatcher{privatevarmDataBase:DataBaseData;privatevarmConnection:Connection;publicfunctionDataBase(database:DataBaseData){mDataBase=database;}publicfunctionconnect():void{mConnection=newConnection(mDataBase.host,mDataBase.port,mDataBase.username,mDataBase.password,mDataBase.database);mConnection.addEventListener(Event.CONNECT,onConnected);mConnection.connect();functiononConnected(event:Event):void{mConnection.removeEventListener(Event.CONNECT,onConnected);dispatchEvent(event);}}publicfunctiondisconnect():void{mConnection.disconnect();}publicfunctionselect(sql:String,completeHandler:Function,errorHandler:Function=null):void{varst:Statement=mConnection.createStatement();vartoken:MySqlToken=st.executeQuery(sql);varresponder:AsyncResponder=newAsyncResponder(resultHandler,faultHandler,token);token.addResponder(responder);functionresultHandler(result:Object/*ResultSet*/,token:Object/*MySqlToken*/):void{vardata:Array=[];if(resultisResultSet){varfieldList:Array=result.getColumns();while(result.next()){varitem:Object={};foreach(varfield:FieldinfieldList){item[field.getName()]=result.getString(field.getName());}data.push(item);}}completeHandler(data);}functionfaultHandler(info:Object,token:Object):void{if(errorHandler==null)return;errorHandler();}}publicfunctioninsert(sql:String,completeHandler:Function,errorHandler:Function=null):void{varst:Statement=mConnection.createStatement();vartoken:MySqlToken=st.executeQuery(sql);varresponder:AsyncResponder=newAsyncResponder(resultHandler,faultHandler,token);token.addResponder(responder);functionresultHandler(result:Object/*MySqlResponse*/,token:Object/*MySqlToken*/):void{completeHandler(result.insertID);}functionfaultHandler(info:Object,token:Object):void{if(errorHandler==null)return;errorHandler();}}publicfunctionremove(sql:String,completeHandler:Function,errorHandler:Function=null):void{varst:Statement=mConnection.createStatement();vartoken:MySqlToken=st.executeQuery(sql);varresponder:AsyncResponder=newAsyncResponder(resultHandler,faultHandler,token);token.addResponder(responder);functionresultHandler(result:Object/*MySqlResponse*/,token:Object/*MySqlToken*/):void{completeHandler();}functionfaultHandler(info:Object,token:Object):void{if(errorHandler==null)return;errorHandler();}}}}
其中有3個方法,select,insert,remove,分別對應(yīng)了SQL語句中的select,insert,delete
如何將AS3連接到MySQL數(shù)據(jù)庫上
用法很簡單,先連接:
vardatabaseData:DataBaseData=newDataBaseData();databaseData.host="127.0.0.1";databaseData.database="game";databaseData.password="123456";databaseData.port=3306;databaseData.username="root";vardataBase:DataBase=newDataBase(databaseData);dataBase.addEventListener(Event.CONNECT,onConnected);dataBase.connect();functiononConnected(event:Event):void{dataBase.removeEventListener(Event.CONNECT,onConnected);dispatchEvent(newEvent(Event.COMPLETE));}
如果不需要用了可以先斷開連接:
dataBase.disconnect();
下面是select語句:
varsql:String="selectid,namefrom`npc`";dataBase.select(sql,endQuery);functionendQuery(data:Array):void{varnpcList:Array=[];foreach(varitem:Objectindata){varnpc:Object={id:item.id,name:item.name};npcList.push(npc);}}
insert語句用法會返回一個insertID,也就是插入的那條數(shù)據(jù)的ID。
到此,關(guān)于“怎么把AS3連接到MySQL數(shù)據(jù)庫上”的學(xué)習就結(jié)束了,希望能夠解決大家的疑惑。理論與實踐的搭配能更好的幫助大家學(xué)習,快去試試吧!若想繼續(xù)學(xué)習更多相關(guān)知識,請繼續(xù)關(guān)注創(chuàng)新互聯(lián)網(wǎng)站,小編會繼續(xù)努力為大家?guī)砀鄬嵱玫奈恼拢?/p>
網(wǎng)頁題目:怎么把AS3連接到MySQL數(shù)據(jù)庫上
文章轉(zhuǎn)載:http://weahome.cn/article/gcddpp.html