首页 >Unity3D频道 >【Unity3D研究院之游戏开发】 > Unity3D研究院之Unity中连接本地或局域网MySQL数据库(五十九)
2013
05-11

Unity3D研究院之Unity中连接本地或局域网MySQL数据库(五十九)

         最近MOMO身心疲惫。。今天是周末在家无聊我还是决定来学习。不知道学什么,就学MySQL吧。本篇主要记录从MySQL安装到局域网内任意机器连接数据库,也算是对自己学习的总结。今天我没用Mac电脑,而是选择Windows,没有别有用心,而是想熟悉一下Windows下操作Unity。

官网上下载MySQL的安装程序,这里有一篇详细的安装文章,http://www.jb51.net/article/23876.htm  为了让中文完美的运行,配置MySQL的时候Character Set处设置成UTF-8,好像默认是不能显示中文。配置完毕后就可以在本机中启动MySQL,也可以在cmd命令行中start和stop 启动与关闭MySQL。

net start mysql 
net 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

using UnityEngine;  
using System;  
using System.Data;  
using System.Collections;   
using MySql.Data.MySqlClient;
using MySql.Data;
using System.IO;
public class SqlAccess 
{

    public static MySqlConnection dbConnection;
	//如果只是在本地的话,写localhost就可以。
   // static string host = "localhost";  
	//如果是局域网,那么写上本机的局域网IP
	static string host = "192.168.1.106";  
    static string id = "root";
    static string pwd = "1234";
    static string database = "xuanyusong"; 

	public SqlAccess()
	{
		OpenSql();
	}

	public static void OpenSql()
	{

		try
		{
			string connectionString = string.Format("Server = {0};port={4};Database = {1}; User ID = {2}; Password = {3};",host,database,id,pwd,"3306");
			dbConnection = new MySqlConnection(connectionString);
			dbConnection.Open(); 
		}catch (Exception e)
		{
	        throw new Exception("服务器连接失败,请重新检查是否打开MySql服务。" + e.Message.ToString());  

		}

	}

	public DataSet CreateTable (string name, string[] col, string[] colType)
    {
        if (col.Length != colType.Length) 
		{

            throw new Exception ("columns.Length != colType.Length");

        }

        string query = "CREATE TABLE " + name + " (" + col[0] + " " + colType[0];

        for (int i = 1; i < col.Length; ++i) {

            query += ", " + col[i] + " " + colType[i];

        }

        query += ")";

        return  ExecuteQuery(query);
    }

	public DataSet CreateTableAutoID (string name, string[] col, string[] colType)
    {
        if (col.Length != colType.Length) 
		{

            throw new Exception ("columns.Length != colType.Length");

        }

        string query = "CREATE TABLE " + name + " (" + col[0] + " " + colType[0] +  " NOT NULL AUTO_INCREMENT";

        for (int i = 1; i < col.Length; ++i) {

            query += ", " + col[i] + " " + colType[i];

        }

        query += ", PRIMARY KEY ("+ col[0] +")" + ")";

		Debug.Log(query);

        return  ExecuteQuery(query);
    }

	//插入一条数据,包括所有,不适用自动累加ID。
	public DataSet InsertInto (string tableName, string[] values)
    {

        string query = "INSERT INTO " + tableName + " VALUES (" + "'"+ values[0]+ "'";

        for (int i = 1; i < values.Length; ++i) {

            query += ", " + "'"+values[i]+ "'";

        }

        query += ")";

		Debug.Log(query);
        return ExecuteQuery (query);

    }

	//插入部分ID
	public DataSet InsertInto (string tableName, string[] col,string[] values)
    {

		if (col.Length != values.Length) 
		{

            throw new Exception ("columns.Length != colType.Length");

        }

        string query = "INSERT INTO " + tableName + " (" + col[0];
	    for (int i = 1; i < col.Length; ++i) 
		{

            query += ", "+col[i];

        }

		query += ") VALUES (" + "'"+ values[0]+ "'";
        for (int i = 1; i < values.Length; ++i) 
		{

            query += ", " + "'"+values[i]+ "'";

        }

        query += ")";

		Debug.Log(query);
        return ExecuteQuery (query);

    }

