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

吾爱主题 阅读:140 2024-04-02 08:03:22 评论:0

表格如下:

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

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


代码:

?
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

可以去百度分享获取分享代码输入这里。
声明

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

【腾讯云】云服务器产品特惠热卖中
搜索
标签列表
    关注我们

    了解等多精彩内容