Unity使用MySql实现简单的增删改查

准备

官方文档示例

  1. MySqlConnectionStringBuilder 连接相关设置
  2. MySqlException Sql异常
  3. MySqlConnection 打开数据库 关闭数据库
  4. MySqlCommand Sql命令
    ExecuteReader 查询数据
    ExecuteNonQuery 插入 更新 删除数据
    ExecuteScalar 返回操作结果第一行第一列数据,可查询,插入,更新,删除

示例

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

public class ConnectMySql : MonoBehaviour
{
    MySqlConnectionStringBuilder mySqlConnectionString = new MySqlConnectionStringBuilder()
    {
        Server = "localhost",
        Port = 3306,
        UserID = "root",
        Password = "root",
        Database = "student"
    };
    MySqlConnection mySqlConnection;

    void Start()
    {
    	//连接
        mySqlConnection = new MySqlConnection(mySqlConnectionString.ConnectionString);
        try
        {
            mySqlConnection.Open();
        }
        catch (MySqlException e)
        {
        	//打印连接异常信息
            Debug.Log(string.Format("错误信息:{0},\n错误编码:{1}", e.Message, e.Number));
        }

    }

    private void OnGUI()
    {
        if (GUILayout.Button("查询"))
        {
            if (mySqlConnection.State == ConnectionState.Open)
            {
                MySqlCommand command = new MySqlCommand("select * from new_table", mySqlConnection);
                MySqlDataReader reader = command.ExecuteReader();
                while (reader.Read())
                {
                    var info = (reader[0], reader[1], reader[2]).ToString();
                    Debug.Log(info);
                }
                reader.Close();
            }
        }

        if (GUILayout.Button("插入"))
        {
            if (mySqlConnection.State == ConnectionState.Open)
            {
                string sql = "INSERT INTO new_table(id,name,age) Values (7,'赤色','65')";
                MySqlCommand insertCommand = new MySqlCommand(sql, mySqlConnection);
                Debug.Log(insertCommand.ExecuteNonQuery());
            }
        }

        if (GUILayout.Button("更新"))
        {
            if (mySqlConnection.State == ConnectionState.Open)
            {
                string sql = "UPDATE new_table SET name='赤',age=12 WHERE id=7";
                MySqlCommand command = new MySqlCommand(sql, mySqlConnection);
                Debug.Log(command.ExecuteNonQuery());
            }
        }

        if (GUILayout.Button("删除"))
        {
            if (mySqlConnection.State == ConnectionState.Open)
            {
                string sql = "DELETE FROM new_table WHERE id=7";
                MySqlCommand command = new MySqlCommand(sql, mySqlConnection);
                Debug.Log(command.ExecuteNonQuery());
            }
        }

        if (GUILayout.Button("查询表中首个id"))
        {
            if (mySqlConnection.State == ConnectionState.Open)
            {
                string sql = "SELECT * FROM new_table";
                //sql = "INSERT INTO new_table(id,name,age) Values (7,'赤色','65')";
                //sql = "UPDATE new_table SET name='赤',age=12 WHERE id=7";
                //sql = "DELETE FROM new_table WHERE id=7";
                MySqlCommand command = new MySqlCommand(sql, mySqlConnection);
                object result = command.ExecuteScalar();
                Debug.Log(result);
            }
        }
    }

    private void OnDestroy()
    {
        if (mySqlConnection != null)
            mySqlConnection.Close();
    }

}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值