怎么把AS3连接到MySQL数据库上

发布时间:2021-08-09 14:33:08 作者:chen
来源:亿速云 阅读:227

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

推荐阅读:
  1. AS3 ApplicationDomain
  2. AS3 属性绑定

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

as3 mysql 数据库

上一篇:linux中怎么利用scp实现自动远程备份

下一篇:怎么对MySQL服务器进行调优

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》