重庆分公司,新征程启航
为企业提供网站建设、域名注册、服务器等服务
这篇文章主要介绍“怎么把AS3连接到MySQL数据库上”,在日常操作中,相信很多人在怎么把AS3连接到MySQL数据库上问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”怎么把AS3连接到MySQL数据库上”的疑惑有所帮助!接下来,请跟着小编一起来学习吧!
成都创新互联公司成立于2013年,我们提供高端重庆网站建设公司、成都网站制作、网站设计、网站定制、成都营销网站建设、成都小程序开发、微信公众号开发、seo优化排名服务,提供专业营销思路、内容策划、视觉设计、程序开发来完成项目落地,为成都食品包装袋企业提供源源不断的流量和订单咨询。
找到一个好东西: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数据库上”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注创新互联网站,小编会继续努力为大家带来更多实用的文章!