服务器之家:专注于服务器技术及软件下载分享
分类导航

Mysql|Sql Server|Oracle|Redis|MongoDB|PostgreSQL|Sqlite|DB2|mariadb|Access|数据库技术|

服务器之家 - 数据库 - Mysql - Unity连接MySQL并读取表格数据的实现代码

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

2021-08-20 16:501eonleonChan Mysql

本文给大家介绍Unity连接MySQL并读取表格数据的实现代码,实例化的同时调用MySqlConnection,传入参数,这里的传入参数个人认为是CMD里面的直接输入了,string格式直接类似手敲到cmd里面,完整代码参考下本文

表格如下:

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

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

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

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

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

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

代码:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
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();
    }
}

到此这篇关于unity连接mysql时读取表格的方式的文章就介绍到这了,更多相关unity连接mysql读取表格内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!

原文链接:https://blog.csdn.net/weixin_46840974/article/details/117914326

延伸 · 阅读

精彩推荐