当前位置:   article > 正文

【Unity】连接MySQL实现增、删、改、查_unity mysql

unity mysql

1.需要先安装MySQL,如果已经安装则忽略

安装教程: MySQL安装教程

2.下载mysql-connector-net库

官方下载地址:https://downloads.mysql.com/archives/c-net/
提示:选择和自己MySQL相匹配的版本,不然会跟unity版本匹配不上。
我MySQL是5.6.39 下载的mysql-connector-net库是5.2.7
在这里插入图片描述

3.解压后,找到mysql.data.dll,放入Unity工程里

请添加图片描述

4.在Unity的安装目录下,把这几个dll放入Unity资源的Plugins文件夹中,防止打包出来连不上数据库。

我的路径为:C:\Program Files\Unity\Hub\Editor\2021.3.33f1c1\Editor\Data\MonoBleedingEdge\lib\mono\unityjit-win32
请添加图片描述

5.执行脚本

using MySql.Data.MySqlClient;
using System;
using System.Data;
using UnityEngine;
 
public class Test : MonoBehaviour
{
    public MySqlConnection mysql;
    static string host = "localhost";
    static string port = "3306";
    static string username = "root";
    static string pwd = "123456";
    static string database = "gametest";
 
    void Start()
    {
        OpenSQL();
    }
    //连接数据库
    public void OpenSQL()
    {
        //建立连接语句
        string constr = string.Format("server = {0};port={1};database = {2};user = {3};password = {4};", host, port, database, username, pwd);
        //建立连接
        mysql = new MySqlConnection(constr);
        //打开连接
        mysql.Open();
    }

    //查询,读取数据
    public void Search(string tableName)
    {
        //sql命令,选择gametest表
        string sqlString = "select * from "+ tableName;
        MySqlCommand cmd = new MySqlCommand(sqlString, mysql);
        MySqlDataReader reader = cmd.ExecuteReader();
        //Read一次就是一行数据,Read不为空执行打印数据
        while (reader.Read())
        {
            Debug.Log("查询数据:"+reader[0] + "  " + reader[1] + "   " + reader[2]);
        }
    }
 
    //添加数据
    public void AddData(int id, int level,string name)
    {
        //在表player中添加ID = id,LV = level,Name = name
        string sql = "insert into player(ID,LV,Name) values('" + id + "','" + level + "','" + name + "')";
        MySqlCommand cmd = new MySqlCommand(sql, mysql);
        //返回更改数据行数
        int result = cmd.ExecuteNonQuery();
        Debug.Log("添加数据成功:"+ result);
       
    }
    //更新数据
    public void UpdateData()
    {
        //更新表player中ID = 2的数据 ,设置LV = 9,Name = zhang
        string sql = "update player set LV='9',Name='zhang' where ID=2";//更改的sql命令
        MySqlCommand cmd = new MySqlCommand(sql, mysql);
        //返回值是数据库中修改的行数
        int result = cmd.ExecuteNonQuery();
        Debug.Log("更新数据成功:" + result);
      
    }
    //删除数据
   	public void DeleteData()
    {
        //删除的sql命令,这里是删除player中id=20的一行数据
        string sql = "delete from player where ID=2";                                     
        MySqlCommand cmd = new MySqlCommand(sql, mysql);
        int result = cmd.ExecuteNonQuery();
        Debug.Log("删除数据成功:" + result);
    }
 
    /// <summary>
    /// 关闭数据库连接
    /// </summary>
    public void Close()
    {
        if (mysql != null)
        {
            mysql.Close();
            mysql.Dispose();
            mysql = null;
        }
    }
 
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89

6.查询测试

在这里插入图片描述

6.增加测试

在这里插入图片描述

7.修改测试

在这里插入图片描述

8.删除测试

在这里插入图片描述

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/空白诗007/article/detail/759021
推荐阅读
相关标签
  

闽ICP备14008679号