   	public DataSet SelectWhere (string tableName, string[] items, string[] col, string[] operation, string[] values)
    {

        if (col.Length != operation.Length || operation.Length != values.Length) {

            throw new Exception ("col.Length != operation.Length != values.Length");

        }

        string query = "SELECT " + items[0];

        for (int i = 1; i < items.Length; ++i) {

            query += ", " + items[i];

        }

        query += " FROM " + tableName + " WHERE " + col[0] + operation[0] + "'" + values[0] + "' ";

        for (int i = 1; i < col.Length; ++i) {

            query += " AND " + col[i] + operation[i] + "'" + values[0] + "' ";

        }

        return ExecuteQuery (query);

    } 

	public DataSet UpdateInto (string tableName, string []cols,string []colsvalues,string selectkey,string selectvalue)
	{

		string query = "UPDATE "+tableName+" SET "+cols[0]+" = "+colsvalues[0];

		for (int i = 1; i < colsvalues.Length; ++i) {

		 	 query += ", " +cols[i]+" ="+ colsvalues[i];
		}

		 query += " WHERE "+selectkey+" = "+selectvalue+" ";

		return ExecuteQuery (query);
	}

	public DataSet Delete(string tableName,string []cols,string []colsvalues)
	{
		string query = "DELETE FROM "+tableName + " WHERE " +cols[0] +" = " + colsvalues[0];

		for (int i = 1; i < colsvalues.Length; ++i) 
		{

		 	    query += " or " +cols[i]+" = "+ colsvalues[i];
		}
		Debug.Log(query);
		return ExecuteQuery (query);
	}

	public  void Close()
	{

		if(dbConnection != null)
		{
			dbConnection.Close();
			dbConnection.Dispose();
			dbConnection = null;
		}

	}

    public static DataSet ExecuteQuery(string sqlString)  
    {  
		if(dbConnection.State==ConnectionState.Open)
		{
     		DataSet ds = new DataSet();  
      		try  
	    	{  

				MySqlDataAdapter da = new MySqlDataAdapter(sqlString, dbConnection); 
				da.Fill(ds);

	    	}  
		    catch (Exception ee)  
		    {
		        throw new Exception("SQL:" + sqlString + "/n" + ee.Message.ToString());  
		    }
			finally
			{
			}
			return ds;
		}
	    return null;
	}

}

 

然后在来看看调用,把如下脚本绑定在任意对象即可,调用包括、创建表、插入信息、查找信息、删除信息、更新信息。代码比较简单我就不一一注释了,这里我用try catch如果有错误信息将打印在屏幕中。 创建表包括是否递增ID,所以有两种创建表的方式。如果你的数据库是提前预制的话可以这样来读取数据库。

using UnityEngine;  
using System;  
using System.Data;  
using System.Collections;   
using MySql.Data.MySqlClient;
using MySql.Data;
using System.IO;
public class NewBehaviourScript : MonoBehaviour {

