Unity连接MySQL并读取表格数据的实现代码
作者:1eonleonChan 时间:2024-01-19 05:20:30
表格如下:
在Unity读取并调用时的代码:
而如果想要查看该数据库中的另一个表,不是直接使用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();
}
}
来源:https://blog.csdn.net/weixin_46840974/article/details/117914326
标签:Unity,MySQL,数据
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Python使用struct库的用法小结
2023-04-29 18:42:53
javascript自定义加载loading效果
2024-04-27 15:23:14
![](https://img.aspxhome.com/file/2023/0/135500_0s.png)
ThinkPHP实现将本地文件打包成zip下载
2024-05-11 09:48:29
python 采用paramiko 远程执行命令及报错解决
2021-01-23 19:17:16
深入讨论Python函数的参数的默认值所引发的问题的原因
2022-08-03 00:54:31
用Python实现数据筛选与匹配实例
2023-01-13 15:31:42
![](https://img.aspxhome.com/file/2023/1/112411_0s.jpg)
python 获得任意路径下的文件及其根目录的方法
2022-02-02 17:32:15
ASP常见的保留字整理(变量与表名注意不能用)
2013-06-01 19:58:01
使用OpenCV实现人脸图像卡通化的示例代码
2023-01-03 13:38:49
![](https://img.aspxhome.com/file/2023/7/82527_0s.jpg)
python的字典和集合你了解吗
2022-12-13 11:28:44
![](https://img.aspxhome.com/file/2023/0/65550_0s.png)
树型结构列出指定目录里所有文件的PHP类
2023-11-17 04:49:04
pycharm下配置pyqt5的教程(anaconda虚拟环境下+tensorflow)
2021-07-02 16:53:41
![](https://img.aspxhome.com/file/2023/2/128342_0s.png)
10分钟教你本地配置多个git ssh连接的方法
2022-03-05 04:44:53
![](https://img.aspxhome.com/file/2023/3/123733_0s.png)
js实现图片加载淡入淡出效果
2024-04-22 22:41:15
python统计字符串中指定字符出现次数的方法
2022-11-17 11:37:46
Golang实现WebSocket服务的项目实践
2024-04-28 09:15:51
使用python绘制温度变化雷达图
2021-06-23 05:44:02
![](https://img.aspxhome.com/file/2023/5/121625_0s.jpg)
python通过pil将图片转换成黑白效果的方法
2021-07-17 22:13:16
MySql nion与Limit查询介绍
2024-01-23 15:53:52
![](https://img.aspxhome.com/file/2023/9/101349_0s.png)
详解python脚本自动生成需要文件实例代码
2023-10-04 08:44:10