Unity連接MySQL并讀取表格數(shù)據(jù)的實(shí)現(xiàn)代碼
表格如下:

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


而如果想要查看該數(shù)據(jù)庫中的另一個(gè)表,不是直接使用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()
{
//數(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í)讀取表格的方式的文章就介紹到這了,更多相關(guān)Unity連接MySQL讀取表格內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
根據(jù)mysql慢日志監(jiān)控SQL語句執(zhí)行效率
根據(jù)mysql慢日志監(jiān)控SQL語句執(zhí)行效率 啟用MySQL的log-slow-queries(慢查詢記錄)。2012-11-11
mysql5.7.33誤刪除ibdata文件找回?cái)?shù)據(jù)的方法
這篇文章主要介紹了mysql5.7.33誤刪除ibdata文件找回?cái)?shù)據(jù)的方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-03-03
mysql8.0.11 winx64手動(dòng)安裝配置教程
這篇文章主要為大家詳細(xì)介紹了mysql8.0.11 winx64手動(dòng)安裝配置教程,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-05-05

