程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 數據庫知識 >> MYSQL數據庫 >> MYSQL入門知識 >> 如何將AS3連接到MySQL數據庫上

如何將AS3連接到MySQL數據庫上

編輯:MYSQL入門知識
 

最近由於制作任務編輯器,需要連接到數據庫中進行一些任務數據的操作,而我們存放任務數據的數據庫中MYSQL,

所以就找了一下AS3接連MYSQL數據庫的資料。

擴張閱讀:

ActionScript簡稱“AS”——動作腳本,是遵循 ECMAscript第四版 的 Adobe Flash Player 運行時環境的編程語言。

它在 Flash 內容和應用程序中實現交互性、數據處理以及其他功能。 ActionScript是Flash的腳本語言,與JavaScript相似,

ActionScript是一種編程語言,新出的AS3.0使用OOP(面對對象編程),增加更強的報錯能力,指定類型也更明確。

找到一個好東西:ActionScript 3 MySql Driver,用SVN的童鞋可以直接checkout:

http://assql.googlecode.com/svn/trunk/為了使用簡單,我做了一個簡單的包裝類(包含一個數據結構):

  1. package com.easily.ds
  2.  
  3. {
  4.  
  5. public class DataBaseData
  6.  
  7. {
  8.  
  9. public var host:String;
  10.  
  11. public var port:int;
  12.  
  13. public var username:String;
  14.  
  15. public var password:String;
  16.  
  17. public var database:String;
  18.  
  19. }
  20.  
  21. }
  1. package com.easily.util
  2.  
  3. {
  4.  
  5. import com.easily.ds.DataBaseData;
  6.  
  7. import com.maclema.mysql.Connection;
  8.  
  9. import com.maclema.mysql.Field;
  10.  
  11. import com.maclema.mysql.MySqlResponse;
  12.  
  13. import com.maclema.mysql.MySqlToken;
  14.  
  15. import com.maclema.mysql.ResultSet;
  16.  
  17. import com.maclema.mysql.Statement;
  18.  
  19.  
  20.  
  21. import flash.events.Event;
  22.  
  23. import flash.events.EventDispatcher;
  24.  
  25.  
  26.  
  27. import mx.rpc.AsyncResponder;
  28.  
  29.  
  30.  
  31. /**
  32.  
  33. * @author Easily
  34.  
  35. */
  36.  
  37. public class DataBase extends EventDispatcher
  38.  
  39. {
  40.  
  41. private var mDataBase:DataBaseData;
  42.  
  43. private var mConnection:Connection;
  44.  
  45.  
  46.  
  47. public function DataBase(database:DataBaseData)
  48.  
  49. {
  50.  
  51. mDataBase = database;
  52.  
  53. }
  54.  
  55.  
  56.  
  57. public function connect():void
  58.  
  59. {
  60.  
  61. mConnection = new Connection(mDataBase.host, mDataBase.port,
  62.  
  63. mDataBase.username, mDataBase.password, mDataBase.database);
  64.  
  65. mConnection.addEventListener(Event.CONNECT, onConnected);
  66.  
  67.  
  68.  
  69. mConnection.connect();
  70.  
  71.  
  72.  
  73. function onConnected(event:Event):void
  74.  
  75. {
  76.  
  77. mConnection.removeEventListener(Event.CONNECT, onConnected);
  78.  
  79.  
  80.  
  81. dispatchEvent(event);
  82.  
  83. }
  84.  
  85. }
  86.  
  87.  
  88.  
  89. public function disconnect():void
  90.  
  91. {
  92.  
  93. mConnection.disconnect();
  94.  
  95. }
  96.  
  97.  
  98.  
  99. public function select(sql:String, completeHandler:Function,
  100. errorHandler:Function = null):void
  101.  
  102. {
  103.  
  104. var st:Statement = mConnection.createStatement();
  105.  
  106. var token:MySqlToken = st.executeQuery(sql);
  107.  
  108. var responder:AsyncResponder = new AsyncResponder
  109. (resultHandler, faultHandler, token);
  110.  
  111. token.addResponder(responder);
  112.  
  113.  
  114.  
  115. function resultHandler(result:Object/*ResultSet*/, token:Object/*MySqlToken*/):void
  116.  
  117. {
  118.  
  119. var data:Array = [];
  120.  
  121.  
  122.  
  123. if (result is ResultSet)
  124.  
  125. {
  126.  
  127. var fieldList:Array = result.getColumns();
  128.  
  129. while (result.next())
  130.  
  131. {
  132.  
  133. var item:Object = {};
  134.  
  135. for each (var field:Field in fieldList)
  136.  
  137. {
  138.  
  139. item[field.getName()] = result.getString(field.getName());
  140.  
  141. }
  142.  
  143. data.push(item);
  144.  
  145. }
  146.  
  147. }
  148.  
  149.  
  150.  
  151. completeHandler(data);
  152.  
  153. }
  154.  
  155.  
  156.  
  157. function faultHandler(info:Object, token:Object):void
  158.  
  159. {
  160.  
  161. if (errorHandler == null) return;
  162.  
  163.  
  164.  
  165. errorHandler();
  166.  
  167. }
  168.  
  169. }
  170.  
  171.  
  172.  
  173. public function insert(sql:String, completeHandler:Function,
  174. errorHandler:
  175. Function = null):void
  176.  
  177. {
  178.  
  179. var st:Statement = mConnection.createStatement();
  180.  
  181. var token:MySqlToken = st.executeQuery(sql);
  182.  
  183. var responder:AsyncResponder = new AsyncResponder
  184. (resultHandler, faultHandler, token);
  185.  
  186. token.addResponder(responder);
  187.  
  188.  
  189.  
  190. function resultHandler(result:Object/*MySqlResponse*/,
  191. token:Object/*MySqlToken*/)
  192. :void
  193.  
  194. {
  195.  
  196. completeHandler(result.insertID);
  197.  
  198. }
  199.  
  200.  
  201.  
  202. function faultHandler(info:Object, token:Object):void
  203.  
  204. {
  205.  
  206. if (errorHandler == null) return;
  207.  
  208.  
  209.  
  210. errorHandler();
  211.  
  212. }
  213.  
  214. }
  215.  
  216.  
  217.  
  218. public function remove(sql:String, completeHandler:Function,
  219. errorHandler:Function = null)
  220. :void
  221.  
  222. {
  223.  
  224. var st:Statement = mConnection.createStatement();
  225.  
  226. var token:MySqlToken = st.executeQuery(sql);
  227.  
  228. var responder:AsyncResponder = new AsyncResponder
  229. (resultHandler, faultHandler, token);
  230.  
  231. token.addResponder(responder);
  232.  
  233.  
  234.  
  235. function resultHandler(result:Object/*MySqlResponse*/,
  236. token:Object/*MySqlToken*/):void
  237.  
  238. {
  239.  
  240. completeHandler();
  241.  
  242. }
  243.  
  244.  
  245.  
  246. function faultHandler(info:Object, token:Object):void
  247.  
  248. {
  249.  
  250. if (errorHandler == null) return;
  251.  
  252.  
  253.  
  254. errorHandler();
  255.  
  256. }
  257.  
  258. }
  259.  
  260. }
  261.  
  262. }

