Unity connects to MySQL and reads table data implementation code

Unity connects to MySQL and reads table data implementation code

The table is as follows:

theTableForm

Code when Unity reads and calls:

0

1

If you want to view another table in the database, instead of using Table[1] directly, you need to change the table name in SELECT * from <?>

Calling different tables
Successful call

Code:

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()
    {
        //Database login data string conStr = "server=localhost;User Id = root;password=123456;Database=gamerdata;charset=utf8";

        //Establish connection //Call MySqlConnection while instantiating, pass in parameters //I personally think the parameters here are directly input in CMD, the string format is directly similar to typing it into cmd MySqlConnection myCon = new MySqlConnection(conStr);

        //Open the connection myCon.Open();

        //Insert data, where useraccount is the table name and the table format is in brackets/*
        //The comment here is because you cannot add values ​​with the same primary key MySqlCommand myCmd = new MySqlCommand("insert into useraccount(id,nickname,password) values ​​(4,'list','testList')", myCon);
        if (myCmd.ExecuteNonQuery() > 0)
        {
            Debug.Log("Query Success!");
        }
        */

        //Query data 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();
    }
}

This is the end of this article about how to read tables when Unity connects to MySQL. For more information about how to read table content when Unity connects to MySQL, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • Solve the Unity urp cascade shadow seam problem
  • Detailed explanation of Unity ground detection solution
  • Detailed comparison and testing of Mask and RectMask2D components in Unity

<<:  Detailed explanation of the sticky position attribute in CSS

>>:  12 Javascript table controls (DataGrid) are sorted out

Recommend

Detailed tutorial on installing VirtualBox and Ubuntu 16.04 under Windows system

1. Software Introduction VirtualBox VirtualBox is...

CSS Sticky Footer Implementation Code

This article introduces the CSS Sticky Footer imp...

React's transition from Class to Hooks

Table of contents ReactHooks Preface WhyHooks? Fo...

How to implement scheduled automatic backup of MySQL under CentOS7

The happiest thing that happens in a production e...

Zabbix configuration DingTalk alarm function implementation code

need Configuring DingTalk alarms in Zabbix is ​​s...

Detailed explanation of the calculation method of flex-grow and flex-shrink in flex layout

Flex(彈性布局) in CSS can flexibly control the layout...

Ten important questions for learning the basics of Javascript

Table of contents 1. What is Javascript? 2. What ...

CentOS8 - bash: garbled characters and solutions

This situation usually occurs because the Chinese...

Detailed example of using typescript to encapsulate axios in Vue3

This axios package is used in the vue3 demo. For ...

In-depth explanation of JavaScript this keyword

Table of contents 1. Introduction 2. Understand t...

Case study of dynamic data binding of this.$set in Vue

I feel that the explanation of this.$set on the I...

About the pitfalls of implementing specified encoding in MySQL

Written in front Environment: MySQL 5.7+, MySQL d...

MySQL database aggregate query and union query operations

Table of contents 1. Insert the queried results 2...

How to try to add sticky effect to your CSS

Written in front I don’t know who first discovere...

Solution to the problem that Navicat cannot remotely connect to MySql server

The solution to the problem that Navicat cannot r...