Unity3D研究院之Unity中連接本地或局域網MySQL數據庫(五十九)
時間: 2013-05-11 / 分類: 【Unity3D研究院之游戲開發】 / 瀏覽次數: 4106 瀏覽數 / 36條評論個評論 發表評論????????????????????
-
???????????????????????????????????????????????????
????????????????????????
? ? ? ? ?最近MOMO身心疲憊。。今天是周末在家無聊我還是決定來學習。不知道學什么,就學MySQL吧。本篇主要記錄從MySQL安裝到局域網內任意機器連接數據庫,也算是對自己學習的總結。今天我沒用Mac電腦,而是選擇Windows,沒有別有用心,而是想熟悉一下Windows下操作Unity。
unity連接。 官網上下載MySQL的安裝程序,這里有一篇詳細的安裝文章,http://www.jb51.net/article/23876.htm??為了讓中文完美的運行,配置MySQL的時候Character Set處設置成UTF-8,好像默認是不能顯示中文。配置完畢后就可以在本機中啟動MySQL,也可以在cmd命令行中start和stop 啟動與關閉MySQL。
?
?為了讓本機MySQL數據庫可以在局域網中任意機器都可以訪問,請看 下面這個網址。
http://dzb3688.blog.163.com/blog/static/105068522201292671444891/
文章有一點點講的不是很清楚,所以我在補充一下、
我用的是Navicat Pewmium查看數據庫,我覺得這個數據庫挺好的,因為我在Mac上也是用的這個數據庫 。(大家可以在網絡上下載它,Windows版本居然有漢化的)如下圖所示,點擊用戶,然后雙擊”root@%” 最后把主機的名字改成 “%”即可、
?
?
下面就是重點了,打開cmd 窗口cd到MySQL的路徑下,一定要cd到這個路徑下,不然mysql 會是無法識別的指令噢。
?
然后執行命令:
mysql?grant all privileges on *.* to root@”%” identified by ‘abc’ with grant option;?? flush privileges;
在執行命令:
mysql?flush privileges;
OK這樣就行了。
然后開始看看代碼怎么寫,為了方便數據庫的創建、增加、刪除、修改、查詢、我封裝了一個類。歡迎大家測試 啦啦啦啦。
SqlAccess.cs
004 | using System.Collections;?? |
005 | using MySql.Data.MySqlClient; |
008 | public class SqlAccess |
011 | ???? public static MySqlConnection dbConnection; |
015 | ???? static string host = "192.168.1.106" ;? |
016 | ???? static string id = "root" ; |
017 | ???? static string pwd = "1234" ; |
018 | ???? static string database = "xuanyusong" ; |
020 | ???? public SqlAccess() |
025 | ???? public static void OpenSql() |
030 | ???????????? string connectionString = string .Format( "Server = {0};port={4};Database = {1}; User ID = {2}; Password = {3};" ,host,database,id,pwd, "3306" ); |
031 | ???????????? dbConnection = new MySqlConnection(connectionString); |
032 | ???????????? dbConnection.Open(); |
033 | ???????? } catch (Exception e) |
035 | ???????????? throw new Exception( "服務器連接失敗,請重新檢查是否打開MySql服務。" + e.Message.ToString());? |
041 | ???? public DataSet CreateTable ( string name, string [] col, string [] colType) |
043 | ???????? if (col.Length != colType.Length) |
046 | ???????????? throw new Exception ( "columns.Length != colType.Length" ); |
050 | ???????? string query = "CREATE TABLE " + name + " (" + col[0] + " " + colType[0]; |
052 | ???????? for ( int i = 1; i < col.Length; ++i) { |
054 | ???????????? query += ", " + col[i] + " " + colType[i]; |
060 | ???????? return ? ExecuteQuery(query); |
063 | ???? public DataSet CreateTableAutoID ( string name, string [] col, string [] colType) |
065 | ???????? if (col.Length != colType.Length) |
068 | ???????????? throw new Exception ( "columns.Length != colType.Length" ); |
072 | ???????? string query = "CREATE TABLE " + name + " (" + col[0] + " " + colType[0] +? " NOT NULL AUTO_INCREMENT" ; |
074 | ???????? for ( int i = 1; i < col.Length; ++i) { |
076 | ???????????? query += ", " + col[i] + " " + colType[i]; |
080 | ???????? query += ", PRIMARY KEY (" + col[0] + ")" + ")" ; |
082 | ???????? Debug.Log(query); |
084 | ???????? return ? ExecuteQuery(query); |
088 | ???? public DataSet InsertInto ( string tableName, string [] values) |
091 | ???????? string query = "INSERT INTO " + tableName + " VALUES (" + "'" + values[0]+ "'" ; |
093 | ???????? for ( int i = 1; i < values.Length; ++i) { |
095 | ???????????? query += ", " + "'" +values[i]+ "'" ; |
101 | ???????? Debug.Log(query); |
102 | ???????? return ExecuteQuery (query); |
107 | ???? public DataSet InsertInto ( string tableName, string [] col, string [] values) |
110 | ???????? if (col.Length != values.Length) |
113 | ???????????? throw new Exception ( "columns.Length != colType.Length" ); |
117 | ???????? string query = "INSERT INTO " + tableName + " (" + col[0]; |
118 | ???????? for ( int i = 1; i < col.Length; ++i) |
121 | ???????????? query += ", " +col[i]; |
125 | ???????? query += ") VALUES (" + "'" + values[0]+ "'" ; |
126 | ???????? for ( int i = 1; i < values.Length; ++i) |
129 | ???????????? query += ", " + "'" +values[i]+ "'" ; |
135 | ???????? Debug.Log(query); |
136 | ???????? return ExecuteQuery (query); |
140 | ???? public DataSet SelectWhere ( string tableName, string [] items, string [] col, string [] operation, string [] values) |
143 | ???????? if (col.Length != operation.Length || operation.Length != values.Length) { |
145 | ???????????? throw new Exception ( "col.Length != operation.Length != values.Length" ); |
149 | ???????? string query = "SELECT " + items[0]; |
151 | ???????? for ( int i = 1; i < items.Length; ++i) { |
153 | ???????????? query += ", " + items[i]; |
157 | ???????? query += " FROM " + tableName + " WHERE " + col[0] + operation[0] + "'" + values[0] + "' " ; |
159 | ???????? for ( int i = 1; i < col.Length; ++i) { |
161 | ???????????? query += " AND " + col[i] + operation[i] + "'" + values[0] + "' " ; |
165 | ???????? return ExecuteQuery (query); |
169 | ???? public DataSet UpdateInto ( string tableName, string []cols, string []colsvalues, string selectkey, string selectvalue) |
172 | ???????? string query = "UPDATE " +tableName+ " SET " +cols[0]+ " = " +colsvalues[0]; |
174 | ???????? for ( int i = 1; i < colsvalues.Length; ++i) { |
176 | ????????????? query += ", " +cols[i]+ " =" + colsvalues[i]; |
179 | ????????? query += " WHERE " +selectkey+ " = " +selectvalue+ " " ; |
181 | ???????? return ExecuteQuery (query); |
184 | ???? public DataSet Delete( string tableName, string []cols, string []colsvalues) |
186 | ???????? string query = "DELETE FROM " +tableName + " WHERE " +cols[0] + " = " + colsvalues[0]; |
188 | ???????? for ( int i = 1; i < colsvalues.Length; ++i) |
191 | ???????????????? query += " or " +cols[i]+ " = " + colsvalues[i]; |
193 | ???????? Debug.Log(query); |
194 | ???????? return ExecuteQuery (query); |
197 | ???? public ? void Close() |
200 | ???????? if (dbConnection != null ) |
202 | ???????????? dbConnection.Close(); |
203 | ???????????? dbConnection.Dispose(); |
204 | ???????????? dbConnection = null ; |
209 | ???? public static DataSet ExecuteQuery( string sqlString)? |
211 | ???????? if (dbConnection.State==ConnectionState.Open) |
213 | ???????????? DataSet ds = new DataSet();? |
217 | ???????????????? MySqlDataAdapter da = new MySqlDataAdapter(sqlString, dbConnection); |
218 | ???????????????? da.Fill(ds); |
221 | ???????????? catch (Exception ee)? |
223 | ???????????????? throw new Exception( "SQL:" + sqlString + "/n" + ee.Message.ToString());? |
228 | ???????????? return ds; |
?
然后在來看看調用,把如下腳本綁定在任意對象即可,調用包括、創建表、插入信息、查找信息、刪除信息、更新信息。代碼比較簡單我就不一一注釋了,這里我用try catch如果有錯誤信息將打印在屏幕中。 創建表包括是否遞增ID,所以有兩種創建表的方式。如果你的數據庫是提前預制的話可以這樣來讀取數據庫。
04 | using System.Collections;?? |
05 | using MySql.Data.MySqlClient; |
08 | public class NewBehaviourScript : MonoBehaviour { |
10 | ???? string Error = null ; |
16 | ???????? SqlAccess sql = new ? SqlAccess(); |
18 | ????????? sql.CreateTableAutoID( "momo" , new string []{ "id" , "name" , "qq" , "email" , "blog" }, new string []{ "int" , "text" , "text" , "text" , "text" }); |
20 | ???????? sql.InsertInto( "momo" , new string []{ "name" , "qq" , "email" , "blog" }, new string []{ "xuanyusong" , "289187120" , "xuanyusong@gmail.com" , "xuanyusong.com" }); |
21 | ???????? sql.InsertInto( "momo" , new string []{ "name" , "qq" , "email" , "blog" }, new string []{ "ruoruo" , "34546546" , "ruoruo@gmail.com" , "xuanyusong.com" }); |
23 | ???????? DataSet ds? = sql.SelectWhere( "momo" , new string []{ "name" , "qq" }, new string []{ "id" }, new string []{ "=" }, new string []{ "1" }); |
27 | ???????????? DataTable table = ds.Tables[0]; |
29 | ???????????? foreach (DataRow row in table.Rows) |
31 | ??????????????? foreach (DataColumn column in table.Columns) |
33 | ???????????????????? Debug.Log(row[column]); |
38 | ????????? sql.UpdateInto( "momo" , new string []{ "name" , "qq" }, new string []{ "'ruoruo'" , "'11111111'" }, "email" , "'xuanyusong@gmail.com'" ? ); |
40 | ????????? sql.Delete( "momo" , new string []{ "id" , "email" }, new string []{ "1" , "'000@gmail.com'" }? ); |
42 | ???????? } catch (Exception e) |
44 | ???????????? Error = e.Message; |
53 | ???????? if (Error != null ) |
55 | ???????????? GUILayout.Label(Error); |
然后是用到的dll 一個都不能少,不然會出現各種問題。最后的下載地址我會給出,并且包含這些文件。
?
?
?
為了測試局域網的連接, 我還編譯到了Android手機上,在Android上訪問這個數據庫,也是沒問題的。當然手機和電腦用的是同一個wifi網絡。 目前這個項目在 Windows 和 ?Android上都可以很好的運行,我感覺在Mac上和iPhone上應該也木有問題,歡迎大家測試,如果發現在別的平臺下有問題請告訴我,我會進一步研究的。 歡迎大家留言,一起學習啦啦啦啦 嘿嘿嘿~~。。
?
?
?