	string Error = null;
	void Start () 
	{
		try
		{

		SqlAccess sql = new  SqlAccess();

		 sql.CreateTableAutoID("momo",new string[]{"id","name","qq","email","blog"}, new string[]{"int","text","text","text","text"});
		//sql.CreateTable("momo",new string[]{"name","qq","email","blog"}, new string[]{"text","text","text","text"});
		sql.InsertInto("momo",new string[]{"name","qq","email","blog"},new string[]{"xuanyusong","289187120","xuanyusong@gmail.com","xuanyusong.com"});
		sql.InsertInto("momo",new string[]{"name","qq","email","blog"},new string[]{"ruoruo","34546546","ruoruo@gmail.com","xuanyusong.com"});

		DataSet ds  = sql.SelectWhere("momo",new string[]{"name","qq"},new string []{"id"},new string []{"="},new string []{"1"});
		if(ds != null)
		{

			DataTable table = ds.Tables[0];

			foreach (DataRow row in table.Rows)
			{
			   foreach (DataColumn column in table.Columns)
			   {
					Debug.Log(row[column]);
			   }
			 }
		}	

		 sql.UpdateInto("momo",new string[]{"name","qq"},new string[]{"'ruoruo'","'11111111'"}, "email", "'xuanyusong@gmail.com'"  );

		 sql.Delete("momo",new string[]{"id","email"}, new string[]{"1","'000@gmail.com'"}  );
		 sql.Close();
		}catch(Exception e)
		{
			Error = e.Message;
		}

	}

	// Update is called once per frame
	void OnGUI () 
	{

		if(Error != null)
		{
			GUILayout.Label(Error);
		}

	}
}

然后是用到的dll 一个都不能少,不然会出现各种问题。最后的下载地址我会给出,并且包含这些文件。

 

未命名

 

 

为了测试局域网的连接, 我还编译到了Android手机上,在Android上访问这个数据库,也是没问题的。当然手机和电脑用的是同一个wifi网络。 目前这个项目在 Windows 和  Android上都可以很好的运行,我感觉在Mac上和iPhone上应该也木有问题,欢迎大家测试,如果发现在别的平台下有问题请告诉我,我会进一步研究的。 欢迎大家留言,一起学习啦啦啦啦 嘿嘿嘿~~。。

 

未命名

 

 

下载地址:http://vdisk.weibo.com/s/B5ac9

最后编辑:
作者:雨松MOMO
专注移动互联网,Unity3D游戏开发
捐 赠写博客不易,如果您想请我喝一杯星巴克的话?就进来看吧!

