unity3d链接mysql数据库_Unity 3D 连接Mysql数据库

usingUnityEngine;usingSystem;usingSystem.Data;usingSystem.Collections;usingMySql.Data.MySqlClient;usingMySql.Data;usingSystem.IO;public classSqlAccess

{public staticMySqlConnection dbConnection;//如果只是在本地的话,写localhost就可以。//static string host = "localhost";//如果是局域网,那么写上本机的局域网IP

static string host = "127.0.0.1";static string id = "root";static string pwd = "123456";static string database = "test";publicSqlAccess()

{

OpenSql();

}public static voidOpenSql()

{try{string connectionString = string.Format("Server = {0};port={4};Database = {1}; User ID = {2}; Password = {3};",host,database,id,pwd,"3306");

dbConnection= newMySqlConnection(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+= ")";returnExecuteQuery(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);returnExecuteQuery(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);returnExecuteQuery (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);returnExecuteQuery (query);

}

//读取表的数据public DataSet SelectAll(stringName)

{string query ="select * from"+" "+Name;returnExecuteQuery (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] + "'";

}returnExecuteQuery (query);

}

//更新数据public DataSet UpdateInto (string tableName, string []cols,string []colsvalues,string selectkey,stringselectvalue)

{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+" ";returnExecuteQuery (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);returnExecuteQuery (query);

}public voidClose()

{if(dbConnection != null)

{

dbConnection.Close();

dbConnection.Dispose();

dbConnection= null;

}

}

//执行sql语句public static DataSet ExecuteQuery(stringsqlString)

{if(dbConnection.State==ConnectionState.Open)

{

DataSet ds= new DataSet(); //表的集合

try{

MySqlDataAdapter da= newMySqlDataAdapter(sqlString, dbConnection);

da.Fill(ds);

}catch(Exception ee)

{throw new Exception("SQL:" + sqlString + "/n" +ee.Message.ToString());

}finally{

}returnds;

}return null;

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值