表格如下:
在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
版权声明:本文(即:原文链接:https://www.qin1qin.com/catagory/6848/)内容由互联网用户自发投稿贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 630367839@qq.com 举报,一经查实,本站将立刻删除。