【UNITY】MYSQL配置+Navicat连接MYSQL+UNITY简易账号密码登陆功能

一、 MYSQL配置:

  1. 下载对应的mysql:downloads.mysql.com/archives/
  2. 环境变量→系统变量→Path,新建一个环境变量,添加mysql的bin文件目录。例如:D:\mysql-8.0.18winx64\bin
  3. 以管理员权限打开cmd,输入:①cd D:\mysql-8.0.18winx64\bin(cd 空格 mysql的bin路径)②mysqld -install ③mysqld --initialize ④ net start mysql(显示MySQL服务正在启动... MySQL服务已经启动成功)
  4. 输入 mysql -u root -p,然后输入密码进入数据库(如果输入密码时无法直接回车键进去并忘记默认的密码,提示ERROR 1405(28000),则参考blog.csdn.net/uniquewonderq/article/details/95077299)

 

二、Navicat连接MYSQL:

        文件→新建连接→MySQL,连接名随意;主机、端口、用户名、密码与所要连接的数据库要匹配

 

三、UNITY简易账号密码登陆

using MySql.Data.MySqlClient;
using System;
using System.Data;
using UnityEngine;

public class LoginController : MonoBehaviour
{
    private static MySqlConnection sqlConnection;
    private static string connStr = "Database=mysql_test;Data Source=127.0.0.1;User Id=root;Password=root;port=3306";

    //连接数据库
    public static MySqlConnection MysqlConn()
    {
        MySqlConnection connection = null;
        connection = new MySqlConnection(connStr);
        return connection;
    }

    //在DataSet中尝试匹配玩家输入的账号密码
    public bool MatchAccAndPassw(string sqlTable, string acc, string passw)
    {
        string query = "SELECT * FROM " + sqlTable;
        DataSet tableSet = QueryAcc(query);
        DataTable table = tableSet.Tables[0];
        if (table != null)
        {
            for (int i = 0; i < table.Rows.Count; i++)
            {
                Debug.Log("账号:" + table.Rows[i][0].ToString() + ",密码:" + table.Rows[i][1].ToString());
            if (table.Rows[i][0].ToString() == acc)
            {
                if (table.Rows[i][1].ToString() == passw)
                {
                    Debug.Log("登陆成功");
                    return true;
                }
                else
                {
                    Debug.LogError("密码不正确,请重试");
                    return false;
                }
            }
            else
            {
                Debug.LogError("账号不存在,请重试");
                return false;
                }
            }
        }
        return false;
    }
    
    //将查询到的数据存到DataSet并返回
    //sqlStr:数据库查询指令
    public DataSet QueryAcc(string sqlStr)
    {
        using (var connection = MysqlConn())
        {
            DataSet ds = new DataSet();
            try
            {
                connection.Open();
                MySqlDataAdapter dataAda = new MySqlDataAdapter(sqlStr, connection);
                dataAda.Fill(ds);
            }
            catch (Exception e)
            {
                connection.Close();
                throw new Exception(e.Message);
            }
            finally
            {
                connection.Close();
            }
            return ds;
        }
    }
}

 

        ***报错MySQL Client does not support authentication:

                管理员权限打开cmd,进入mysql:

//alter user '用户名'@'主机'  identified with mysql_native_password by '密码';
mysql> alter user 'root'@'localhost' identified with mysql_native_password by '123456';

mysql> flush privileges;

 

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Unity连接MySQL数据库可以通过使用MySQL Connector/NET来实现。下面是一些基本步骤: 1. 下载并安装MySQL Connector/NET:首先,你需要从MySQL官方网站下载并安装MySQL Connector/NET。这是一个用于在.NET应用程序连接和操作MySQL数据库的驱动程序。 2. 导入MySQL Connector/NET到Unity项目:在Unity,你需要将MySQL Connector/NET的DLL文件导入到项目。可以将DLL文件直接拖放到Unity的Assets文件夹。 3. 编写连接代码:在Unity,你可以使用C#编写代码来连接MySQL数据库。首先,你需要在代码引入MySQL Connector/NET的命名空间。然后,使用连接字符串来指定数据库连接信息,包括服务器地址、用户名、密码等。最后,使用MySQL Connector/NET提供的类和方法来执行数据库操作,如查询、插入、更新等。 以下是一个简单的示例代码,展示了如何连接MySQL数据库并执行查询操作: ```csharp using System; using MySql.Data.MySqlClient; public class MySQLConnector : MonoBehaviour { private MySqlConnection connection; private string server = "localhost"; private string database = "your_database_name"; private string uid = "your_username"; private string password = "your_password"; void Start() { string connectionString = $"Server={server};Database={database};Uid={uid};Pwd={password};"; connection = new MySqlConnection(connectionString); try { connection.Open(); Debug.Log("Connected to MySQL database!"); // 执行查询操作 string query = "SELECT * FROM your_table_name"; MySqlCommand cmd = new MySqlCommand(query, connection); MySqlDataReader dataReader = cmd.ExecuteReader(); while (dataReader.Read()) { // 处理查询结果 string column1 = dataReader.GetString(0); string column2 = dataReader.GetString(1); Debug.Log($"Column 1: {column1}, Column 2: {column2}"); } dataReader.Close(); } catch (Exception e) { Debug.Log("Error connecting to MySQL database: " + e.Message); } finally { connection.Close(); } } } ``` 请注意,上述代码仅为示例,你需要根据你的实际情况进行修改和扩展。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值