溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

Unity連接MySQL并讀取表格數(shù)據(jù)的實(shí)現(xiàn)方法

發(fā)布時(shí)間:2021-06-21 10:14:44 來源:億速云 閱讀:962 作者:chen 欄目:開發(fā)技術(shù)

這篇文章主要介紹“Unity連接MySQL并讀取表格數(shù)據(jù)的實(shí)現(xiàn)方法”,在日常操作中,相信很多人在Unity連接MySQL并讀取表格數(shù)據(jù)的實(shí)現(xiàn)方法問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對(duì)大家解答”Unity連接MySQL并讀取表格數(shù)據(jù)的實(shí)現(xiàn)方法”的疑惑有所幫助!接下來,請(qǐng)跟著小編一起來學(xué)習(xí)吧!

表格如下:

Unity連接MySQL并讀取表格數(shù)據(jù)的實(shí)現(xiàn)方法

在Unity讀取并調(diào)用時(shí)的代碼:

Unity連接MySQL并讀取表格數(shù)據(jù)的實(shí)現(xiàn)方法

Unity連接MySQL并讀取表格數(shù)據(jù)的實(shí)現(xiàn)方法

而如果想要查看該數(shù)據(jù)庫中的另一個(gè)表,不是直接使用Table[1],而是需要更改SELECT * from <?>的表名

Unity連接MySQL并讀取表格數(shù)據(jù)的實(shí)現(xiàn)方法
Unity連接MySQL并讀取表格數(shù)據(jù)的實(shí)現(xiàn)方法

代碼:

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()
    {
        //數(shù)據(jù)庫登錄數(shù)據(jù)
        string conStr = "server=localhost;User Id = root;password=123456;Database=gamerdata;charset=utf8";

        //建立連接
        //實(shí)例化的同時(shí)調(diào)用MySqlConnection,傳入?yún)?shù)
        //這里的傳入?yún)?shù)個(gè)人認(rèn)為是CMD里面的直接輸入了,string格式直接類似手敲到cmd里面
        MySqlConnection myCon = new MySqlConnection(conStr);

        //打開連接
        myCon.Open();

        //插入數(shù)據(jù),其中useraccount為表名,括號(hào)內(nèi)為表的格式
        /*
        //此處注釋是因?yàn)椴荒芴砑酉嗤麈I的值
        MySqlCommand myCmd = new MySqlCommand("insert into useraccount(id,nickname,password) values (4,'list','testList')", myCon);
        if (myCmd.ExecuteNonQuery() > 0)
        {
            Debug.Log("Query Success!");
        }
        */

        //查詢數(shù)據(jù)
        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();
    }
}

到此,關(guān)于“Unity連接MySQL并讀取表格數(shù)據(jù)的實(shí)現(xiàn)方法”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實(shí)踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識(shí),請(qǐng)繼續(xù)關(guān)注億速云網(wǎng)站,小編會(huì)繼續(xù)努力為大家?guī)砀鄬?shí)用的文章!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI