欢迎来到IDC91站长网!
解决服务器各种技术问题,加微信uecomzsr

MySQL

当前位置: IDC91 > 数据库 > MySQL

Unity连接MySQL并读取表格数据的实现代码

时间:2022-05-30 13:50:37|栏目:MySQL|点击:

表格如下:

theTableForm

在Unity读取并调用时的代码:

0

1

而如果想要查看该数据库中的另一个表,不是直接使用Table[1],而是需要更改SELECT * from <?>的表名

调用不同表
成功调用

代码:

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

public class getGameUserAccount : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {
        mySqlCon();
    }

    // Update is called once per frame
    void Update()
    {
        
    }

    public void mySqlCon()
    {
        //数据库登录数据
        string conStr = "server=localhost;User Id = root;password=123456;Database=gamerdata;charset=utf8";

        //建立连接
        //实例化的同时调用MySqlConnection,传入参数
        //这里的传入参数个人认为是CMD里面的直接输入了,string格式直接类似手敲到cmd里面
        MySqlConnection myCon = new MySqlConnection(conStr);

        //打开连接
        myCon.Open();

        //插入数据,其中useraccount为表名,括号内为表的格式
        /*
        //此处注释是因为不能添加相同主键的值
        MySqlCommand myCmd = new MySqlCommand("insert into useraccount(id,nickname,password) values (4,'list','testList')", myCon);
        if (myCmd.ExecuteNonQuery() > 0)
        {
            Debug.Log("Query Success!");
        }
        */

        //查询数据
        string selStr = "select * from useraccount";
        MySqlCommand mySelect = new MySqlCommand(selStr, myCon);

        DataSet ds = new DataSet();

        try
        {
            MySqlDataAdapter da = new MySqlDataAdapter(selStr, myCon);
            da.Fill(ds);
            
            Debug.Log(ds.Tables[0].Rows[0][0]);
            Debug.Log(ds.Tables[0].Rows[0][1]);
            Debug.Log(ds.Tables[0].Rows[0][2]);
            Debug.Log(ds.Tables[0].Rows[0][3]);

            //Table[0].Rows[0][0]
            Debug.Log("Query Success!");
        }
        catch (Exception e)
        {
            throw new Exception("SQL:" + selStr + "n" + e.Message.ToString());
        }

        myCon.Close();
    }
}

上一篇:Mysql数据库值的添加、修改、删除及清空操作实例

栏    目:MySQL

下一篇:新手入门Mysql--sql执行过程

本文标题:Unity连接MySQL并读取表格数据的实现代码

本文地址:https://idc91.com/shujuku/3567.html

广告投放 | 联系我们 | 免责申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

Copyright © 2023 IDC91.COM 版权所有晋ICP备17006296号