《Unity3D研究院之Unity中连接本地或局域网MySQL数据库(五十九)》有 99 条评论

  1. qiaoxide说:

    实际游戏中这样做,卡死你

  2. paulpn说:

    导入System.Drawing.dll这个文件就会报错错误1ReflectionTypeLoadException: The classes in the module cannot be loaded.System.Reflection.Assembly.GetTypes () (at /Users/builduser/buildslave/mono-runtime-and-classlibs/build/mcs/class/corlib/System.Reflection/Assembly.cs:371)UnityEditor.Events.InterceptedEventsPreview.GetEventsInfo (UnityEngine.GameObject gameObject) (at C:/buildslave/unity/build/Extensions/guisystem/UnityEditor.UI/UI/InterceptedEventsPreview.cs:162)UnityEditor.Events.InterceptedEventsPreview.Initialize (UnityEngine.Object[] targets) (at C:/buildslave/unity/build/Extensions/guisystem/UnityEditor.UI/UI/InterceptedEventsPreview.cs:61)UnityEditor.InspectorWindow.GetPreviewsForType (UnityEditor.Editor editor) (at C:/buildslave/unity/build/Editor/Mono/Inspector/InspectorWindow.cs:304)UnityEditor.InspectorWindow.CreatePreviewables () (at C:/buildslave/unity/build/Editor/Mono/Inspector/InspectorWindow.cs:272)UnityEditor.InspectorWindow.OnGUI () (at C:/buildslave/unity/build/Editor/Mono/Inspector/InspectorWindow.cs:334)System.Reflection.MonoMethod.Invoke (System.Object obj, BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) (at /Users/builduser/buildslave/mono-runtime-and-classlibs/build/mcs/class/corlib/System.Reflection/MonoMethod.cs:222)错误2ArgumentException: Getting control 0’s position in a group with only 0 controls when doing RepaintAbortingUnityEngine.GUILayoutGroup.GetNext () (at C:/buildslave/unity/build/artifacts/generated/common/runtime/GUILayoutUtilityBindings.gen.cs:511)UnityEngine.GUILayoutUtility.BeginLayoutGroup (UnityEngine.GUIStyle style, UnityEngine.GUILayoutOption[] options, System.Type LayoutType) (at C:/buildslave/unity/build/artifacts/generated/common/runtime/GUILayoutUtilityBindings.gen.cs:208)UnityEditor.EditorGUILayout.BeginVerticalScrollView (Vector2 scrollPosition, Boolean alwaysShowVertical, UnityEngine.GUIStyle verticalScrollbar, UnityEngine.GUIStyle background, UnityEngine.GUILayoutOption[] options) (at C:/buildslave/unity/build/Editor/Mono/EditorGUI.cs:6707)UnityEditor.EditorGUILayout.BeginVerticalScrollView (Vector2 scrollPosition, UnityEngine.GUILayoutOption[] options) (at C:/buildslave/unity/build/Editor/Mono/EditorGUI.cs:6701)UnityEditor.InspectorWindow.OnGUI () (at C:/buildslave/unity/build/Editor/Mono/Inspector/InspectorWindow.cs:341)System.Reflection.MonoMethod.Invoke (System.Object obj, BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) (at /Users/builduser/buildslave/mono-runtime-and-classlibs/build/mcs/class/corlib/System.Reflection/MonoMethod.cs:222)

  3. 請問一下,我的資料庫想要連在虛擬網路上的話,應該怎麼改?

  4. youke说:

    怎么备份数据库!

  5. 都过去这么久了,我现在在项目里也碰到这个问题,项目里用到了Vuforia的插件就有这问题,不知道你还记得怎么解决的吗,都不知道能不能看的见啊

  6. aiyusong说:

    用MySqlDataReader改了一下 分享给大家 using UnityEngine; using System; using System.Data; using System.Collections; using MySql.Data.MySqlClient;using MySql.Data;using System.IO;public class mySqlAccess { public static MySqlConnection dbConnection;private static MySqlCommand sqlCommand;private static MySqlDataReader sqlReader;//如果只是在本地的话,写localhost就可以。 // static string host = “localhost”; //如果是局域网,那么写上本机的局域网IPstatic string host = “localhost”; static string id = “root”; static string pwd = “123456”;static string database = “songsong”;public mySqlAccess(){OpenSql();}public static void OpenSql(){try{string connectionString = string.Format(“Server = {0};port={4};Database = {1}; User ID = {2}; Password = {3};”,host,database,id,pwd,”3306″);dbConnection = new MySqlConnection(connectionString);dbConnection.Open();}catch (Exception e){ throw new Exception(“服务器连接失败,请重新检查是否打开MySql服务。” e.Message.ToString());}}public MySqlDataReader CreateTable (string name, string[] col, string[] colType) { if (col.Length != colType.Length) { throw new Exception (“columns.Length != colType.Length”); } string query = “CREATE TABLE ” name ” (” col[0] ” ” colType[0]; for (int i = 1; i < col.Length; i) { query = “, ” col Unity3D研究院之Unity中连接本地或局域网MySQL数据库(五十九) - 雨松MOMO程序研究院 - 1 ” ” colType Unity3D研究院之Unity中连接本地或局域网MySQL数据库(五十九) - 雨松MOMO程序研究院 - 2 ; } query = “)”; return ExecuteQuery(query); }public MySqlDataReader CreateTableAutoID (string name, string[] col, string[] colType) { if (col.Length != colType.Length) { throw new Exception (“columns.Length != colType.Length”); } string query = “CREATE TABLE ” name ” (” col[0] ” ” colType[0] ” NOT NULL AUTO_INCREMENT”; for (int i = 1; i < col.Length; i) { query = “, ” col Unity3D研究院之Unity中连接本地或局域网MySQL数据库(五十九) - 雨松MOMO程序研究院 - 3 ” ” colType Unity3D研究院之Unity中连接本地或局域网MySQL数据库(五十九) - 雨松MOMO程序研究院 - 4 ; } query = “, PRIMARY KEY (” col[0] “)” “)”;Debug.Log(query); return ExecuteQuery(query); }//插入一条数据,包括所有,不适用自动累加ID。public MySqlDataReader InsertInto (string tableName, string[] values) { string query = “INSERT INTO ” tableName ” VALUES (” “‘” values[0] “‘”; for (int i = 1; i < values.Length; i) { query = “, ” “‘” values Unity3D研究院之Unity中连接本地或局域网MySQL数据库(五十九) - 雨松MOMO程序研究院 - 5 “‘”; } query = “)”;Debug.Log(query); return ExecuteQuery (query); }//插入部分IDpublic MySqlDataReader InsertInto (string tableName, string[] col,string[] values) {if (col.Length != values.Length) { throw new Exception (“columns.Length != colType.Length”); } string query = “INSERT INTO ” tableName ” (” col[0]; for (int i = 1; i < col.Length; i) { query = “, ” col Unity3D研究院之Unity中连接本地或局域网MySQL数据库(五十九) - 雨松MOMO程序研究院 - 6 ; }query = “) VALUES (” “‘” values[0] “‘”; for (int i = 1; i < values.Length; i) { query = “, ” “‘” values Unity3D研究院之Unity中连接本地或局域网MySQL数据库(五十九) - 雨松MOMO程序研究院 - 7 “‘”; } query = “)”;Debug.Log(query); return ExecuteQuery (query); }public MySqlDataReader SelectWhere (string tableName, string[] items, string[] col, string[] operation, string[] values) { if (col.Length != operation.Length || operation.Length != values.Length) { throw new Exception (“col.Length != operation.Length != values.Length”); } string query = “SELECT ” items[0]; for (int i = 1; i < items.Length; i) { query = “, ” items Unity3D研究院之Unity中连接本地或局域网MySQL数据库(五十九) - 雨松MOMO程序研究院 - 8 ; } query = ” FROM ” tableName ” WHERE ” col[0] operation[0] “‘” values[0] “‘ “; for (int i = 1; i < col.Length; i) { query = ” AND ” col Unity3D研究院之Unity中连接本地或局域网MySQL数据库(五十九) - 雨松MOMO程序研究院 - 9 operation Unity3D研究院之Unity中连接本地或局域网MySQL数据库(五十九) - 雨松MOMO程序研究院 - 10 “‘” values[0] “‘ “; } return ExecuteQuery (query); } public MySqlDataReader UpdateInto (string tableName, string []cols,string []colsvalues,string selectkey,string selectvalue){string query = “UPDATE ” tableName ” SET ” cols[0] ” = ” colsvalues[0];for (int i = 1; i < colsvalues.Length; i) { query = “, ” cols Unity3D研究院之Unity中连接本地或局域网MySQL数据库(五十九) - 雨松MOMO程序研究院 - 11 ” =” colsvalues Unity3D研究院之Unity中连接本地或局域网MySQL数据库(五十九) - 雨松MOMO程序研究院 - 12 ;} query = ” WHERE ” selectkey ” = ” selectvalue ” “;return ExecuteQuery (query);}public MySqlDataReader Delete(string tableName,string []cols,string []colsvalues){string query = “DELETE FROM ” tableName ” WHERE ” cols[0] ” = ” colsvalues[0];for (int i = 1; i < colsvalues.Length; i) { query = ” or ” cols Unity3D研究院之Unity中连接本地或局域网MySQL数据库(五十九) - 雨松MOMO程序研究院 - 13 ” = ” colsvalues Unity3D研究院之Unity中连接本地或局域网MySQL数据库(五十九) - 雨松MOMO程序研究院 - 14 ;}Debug.Log(query);return ExecuteQuery (query);}public void Close(){if(sqlCommand != null){sqlCommand.Dispose();sqlCommand.Cancel();sqlCommand = null;}if(sqlReader != null){sqlReader.Close();sqlReader.Dispose();sqlReader = null;}if(dbConnection != null){dbConnection.Close();dbConnection.Dispose();dbConnection = null;}Debug.Log (“Disconnected mysql”);} public MySqlDataReader ExecuteQuery(string sqlString) { if(dbConnection.State==ConnectionState.Open){sqlCommand = dbConnection.CreateCommand();sqlCommand.CommandText = sqlString;sqlReader = sqlCommand.ExecuteReader();Debug.Log(“SQL:” sqlString );return sqlReader;} return null;}}

  7. aiyusong说:

    用MySqlDataReader改了一下 分享给大家 using UnityEngine; using System; using System.Data; using System.Collections; using MySql.Data.MySqlClient;using MySql.Data;using System.IO;public class mySqlAccess { public static MySqlConnection dbConnection; private static MySqlCommand sqlCommand; private static MySqlDataReader sqlReader; //如果只是在本地的话,写localhost就可以。 // static string host = “localhost”; //如果是局域网,那么写上本机的局域网IP static string host = “localhost”; static string id = “root”; static string pwd = “123456”; static string database = “songsong”; public mySqlAccess() { OpenSql(); } public static void OpenSql() { try { string connectionString = string.Format(“Server = {0};port={4};Database = {1}; User ID = {2}; Password = {3};”,host,database,id,pwd,”3306″); dbConnection = new MySqlConnection(connectionString); dbConnection.Open(); }catch (Exception e) { throw new Exception(“服务器连接失败,请重新检查是否打开MySql服务。” + e.Message.ToString()); } } public MySqlDataReader CreateTable (string name, string[] col, string[] colType) { if (col.Length != colType.Length) { throw new Exception (“columns.Length != colType.Length”); } string query = “CREATE TABLE ” + name + ” (” + col[0] + ” ” + colType[0]; for (int i = 1; i < col.Length; ++i) { query += “, ” + col Unity3D研究院之Unity中连接本地或局域网MySQL数据库(五十九) - 雨松MOMO程序研究院 - 1 + ” ” + colType Unity3D研究院之Unity中连接本地或局域网MySQL数据库(五十九) - 雨松MOMO程序研究院 - 2 ; } query += “)”; return ExecuteQuery(query); } public MySqlDataReader CreateTableAutoID (string name, string[] col, string[] colType) { if (col.Length != colType.Length) { throw new Exception (“columns.Length != colType.Length”); } string query = “CREATE TABLE ” + name + ” (” + col[0] + ” ” + colType[0] + ” NOT NULL AUTO_INCREMENT”; for (int i = 1; i < col.Length; ++i) { query += “, ” + col Unity3D研究院之Unity中连接本地或局域网MySQL数据库(五十九) - 雨松MOMO程序研究院 - 3 + ” ” + colType Unity3D研究院之Unity中连接本地或局域网MySQL数据库(五十九) - 雨松MOMO程序研究院 - 4 ; } query += “, PRIMARY KEY (“+ col[0] +”)” + “)”; Debug.Log(query); return ExecuteQuery(query); } //插入一条数据,包括所有,不适用自动累加ID。 public MySqlDataReader InsertInto (string tableName, string[] values) { string query = “INSERT INTO ” + tableName + ” VALUES (” + “‘”+ values[0]+ “‘”; for (int i = 1; i < values.Length; ++i) { query += “, ” + “‘”+values Unity3D研究院之Unity中连接本地或局域网MySQL数据库(五十九) - 雨松MOMO程序研究院 - 5 + “‘”; } query += “)”; Debug.Log(query); return ExecuteQuery (query); } //插入部分ID public MySqlDataReader InsertInto (string tableName, string[] col,string[] values) { if (col.Length != values.Length) { throw new Exception (“columns.Length != colType.Length”); } string query = “INSERT INTO ” + tableName + ” (” + col[0]; for (int i = 1; i < col.Length; ++i) { query += “, “+col Unity3D研究院之Unity中连接本地或局域网MySQL数据库(五十九) - 雨松MOMO程序研究院 - 6 ; } query += “) VALUES (” + “‘”+ values[0]+ “‘”; for (int i = 1; i < values.Length; ++i) { query += “, ” + “‘”+values Unity3D研究院之Unity中连接本地或局域网MySQL数据库(五十九) - 雨松MOMO程序研究院 - 7 + “‘”; } query += “)”; Debug.Log(query); return ExecuteQuery (query); } public MySqlDataReader SelectWhere (string tableName, string[] items, string[] col, string[] operation, string[] values) { if (col.Length != operation.Length || operation.Length != values.Length) { throw new Exception (“col.Length != operation.Length != values.Length”); } string query = “SELECT ” + items[0]; for (int i = 1; i < items.Length; ++i) { query += “, ” + items Unity3D研究院之Unity中连接本地或局域网MySQL数据库(五十九) - 雨松MOMO程序研究院 - 8 ; } query += ” FROM ” + tableName + ” WHERE ” + col[0] + operation[0] + “‘” + values[0] + “‘ “; for (int i = 1; i < col.Length; ++i) { query += ” AND ” + col Unity3D研究院之Unity中连接本地或局域网MySQL数据库(五十九) - 雨松MOMO程序研究院 - 9 + operation Unity3D研究院之Unity中连接本地或局域网MySQL数据库(五十九) - 雨松MOMO程序研究院 - 10 + “‘” + values[0] + “‘ “; } return ExecuteQuery (query); } public MySqlDataReader UpdateInto (string tableName, string []cols,string []colsvalues,string selectkey,string selectvalue) { string query = “UPDATE “+tableName+” SET “+cols[0]+” = “+colsvalues[0]; for (int i = 1; i < colsvalues.Length; ++i) { query += “, ” +cols Unity3D研究院之Unity中连接本地或局域网MySQL数据库(五十九) - 雨松MOMO程序研究院 - 11 +” =”+ colsvalues Unity3D研究院之Unity中连接本地或局域网MySQL数据库(五十九) - 雨松MOMO程序研究院 - 12 ; } query += ” WHERE “+selectkey+” = “+selectvalue+” “; return ExecuteQuery (query); } public MySqlDataReader Delete(string tableName,string []cols,string []colsvalues) { string query = “DELETE FROM “+tableName + ” WHERE ” +cols[0] +” = ” + colsvalues[0]; for (int i = 1; i < colsvalues.Length; ++i) { query += ” or ” +cols Unity3D研究院之Unity中连接本地或局域网MySQL数据库(五十九) - 雨松MOMO程序研究院 - 13 +” = “+ colsvalues Unity3D研究院之Unity中连接本地或局域网MySQL数据库(五十九) - 雨松MOMO程序研究院 - 14 ; } Debug.Log(query); return ExecuteQuery (query); } public void Close() { if(sqlCommand != null) { sqlCommand.Dispose(); sqlCommand.Cancel(); sqlCommand = null; } if(sqlReader != null) { sqlReader.Close(); sqlReader.Dispose(); sqlReader = null; } if(dbConnection != null) { dbConnection.Close(); dbConnection.Dispose(); dbConnection = null; } Debug.Log (“Disconnected mysql”); } public MySqlDataReader ExecuteQuery(string sqlString) { if(dbConnection.State==ConnectionState.Open) { sqlCommand = dbConnection.CreateCommand(); sqlCommand.CommandText = sqlString; sqlReader = sqlCommand.ExecuteReader(); Debug.Log(“SQL:” + sqlString ); return sqlReader; } return null; }}

  8. 星落说:

    兄弟,你这问题解决了么?我也遇到了

  9. 感谢说:

    能不能用MySqlDataReader去读取?和DataSet相比哪个更好呢?

  10. 钟小洪说:

    android os 这样连上了。void Start() {StartCoroutine(openSqlConnection1());} IEnumerator openSqlConnection1() { // public static void openSqlConnection1() {print (“Open”); /*string connectionString = “Server = http://www.waixing.com;” “Database = vdate;” “User ID = test;” “Password= 123456;” “Pooling=false”; */ string connectionString = string.Format(“Server = {0}; Database = {1}; User ID = {2}; Password = {3};”,host,database,id,pwd); //dbConnection = new MySqlConnection(connectionString); dbConnection = new MySqlConnection(connectionString); dbConnection.Open(); //openSqlConnection(connectionString); yield return new WaitForSeconds(3); Debug.Log(“Connected to database.”); }

  11. 钟小洪说:

    android os 这样连上了。void Start() { StartCoroutine(openSqlConnection1()); } IEnumerator openSqlConnection1() { // public static void openSqlConnection1() { print (“Open”); /* string connectionString = “Server = http://www.waixing.com;” + “Database = vdate;” + “User ID = test;” + “Password= 123456;” + “Pooling=false”; */ string connectionString = string.Format(“Server = {0}; Database = {1}; User ID = {2}; Password = {3};”,host,database,id,pwd); //dbConnection = new MySqlConnection(connectionString); dbConnection = new MySqlConnection(connectionString); dbConnection.Open(); //openSqlConnection(connectionString); yield return new WaitForSeconds(3); Debug.Log(“Connected to database.”); }

  12. 管磊说:

    我在Plugins里面去掉system.Drawing.dll了,但是xcode报的错是:cannot use ‘throw’ with exceptions disabled.共有14处报错,都是这个错误。请问这个周末解决啊。。

  13. 鹏说:

    看大家都在纠结说IOS上无法发布(报错:Cross compilation job System.Drawing.dll failed.UnityEngine.UnityException: Failed AOT cross compiler: …………………………),最近专门研究了一下。在Plugins里面去掉system.Drawing.dll保留其他四个,发布Xcode就不会报错了,但是问题来了,Xcode编译的时候又会抛出两个错误,其实是一类错误”Use of undeclared identifier ‘GL_BGRA_EXT’“,找到报错的地方,修改头文件将OpenGLES/ES2/gl.h改成了OpenGLES/ES2/glext.h编译通过。编译通过,亲测没有问题,MySQL数据库可以访问,问题解决。

  14. xs说:

    说别人垃圾的时候你做了什么,再好的社会也会漏下几个你这样的没被带走

  15. a360289说:

    有解决这个问题么。。。

  16. hello说:

    雨松兄 请问一下在pc上可以 也能build 但一旦切换到web平台就报错 是什么情况

  17. student说:

    宣老师有unity网游方面客户端与服务器端的教程吗?

  18. u3d_lover说:

    我测试了PC,MAC,IOS,ANDROID,目前IOS无法发布(报错:Cross compilation job System.Drawing.dll failed.UnityEngine.UnityException: Failed AOT cross compiler: …………………………)其他的平台都可以运行

  19. u3d学习者说:

    “打开cmd 窗口cd到MySQL的路径下,一定要cd到这个路径下,不然mysql 会是无法识别的指令噢。”直接添加到path中就可以识别了,不用每次都这么麻烦“然后开始看看代码怎么写,为了方便数据库的创建、增加、删除、修改、查询、我封装了一个类。欢迎大家测试 啦啦啦啦。”这也算封装????sql就不该用拼接的方式来实现。。。

  20. 梧桐灬专攻精神说:

    宣老师,发现个问题,当我执行Updata语句去修改记录的字段有中文字符时候,都变了问号的,但是我已在该字段的整理设置为utf8_general_ci,用Select语句读取就没问题,但是修改就变问号了,希望能指点下。

留下一个回复

你的email不会被公开。 必填项已用 * 标注。