[计算机] Unity MySql 增删改查

查看:704 |回复:4 | 2021-9-2 09:31:36

您需要 登录 才可以下载或查看,没有账号?注册

x
本帖最后由 MrTang1988 于 2021-9-2 09:42 编辑



(仅供参考~~~~~~)
1、安装MysQl
https://blog.csdn.net/weixin_42869365/article/details/83472466
2、下载安装
Navicat for MySQL9.0
3、Unity中 从新建表到 增删改查(Plugins文件下导入相关DLL)I18N.Dll、I18N.West.Dll、MySql.Data.Dll、System.Data.Dll、System.Drawing.Dll4、代码实现部分
  1. using UnityEngine;
  2. using System;
  3. using System.Data;
  4. using System.Collections;
  5. using MySql.Data.MySqlClient;
  6. using MySql.Data;
  7. using System.IO;
  8. public class SqlAccess
  9. {
  10.     public static MySqlConnection dbConnection;
  11.     //如果只是在本地的话,写localhost就可以。
  12.     static string host = "localhost";  
  13.     //如果是局域网,那么写上本机的局域网IP
  14.     //static string host = "192.168.1.106";
  15.     static string id = "root";
  16.     static string pwd = "root";
  17.     //数据库名
  18.     static string database = "";
  19.     public SqlAccess()
  20.     {
  21.         OpenSql();
  22.     }

  23.     public static void OpenSql()
  24.     {
  25.         try
  26.         {
  27.             string connectionString = string.Format("Server = {0};port={4};Database = {1}; User ID = {2}; Password = {3};", host, database, id, pwd, "3306");
  28.             dbConnection = new MySqlConnection(connectionString);
  29.             dbConnection.Open();
  30.         }
  31.         catch (Exception e)
  32.         {
  33.             throw new Exception("服务器连接失败,请重新检查是否打开MySql服务。" + e.Message.ToString());

  34.         }

  35.     }
  36.    
  37.     /// <summary>
  38.     /// 代码新建数据表
  39.     /// </summary>
  40.     /// <param name="name">新建表名</param>
  41.     /// <param name="col">字段数组及具体字段</param>
  42.     /// <param name="colType">字段类型参数对应</param>
  43.     /// <returns></returns>
  44.     public DataSet CreateTableAutoID(string name, string[] col, string[] colType)
  45.     {
  46.         if (col.Length != colType.Length)
  47.         {
  48.             throw new Exception("columns.Length != colType.Length");
  49.         }
  50.         string query = "CREATE TABLE " + name + " (" + col[0] + " " + colType[0] + " NOT NULL AUTO_INCREMENT";
  51.         for (int i = 1; i < col.Length; ++i)
  52.         {
  53.             query += ", " + col[i] + " " + colType[i];
  54.         }
  55.         query += ", PRIMARY KEY (" + col[0] + ")" + ")";
  56.         Debug.Log(query);
  57.         return ExecuteQuery(query);
  58.     }
  59.     //插入一条数据,包括所有,不适用自动累加ID。
  60.     /// <summary>
  61.     /// 插入数据
  62.     /// </summary>
  63.     /// <param name="tableName">表单名称、</param>
  64.     /// <param name="values"></param>
  65.     /// <returns></returns>
  66.     public DataSet InsertInto(string tableName, string[] values)
  67.     {
  68.         string query = "INSERT INTO " + tableName + " VALUES (" + "'" + values[0] + "'";
  69.         for (int i = 1; i < values.Length; ++i)
  70.         {
  71.             query += ", " + "'" + values[i] + "'";
  72.         }
  73.         query += ")";
  74.         Debug.Log(query);
  75.         return ExecuteQuery(query);
  76.     }
  77.     //插入部分ID
  78.     /// <summary>
  79.     /// 插入数据
  80.     /// </summary>
  81.     /// <param name="tableName">数据表名称</param>
  82.     /// <param name="col">字段名称</param>
  83.     /// <param name="values">数据值</param>
  84.     /// <returns></returns>
  85.     public DataSet InsertInto(string tableName, string[] col, string[] values)
  86.     {
  87.         if (col.Length != values.Length)
  88.         {
  89.             throw new Exception("columns.Length != colType.Length");
  90.         }
  91.         string query = "INSERT INTO " + tableName + " (" + col[0];
  92.         for (int i = 1; i < col.Length; ++i)
  93.         {
  94.             query += ", " + col[i];
  95.         }
  96.         query += ") VALUES (" + "'" + values[0] + "'";
  97.         for (int i = 1; i < values.Length; ++i)
  98.         {
  99.             query += ", " + "'" + values[i] + "'";
  100.         }
  101.         query += ")";
  102.         Debug.Log(query);
  103.         return ExecuteQuery(query);
  104.     }
  105.     /// <summary>
  106.     /// 查找数据
  107.     /// </summary>
  108.     /// <param name="tableName">数据表名称</param>
  109.     /// <param name="items">需要查找字段名称</param>
  110.     /// <param name="col">查找数据字段使用属性字段</param>
  111.     /// <param name="operation">判断字符</param>
  112.     /// <param name="values">查找数据字段使用属性字段对应的值</param>
  113.     /// <returns></returns>
  114.     public DataSet SelectWhere(string tableName, string[] items, string[] col, string[] operation, string[] values)
  115.     {
  116.         if (col.Length != operation.Length || operation.Length != values.Length)
  117.         {
  118.             throw new Exception("col.Length != operation.Length != values.Length");
  119.         }
  120.         string query = "SELECT " + items[0];
  121.         for (int i = 1; i < items.Length; ++i)
  122.         {
  123.             query += ", " + items[i];
  124.         }
  125.         query += " FROM " + tableName + " WHERE " + col[0] + operation[0] + "'" + values[0] + "' ";
  126.         for (int i = 1; i < col.Length; ++i)
  127.         {
  128.             query += " AND " + col[i] + operation[i] + "'" + values[0] + "' ";
  129.         }
  130.         return ExecuteQuery(query);
  131.     }
  132.     /// <summary>
  133.     ///
  134.     /// </summary>
  135.     /// <param name="tableName">数据表名</param>
  136.     /// <param name="cols">需要更新的字段</param>
  137.     /// <param name="colsvalues">需要更新的字段对应的值</param>
  138.     /// <param name="selectkey">定位关系数据字段</param>
  139.     /// <param name="selectvalue">定位关系数据对应的值</param>
  140.     /// <returns></returns>
  141.     public DataSet UpdateInto(string tableName, string[] cols, string[] colsvalues, string selectkey, string selectvalue)
  142.     {
  143.         //更新字段值
  144.         string query = "UPDATE " + tableName + " SET " + cols[0] + " = " + colsvalues[0];
  145.         for (int i = 1; i < colsvalues.Length; ++i)
  146.         {
  147.             query += ", " + cols[i] + " =" + colsvalues[i];
  148.         }   
  149.         query += " WHERE " + selectkey + " = " + selectvalue + " ";
  150.         return ExecuteQuery(query);
  151.     }
  152.     /// <summary>
  153.     /// 删除数据
  154.     /// </summary>
  155.     /// <param name="tableName">数据表名</param>
  156.     /// <param name="cols">字段</param>
  157.     /// <param name="colsvalues">字段对应的值</param>
  158.     /// <returns></returns>
  159.     public DataSet Delete(string tableName, string[] cols, string[] colsvalues)
  160.     {
  161.         string query = "DELETE FROM " + tableName + " WHERE " + cols[0] + " = " + colsvalues[0];
  162.         for (int i = 1; i < colsvalues.Length; ++i)
  163.         {
  164.             query += " or " + cols[i] + " = " + colsvalues[i];
  165.         }
  166.         Debug.Log(query);
  167.         return ExecuteQuery(query);
  168.     }
  169.     public void Close()
  170.     {
  171.         if (dbConnection != null)
  172.         {
  173.             dbConnection.Close();
  174.             dbConnection.Dispose();
  175.             dbConnection = null;
  176.         }
  177.     }

  178.     /// <summary>
  179.     /// 执行Sq语句
  180.     /// </summary>
  181.     /// <param name="sqlString"></param>
  182.     /// <returns></returns>
  183.     public static DataSet ExecuteQuery(string sqlString)
  184.     {
  185.         if (dbConnection.State == ConnectionState.Open)
  186.         {
  187.             DataSet ds = new DataSet();
  188.             try
  189.             {
  190.                 MySqlDataAdapter da = new MySqlDataAdapter(sqlString, dbConnection);
  191.                 da.Fill(ds);
  192.             }
  193.             catch (Exception ee)
  194.             {
  195.                 throw new Exception("SQL:" + sqlString + "/n" + ee.Message.ToString());
  196.             }
  197.             finally
  198.             {
  199.             }
  200.             return ds;
  201.         }
  202.         return null;
  203.     }
  204. }
  205. [code]csharpcode:
  206. using UnityEngine;
  207. using System;
  208. using System.Data;
  209. using System.Collections;
  210. using MySql.Data.MySqlClient;
  211. using MySql.Data;
  212. using System.IO;
  213. public class SqlControll : MonoBehaviour
  214. {
  215.         string Error = null;
  216.         SqlAccess sql;
  217.         void Start()
  218.         {
  219.                

  220.         }
  221.         /// <summary>
  222.         /// 新建表单
  223.         /// </summary>
  224.         /// <param name="_tableName">表单名称</param>
  225.         /// <param name="_field">表单中字段</param>
  226.         /// <param name="_fieldType">表单中字段的类型</param>
  227.         private void CreateTable(string _tableName,string[] _field,string[] _fieldType)
  228.         {
  229.                 //sql.CreateTableAutoID("momo", new string[] { "id", "name", "qq", "email", "blog" }, new string[] { "int", "text", "text", "text", "text" });               
  230.                 try
  231.                 {
  232.                         sql = new SqlAccess();
  233.                         sql.CreateTableAutoID(_tableName, _field, _fieldType);
  234.                         sql.Close();
  235.                 }
  236.                 catch (Exception e)
  237.                 {
  238.                         Error = e.Message;
  239.                 }
  240.         }
  241.         /// <summary>
  242.         /// 添加数据
  243.         /// </summary>
  244.         /// <param name="_tableName">表单名称</param>
  245.         /// <param name="_field">表单中字段</param>
  246.         /// <param name="_fieldType">表单中字段赋值</param>
  247.         private void InsertIntoData(string _tableName, string[] _field, string[] _fieldValue)
  248.         {
  249.                 //sql.InsertInto("momo", new string[] { "name", "qq", "email", "blog" }, new string[] { "ruoruo", "34546546", "ruoruo@gmail.com", "xuanyusong.com" });
  250.                 try {
  251.                         sql.InsertInto(_tableName, _field, _fieldValue);
  252.                         sql.Close();
  253.                 }
  254.                 catch(Exception e)
  255.                 {
  256.                         Error = e.Message;
  257.                 }
  258.         }
  259.         /// <summary>
  260.         /// 删除数据
  261.         /// </summary>
  262.         /// <param name="_tableName">表单名称</param>
  263.         /// <param name="_field">表单中字段</param>
  264.         /// <param name="_fieldValue">表单中字段赋值</param>
  265.         private void DeleteData(string _tableName, string[] _field, string[] _fieldValue)
  266.         {
  267.                 //sql.Delete("momo", new string[] { "qq", "email" }, new string[] { "34546546", "'000@gmail.com'" });
  268.                 try
  269.                 {
  270.                         sql.Delete(_tableName, _field, _fieldValue);
  271.                         sql.Close();
  272.                 }
  273.                 catch (Exception e)
  274.                 {
  275.                         Error = e.Message;
  276.                 }
  277.         }

  278.         /// <summary>
  279.         /// 更新数据(通过关系字段及值确定在表中的数据组,并更新在数据组中需要更新的数据)
  280.         /// </summary>
  281.         /// <param name="tableName">数据表名</param>
  282.         /// <param name="_field">需要更新的字段</param>
  283.         /// <param name="_fieldValue">需要更新的字段对应的值</param>
  284.         /// <param name="_verifyField">关系字段</param>
  285.         /// <param name="_verifyFieldValue">关系字段值</param>
  286.         private void UpdateData(string _tableName, string[] _field, string[] _fieldValue,string _verifyField,string _verifyFieldValue)
  287.         {
  288.                 //sql.UpdateInto("momo", new string[] { "name", "qq" }, new string[] { "'ruoruo'", "'11111111'" }, "email", "'xuanyusong@gmail.com'");
  289.                 try
  290.                 {
  291.                         sql.UpdateInto(_tableName, _field, _fieldValue, _verifyField, _verifyFieldValue);
  292.                         sql.Close();
  293.                 }
  294.                 catch (Exception e)
  295.                 {
  296.                         Error = e.Message;
  297.                 }
  298.         }
  299.         /// <summary>
  300.         /// 查找数据(通过关系字段及值确定在表中的数据组,并查找在数据组中需要查找的数据)
  301.         /// </summary>
  302.         /// <param name="_tableName"></param>
  303.         /// <param name="_field">查找的数据字段</param>
  304.         /// <param name="_verifyField">关系字段</param>
  305.         /// <param name="_verifyFieldValue">关系字段值</param>
  306.         private void SelectData(string _tableName, string[] _field, string[] _verifyField,  string[] _verifyFieldValue)
  307.         {
  308.                 //DataSet ds = sql.SelectWhere("momo", new string[] { "name", "email" }, new string[] { "qq" }, new string[] { "=" }, new string[] { "34546546" });
  309.                 try
  310.                 {
  311.                         DataSet ds = sql.SelectWhere(_tableName, _field, _verifyField, new string[] { "=" }, _verifyFieldValue);
  312.                         sql.Close();
  313.                         if (ds != null)
  314.                         {
  315.                                 DataTable table = ds.Tables[0];
  316.                                 foreach (DataRow row in table.Rows)
  317.                                 {
  318.                                         foreach (DataColumn column in table.Columns)
  319.                                         {
  320.                                                 Debug.Log(row[column]);
  321.                                         }
  322.                                 }
  323.                         }
  324.                 }
  325.                 catch (Exception e)
  326.                 {
  327.                         Error = e.Message;
  328.                 }
  329.         }
  330.         // Update is called once per frame
  331.         void OnGUI()
  332.         {
  333.                 if (Error != null)
  334.                 {
  335.                         GUILayout.Label(Error);
  336.                 }
  337.         }
  338. }
点击此处复制文本



2021-9-2 09:31:36  
 赞 赞 1

使用道具 登录

4个回答,把该问题分享到群,邀请大神一起回答。
2#
感谢分享
回复 收起回复
2021-9-2 17:22:05   回复
 赞 赞 1

使用道具 登录

3#
VS打开SqlAccess卡了一下然后闪退,是什么缘故?
回复 收起回复
2021-11-17 16:59:21   回复
 赞 赞 2

使用道具 登录

4#
回复 收起回复
2023-3-1 12:08:42   回复
 赞 赞 1

使用道具 登录

5#
回复 收起回复
2023-8-30 09:42:09   回复
 赞 赞 0

使用道具 登录

CG 游戏行业专业问题

程序逻辑文章算法
您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表