Unity连接Mysql数据库
Posted 剑起苍穹
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Unity连接Mysql数据库相关的知识,希望对你有一定的参考价值。
1.unity连接数据库需要的dll文件
下载链接: 直接复制下载即可
https://files.cnblogs.com/files/qq2351194611/Plugins%E8%BF%9E%E6%8E%A5mysqldll%E6%96%87%E4%BB%B6.zip
如果是android端需要全部放入,并且需要对应unity版本,去unity安装目录下找到拖进去即可,
如果是PC端只需要一个Mysql.data.dll 文件就行 ,我的版本是Unity2019.4.5f1的, 设置里面.net设置成4.x,不然会报错
下面上代码
两个脚本都能连接成功并读取添加修改(别同时挂载,同时在应该会出BUG,可以分开试验)
1.
using MySql.Data.MySqlClient; using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; public class Connect_Mysql { public bool login_success = false; public bool register_success = false; //数据库表中的数据 名字必须是一样的 要不然上传不上去 public int id; public string username; public string password; //登录验证 public void Click_login(string username, string password) { // server连接地址 port端口号 database表单名 user用户名 password数据库密码 // 用户名一般都是root 你修改了就换成你修改的名称跟密码 string sqlSer = "server =127.0.0.1;database = tool;user = root;password =123456;"; //string sqlSer = "server = localhost" + // "port = 3306;" + // "database = tool;" + // "user =root;" + // "password =123456;"; MySqlConnection conn = new MySqlConnection(sqlSer); try { conn.Open(); Debug.Log("------连接成功------"); //user 表名 string sqlQuary = "select * from user where username =@paral1 and password = @paral2"; MySqlCommand comd = new MySqlCommand(sqlQuary, conn); comd.Parameters.AddWithValue("paral1", username); comd.Parameters.AddWithValue("paral2", password); MySqlDataReader reader = comd.ExecuteReader(); if (reader.Read()) { Debug.Log("------用户存在,登录成功!------"); //进行登入成功后的操作,例如进入新场景。。。 login_success = true; } else { Debug.Log("------用户不存在,请注册。或请检查用户名或和密码!------"); login_success = false; } } catch (System.Exception e) { Debug.Log(e.Message); } finally { conn.Close(); } } //注册验证 public void Click_register(string username, string password) { string sqlSer = "server =127.0.0.1;database = tool;user = root;password =123456;"; MySqlConnection conn = new MySqlConnection(sqlSer); try { conn.Open(); Debug.Log("-----连接成功!------"); string sqlQuary = "select * from user where username =@paral1 and password = @paral2"; MySqlCommand comd = new MySqlCommand(sqlQuary, conn); comd.Parameters.AddWithValue("paral1", username); comd.Parameters.AddWithValue("paral2", password); //comd.Parameters.AddWithValue("paral0", 0);id=@para10 and MySqlDataReader reader = comd.ExecuteReader(); if (reader.Read()) { Debug.Log("-----用户名已存在,请重新输!------"); register_success = false; } else { Insert_User(username, password); Debug.Log("------注册成功,请进行登入------"); register_success = true; } } catch (System.Exception e) { Debug.Log(e.Message); } finally { conn.Close(); } } //插入用户 private void Insert_User(string username, string password) { string sqlSer = "server =127.0.0.1;database = tool;user = root;password =123456;"; MySqlConnection conn = new MySqlConnection(sqlSer); try { conn.Open(); string sqlInsert = "insert into user(username,password) values(\'" + username + "\',\'" + password + "\')"; MySqlCommand comd2 = new MySqlCommand(sqlInsert, conn); int resule = comd2.ExecuteNonQuery(); Debug.Log("添加成功"); } catch (System.Exception e) { Debug.Log(e.Message); } finally { conn.Close(); } } } public class LoginZhuCe : MonoBehaviour { public static LoginZhuCe _instance; public Text login_tips; public GameObject login_panel; public GameObject zuce_panel; //登录 输入框 public InputField input_username; public InputField input_password; //注册 输入框 public InputField iusername; public InputField ipassword; public string username; public string password; Connect_Mysql connect = new Connect_Mysql(); private void Awake() { _instance = this; } public void Panel_bool(bool ol) { if (ol) { login_panel.SetActive(false); zuce_panel.SetActive(true); } else { login_panel.SetActive(true); zuce_panel.SetActive(false); } } //当点击登录按钮时 public void WhenClick_login() { username = input_username.text.ToString(); password = input_password.text.ToString(); connect.Click_login(username, password); if (connect.login_success) { login_tips.text = "登录成功!"; When_login_success(); } else { login_tips.text = "登录失败!"; Invoke("InvokeFouncation", 1.5f); } } //当点击注册按钮时 public void WhenClick_register() { username = iusername.text; password = ipassword.text; connect.Click_register(username, password); if (connect.register_success) { login_tips.text = "注册成功!"; Invoke("InvokeFouncation", 1.5f); } else { login_tips.text = "注册失败!"; Invoke("InvokeFouncation", 1.5f); } } //点击登录与注册按钮出现的成功与失败提示 private void InvokeFouncation() { login_tips.text = ""; } //登入成功 private void When_login_success() { Debug.Log("登入成功哦"); } }
2.
using MySql.Data.MySqlClient; using System.Collections; using System.Collections.Generic; using UnityEngine; /// <summary> /// 连接数据库 包括增删改查功能 /// </summary> public class LianJie : MonoBehaviour { //database代表你要连接的数据库的名字 private const string datebase = "server=127.0.0.1;user=root;password=123456;database=tool;"; private MySqlConnection connection = null; string sql; void Start() { connection = new MySqlConnection(datebase); connection.Open(); //查询 // sql = "select * from user"; //query(sql); //插入 sql = "insert into user values(\'3\',\'我的\',\'123\')"; insert(sql); //sql = "delete from user where id = 2"; //delete(sql); sql = "select * from user"; query(sql); //sql = "update admin set account = \'dasfsaf\' where id = 2"; //updateData(sql); //sql = "select * from admin"; //query(sql); connection.Close(); } //查询 private void query(string sql) { MySqlCommand command = new MySqlCommand(sql, connection); MySqlDataReader reader = command.ExecuteReader(); while (reader.Read()) { print("id:" + reader.GetInt32("id") + "username:" + reader.GetString("username") + " password" + reader.GetString("password")); } command.Dispose(); } //插入 private void insert(string sql) { MySqlCommand command = new MySqlCommand(sql, connection); int n = command.ExecuteNonQuery(); if (n > 0) print("插入成功"); else print("插入失败"); command.Dispose(); } //删除 private void delete(string sql) { MySqlCommand command = new MySqlCommand(sql, connection); int n = command.ExecuteNonQuery(); command.Dispose(); } //改 private void updateData(string sql) { MySqlCommand command = new MySqlCommand(sql, connection); int n = command.ExecuteNonQuery(); command.Dispose(); } }
下面把完整项目包(登录注册)附上
下载地址:
https://files.cnblogs.com/files/qq2351194611/%E7%99%BB%E5%BD%95%E6%B3%A8%E5%86%8C%E8%BF%9E%E6%8E%A5Mysql%E6%95%B0%E6%8D%AE%E5%BA%93%E8%B5%84%E6%BA%90%E5%8C%85.rar
下载完成,新建unity项目导入即可
以上是关于Unity连接Mysql数据库的主要内容,如果未能解决你的问题,请参考以下文章
连接MySQL出现错误:ERROR 1045 (28000): Access denied for user ‘root‘@‘localhost‘ (using password: YES)(代码片段