Unity 使用MySQL制作简易用户管理系统

前言

使用MySQL制作一个简易的用户信息管理系统,包括用户登录,新用户注册,修改用户密码,删除用户信息等。主要功能涉及到在Unity中对MySQL数据库的增删改查,数据库的连接之前的文章里有介绍到,这里就不多做赘述。
Unity连接数据库

用户登录功能

自定义对象赋值

    [Header("登录账号")]
    public string _name;
    [Header("登录密码")]
    public string _pwd;

    public Dictionary<string, string> userData = new Dictionary<string, string>();
    MySqlConnection connection;
    private string connectionString;//SQL连接信息

    private void Awake()
    {
        connectionString = "server=localhost;user id=root;password=登录密码;database=user;charset=utf8;";
    }

连接方法

    private void Start()
    {
        connection = new MySqlConnection(connectionString);
        try
        {
            connection.Open();

            string query = "SELECT * FROM data1";
            var command = new MySqlCommand(query, connection);
            MySqlDataReader reader = command.ExecuteReader();
            while (reader.Read())
            {
                //Debug.Log("开始读取内容");
                int columID = reader.GetInt32("uID");

                string columName = reader.GetString("uName");
                string columPwd = reader.GetString("uPwd");

                userData.Add(columName, columPwd);
                Debug.Log($"读取到内容,ID:{columID};姓名:{columName};密码:{columPwd}");
            }
            reader.Close();
        }
        catch (MySqlException ex)
        {
            Debug.LogError("Failed to connect to the database: " + ex.Message);
        }
        finally
        {
            connection.Close();
        }

    }

用户登录方法

我这只是一个简易的demo,方法就直接写在了Update里

    private void Update()
    {
        if (Input.GetKeyDown(KeyCode.Space))
        {
            Debug.Log("读取字典");
            if (userData.ContainsKey(_name))
            {
                Debug.Log("用户存在,账号输入正确");
                if (_pwd == userData[_name])
                {
                    Debug.Log("密码:" + userData[_name]);
                    Debug.Log("密码输入正确");
                }
                else
                {
                    Debug.Log("密码输入错误");
                }

            }
            else
            {
                Debug.Log("用户不存在");
            }

        }
    }

新用户注册方法

[Header("新增用户名")]
public string _NewName;
[Header("新增用户密码")]
public string _NewPwd;

void AddSql()
    {
        try
        {
            connection.Open();
            string query1 = "INSERT INTO data1 (uID, uName, uPwd) VALUES (@userID, @username, @password)";
            var command = new MySqlCommand(query1, connection);

            byte[] inputNewName = Encoding.UTF8.GetBytes(_NewName);
            string newName = Encoding.UTF8.GetString(inputNewName);

            long _userID = command.LastInsertedId;
            command.Parameters.AddWithValue("@userID", _userID);
            command.Parameters.AddWithValue("@username", _NewName);
            command.Parameters.AddWithValue("@password", _NewPwd);
            command.ExecuteNonQuery();
            Debug.Log("成功插入数据");
        }
        catch (Exception e)
        {
            Debug.LogError("Failed to connect to the database: " + e.Message);
        }
        finally
        {
            connection.Close();
        }
    }

 修改用户信息方法

也可以单独只修改一项

 
[Header("待修改用户")]
public string oldName;
[Header("用户新信息")]
public string newName;
[Header("用户新密码")]
public string newPwd;

void UpdateSql()
    {
        try
        {
            connection.Open();

            string query = "UPDATE data1 SET uName = @newUsername, uPwd = @newPassword WHERE uName = @oldUsername";
            var command = new MySqlCommand(query, connection);
            command.Parameters.AddWithValue("@newUsername", newName);
            command.Parameters.AddWithValue("@newPassword", newPwd);
            command.Parameters.AddWithValue("@oldUsername", oldName);

            command.ExecuteNonQuery();

            Debug.Log("成功更新数据");
        }
        catch (MySqlException ex)
        {
            Debug.LogError("Failed to connect to the database: " + ex.Message);
        }
        finally
        {
            connection.Close();
        }
    }

 删除用户信息

    void RemoveSql()
    {
        connection = new MySqlConnection(connectionString);
        try
        {
            connection.Open();
            string query1 = "DELETE FROM data1 WHERE BINARY uName = @username";

            var command = new MySqlCommand(query1, connection);
            command.Parameters.AddWithValue("@username", _RemoveName);
            command.ExecuteNonQuery();
            Debug.Log("成功删除数据");
        }
        catch (Exception e)
        {
            Debug.LogError("Failed to connect to the database: " + e.Message);
        }
        finally
        {
            connection.Close();
        }

    }

总结

这里都只是一些SQL语法在Unity里的简单应用,实现这些功能的方法肯定不止一种,大家可以多多扩展。

  • 23
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
使用MySQL进行增删改查,需要在Unity使用MySQL Connector/NET驱动程序。下面是一个简单的Unity C#代码示例,说明如何连接到MySQL数据库并执行增删改查操作: 首先,需要在Unity中安装MySQL Connector/NET驱动程序。可以从MySQL官方网站上下载。 然后,需要在C#项目中添加对MySQL Connector/NET的引用。可以在Visual Studio中右键单击项目,选择“添加引用”,然后选择MySQL Connector/NET。 接下来,需要在C#代码中使用MySQL Connector/NET命名空间。可以使用以下代码: using MySql.Data.MySqlClient; 然后,需要创建一个MySQL连接对象,并将其连接到数据库。可以使用以下代码: string connectionString = "Server=myServerAddress;Database=myDataBase;Uid=myUsername;Pwd=myPassword;"; MySqlConnection connection = new MySqlConnection(connectionString); connection.Open(); 在这个代码中,需要将“myServerAddress”替换为MySQL服务器的地址,“myDataBase”替换为要连接的数据库名称,“myUsername”替换为MySQL用户名,“myPassword”替换为MySQL密码。 接下来,可以使用MySQL命令对象执行SQL查询。可以使用以下代码: MySqlCommand command = connection.CreateCommand(); command.CommandText = "SELECT * FROM myTable"; MySqlDataReader reader = command.ExecuteReader(); while (reader.Read()) { Debug.Log(reader["columnName"].ToString()); } 在这个代码中,需要将“myTable”替换为要查询的表名称,“columnName”替换为要检索的列名称。 要执行其他操作,如插入、更新或删除,可以使用类似的方法。 例如,要插入一条记录,可以使用以下代码: MySqlCommand command = connection.CreateCommand(); command.CommandText = "INSERT INTO myTable (columnName1, columnName2) VALUES ('value1', 'value2')"; command.ExecuteNonQuery(); 在这个代码中,需要将“myTable”替换为要插入记录的表名称,“columnName1”和“columnName2”替换为要插入的列名称,“value1”和“value2”替换为要插入的值。 完成所有操作后,需要关闭MySQL连接。可以使用以下代码: connection.Close(); 这是一个简单的Unity使用MySQL进行增删改查的示例代码。注意,这只是一个起点,需要进一步学习MySQL Connector/NET和SQL查询语言才能深入了解如何使用MySQL进行增删改查。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

立刀人

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值