怎么把AS3连接到MySQL数据库上
这篇文章主要介绍"怎么把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数据库上"的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注网站,小编会继续努力为大家带来更多实用的文章!