其中有3個方法,select,insert,remove,分別對應了SQL語句中的select,insert,delete

用法很簡單,先連接:

  1. var databaseData:DataBaseData = new DataBaseData();
  2.  
  3. databaseData.host = "127.0.0.1";
  4.  
  5. databaseData.database = "game";
  6.  
  7. databaseData.password = "123456";
  8.  
  9. databaseData.port = 3306;
  10.  
  11. databaseData.username = "root";
  12.  
  13.  
  14.  
  15. var dataBase:DataBase = new DataBase(databaseData);
  16.  
  17. dataBase.addEventListener(Event.CONNECT, onConnected);
  18.  
  19. dataBase.connect();
  20.  
  21.  
  22.  
  23. function onConnected(event:Event):void
  24.  
  25. {
  26.  
  27. dataBase.removeEventListener(Event.CONNECT, onConnected);
  28.  
  29.  
  30.  
  31. dispatchEvent(new Event(Event.COMPLETE));
  32.  
  33. }

如果不需要用了可以先斷開連接:

  1. dataBase.disconnect();

下面是select語句:

  1. var sql:String = "select id,name from `npc`";
  2.  
  3. dataBase.select(sql, endQuery);
  4.  
  5.  
  6.  
  7. function endQuery(data:Array):void
  8.  
  9. {
  10.  
  11. var npcList:Array= [];
  12.  
  13. for each (var item:Object in data)
  14.  
  15. {
  16.  
  17. var npc:Object = {id:item.id, name:item.name};
  18.  
  19.  
  20.  
  21. npcList.push(npc);
  22.  
  23. }
  24.  
  25. }

insert語句用法會返回一個insertID,也就是插入的那條數據的ID。

 
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved