Unity链接Mysql 数据库实现注册登录

1.搭建注册和登录的UI以及跳转代码撰写

2.安装Mysql 数据库到服务器或者本地电脑

我这里使用的是小皮工具,安装玩数据库后创建一个新的用户以及表格

安装Navicate 链接数据库,方便可视化数据库

点击查询-新建查询-输入命令-运行!完成表格创建!

在这里执行一条SQL数据库代码命令,目的是创建一个表格。

下面就是创建一个简单表格的命令!

sql 复制代码
CREATE TABLE `user` (
  `id` INT NOT NULL AUTO_INCREMENT,
  `username` VARCHAR(50) NOT NULL COMMENT '账号',
  `password` VARCHAR(255) NOT NULL COMMENT '密码哈希值',
  PRIMARY KEY (`id`),
  UNIQUE KEY `idx_username` (`username`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='用户账号表'; 

截图里面我是已经创建过了!

3.把MySql.Data.dll 放到Unity 项目Assets 里面

cs 复制代码
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;
using MySql.Data.MySqlClient;

using UnityEngine.XR.ARFoundation;
using UnityEngine.SceneManagement;


public class UserDataMysql : MonoBehaviour
{
    // 用户输入的用户名和密码,存储到 MySQL 数据库中
    public TMP_InputField UserAccountInput;
    public TMP_InputField UserPasswordInput;


    public TMP_InputField UserLoginAccountInput;
    public TMP_InputField UserLoginPasswordInput;
    public void ResigerButton()
    { 
    // 注册按钮的点击事件,将用户输入的用户名和密码存储到 MySQL 数据库中
        string userAccount = UserAccountInput.text;
        string userPassword = UserPasswordInput.text;
        // 连接 MySQL 数据库,执行 SQL 语句,将用户名和密码存储到数据库中

        // 1.连接数据库
        string connStr = "server=127.0.0.1;port=3306;user=xuni231;password=123456;database=ar";
        MySqlConnection conn = new MySqlConnection(connStr);
        conn.Open();

        // 2.执行 SQL 语句
        string sql = "INSERT INTO user (username, password) VALUES ('" + userAccount + "','" + userPassword + "')";
        MySqlCommand cmd = new MySqlCommand(sql, conn);
        cmd.ExecuteNonQuery();
        Debug.Log("注册成功"+ userAccount+"密码"+ userPassword);

        // 3.关闭数据库连接
        conn.Close();   

    }


    public void LoginButton()
    {
        // 登录按钮的点击事件,从 MySQL 数据库中查询用户输入的用户名和密码是否匹配
        string userAccount = UserLoginAccountInput.text;//用户登录时输入的用户名
        string userPassword = UserLoginPasswordInput.text;//用户登录时输入的密码
                                                          // 连接 MySQL 数据库,执行 SQL 语句,查询用户名和密码是否匹配  

        // 1.连接数据库
        string connStr = "server=127.0.0.1;port=3306;user=xuni231;password=123456;database=ar";
        MySqlConnection conn = new MySqlConnection(connStr);
        conn.Open();

        // 2.执行 SQL 语句
        string sql = "SELECT * FROM user WHERE username='" + userAccount + "' AND password='" + userPassword + "'";
        MySqlCommand cmd = new MySqlCommand(sql, conn);
        MySqlDataReader reader = cmd.ExecuteReader();//把用户输入的账号密码,发给数据库,查询是否匹配

        // 3.读取查询结果,判断是否匹配
        if (reader.Read())
        {
            Debug.Log("登录成功");
            // 登录成功,跳转到AR人体识别界面
            ARButton();
        }
        else
        {
            Debug.Log("登录失败");
        }

        // 4.关闭数据库连接
        conn.Close();
    }

    public void ARButton()
    {
        // 跳转到AR地面识别场景,场景名字:ARMainScence
        //Application.LoadLevel("ARMainScence");//loadlevel是加载场景的函数,参数是场景的名字
       SceneManager.LoadScene("ARMainScence");//SceneManager.LoadScene是加载场景的函数,参数是场景的名字
 }
}
相关推荐
私人珍藏库3 小时前
【Android】Soul v5.86.0 内置模块版
android·app·工具·软件·多功能
千里马学框架4 小时前
aosp新增窗口层级 Type 完整实现方案(有源码)-wms需求和面试题
android·智能手机·架构·wms·aaos·车机
峥嵘life9 小时前
Android 蓝牙设备连接广播详解-2026
android·python·学习
MusingByte12 小时前
别再裸用 Claude Code 了!安卓开发者必装 13 个官方推荐插件,效率翻 3 倍省 70% token
android
_李小白12 小时前
【android opencv学习笔记】Day 29: 滤波算法之Sobel 边缘检测
android·opencv·学习
Dxy123931021613 小时前
Python 操作 MySQL 事务:从入门到避坑
android·python·mysql
峥嵘life15 小时前
Android getprop 属性限制详解:User 版本属性获取问题分析
android·开发语言·python·学习
一航jason16 小时前
Speed Tools:一套低侵入的 Android 插件化 + 动态换肤 + 字体切换框架
android·插件化·组件化·换肤
李斯维16 小时前
Jetpack 可观察数据容器 LiveData 的入门与基础使用
android·android jetpack
问心无愧051317 小时前
ctf show web入门261
android·前端·笔记