Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to connect AS3 to MySQL database

2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly introduces "how to connect AS3 to MySQL database". In daily operation, I believe many people have doubts about how to connect AS3 to MySQL database. Xiaobian consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts of "how to connect AS3 to MySQL database". Next, please follow the editor to study!

Find a good thing: ActionScript3MySqlDriver, you can checkout directly with SVN's children's shoes:

To make http://assql.googlecode.com/svn/trunk/ easy to use, I made a simple wrapper class (containing a data structure):

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 ();}

There are three methods, select,insert,remove, which correspond to select,insert,delete in the SQL statement.

How to connect AS3 to a MySQL database

The usage is simple: connect first:

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);}

If you don't need it, you can disconnect it first:

DataBase.disconnect ()

Here is the select statement:

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);}}

The usage of the insert statement returns an insertID, that is, the ID of the data being inserted.

At this point, the study on "how to connect AS3 to MySQL database" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report