您好,登錄后才能下訂單哦!
這篇文章主要介紹“怎么把AS3連接到MySQL數據庫上”,在日常操作中,相信很多人在怎么把AS3連接到MySQL數據庫上問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”怎么把AS3連接到MySQL數據庫上”的疑惑有所幫助!接下來,請跟著小編一起來學習吧!
找到一個好東西:ActionScript3MySqlDriver,用SVN的童鞋可以直接checkout:
http://assql.googlecode.com/svn/trunk/為了使用簡單,我做了一個簡單的包裝類(包含一個數據結構):
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,分別對應了SQL語句中的select,insert,delete
如何將AS3連接到MySQL數據庫上
用法很簡單,先連接:
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,也就是插入的那條數據的ID。
到此,關于“怎么把AS3連接到MySQL數據庫上”的學習就結束了,希望能夠解決大家的疑惑。理論與實踐的搭配能更好的幫助大家學習,快去試試吧!若想繼續學習更多相關知識,請繼續關注億速云網站,小編會繼續努力為大家帶來更多實用的文章!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。