日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

unity and MySql

發(fā)布時(shí)間:2023/12/20 编程问答 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 unity and MySql 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

摘要:數(shù)據(jù)庫的圖片以及內(nèi)容如圖:

1.數(shù)據(jù)庫的封裝

using System;
using System.Linq;
using System.Data;
using UnityEngine;
using System.Text;
using System.Collections.Generic;
using MySql.Data.MySqlClient;

?

public class SqlAccess
{
??? public static MySqlConnection dbConnection;
??? //如果只是在本地的話,寫localhost就可以。
??? // static string host = "localhost";?
??? //如果是局域網(wǎng),那么寫上本機(jī)的局域網(wǎng)IP
??? static string host = "localhost";
??? static string id = "root";//可以通過這個(gè)用戶訪問數(shù)據(jù)庫
??? static string pwd = "013287";//創(chuàng)建鏈接時(shí)候的密碼
??? static string database = "man";//數(shù)據(jù)庫的名字

??? public SqlAccess()
??? {
??????? OpenSql();
??? }

??? public static void OpenSql()
??? {

??????? try
??????? {
??????????? string connectionString = string.Format("Server = {0};port={4};Database = {1}; User ID = {2}; Password = {3};", host, database, id, pwd, "3306");//鏈接數(shù)據(jù)庫的字段
??????????? dbConnection = new MySqlConnection(connectionString);
??????????? dbConnection.Open();
??????? }
??????? catch (Exception e)
??????? {
??????????? throw new Exception("服務(wù)器連接失敗,請(qǐng)重新檢查是否打開MySql服務(wù)。" + e.Message.ToString());

??????? }

??? }

??? public DataSet CreateTable(string name, string[] col, string[] colType) //創(chuàng)建表
??? {
??????? if (col.Length != colType.Length)
??????? {

??????????? throw new Exception("columns.Length != colType.Length");

??????? }

??????? string query = "CREATE TABLE " + name + " (" + col[0] + " " + colType[0];

??????? for (int i = 1; i < col.Length; ++i)
??????? {

??????????? query += ", " + col[i] + " " + colType[i];

??????? }

??????? query += ")";

??????? return ExecuteQuery(query);
??? }

??? public DataSet CreateTableAutoID(string name, string[] col, string[] colType)
??? {
??????? if (col.Length != colType.Length)
??????? {

??????????? throw new Exception("columns.Length != colType.Length");

??????? }

??????? string query = "CREATE TABLE " + name + " (" + col[0] + " " + colType[0] + " NOT NULL AUTO_INCREMENT";

??????? for (int i = 1; i < col.Length; ++i)
??????? {

??????????? query += ", " + col[i] + " " + colType[i];

??????? }

??????? query += ", PRIMARY KEY (" + col[0] + ")" + ")";

??????? Debug.Log(query);

??????? return ExecuteQuery(query);
??? }

??? //插入一條數(shù)據(jù),包括所有,不適用自動(dòng)累加ID。
??? public DataSet InsertInto(string tableName, string[] values)
??? {

??????? string query = "INSERT INTO " + tableName + " VALUES (" + "'" + values[0] + "'";

??????? for (int i = 1; i < values.Length; ++i)
??????? {

??????????? query += ", " + "'" + values[i] + "'";

??????? }

??????? query += ")";

??????? Debug.Log(query);
??????? return ExecuteQuery(query);

??? }

??? //插入部分ID
??? public DataSet InsertInto(string tableName, string[] col, string[] values)
??? {

??????? if (col.Length != values.Length)
??????? {

??????????? throw new Exception("columns.Length != colType.Length");

??????? }

??????? string query = "INSERT INTO " + tableName + " (" + col[0];
??????? for (int i = 1; i < col.Length; ++i)
??????? {

??????????? query += ", " + col[i];

??????? }

??????? query += ") VALUES (" + "'" + values[0] + "'";
??????? for (int i = 1; i < values.Length; ++i)
??????? {

??????????? query += ", " + "'" + values[i] + "'";

??????? }

??????? query += ")";

??????? Debug.Log(query);
??????? return ExecuteQuery(query);

??? }

??? public DataSet SelectWhere(string tableName, string[] items, string[] col, string[] operation, string[] values)
??? {

??????? if (col.Length != operation.Length || operation.Length != values.Length)
??????? {

??????????? throw new Exception("col.Length != operation.Length != values.Length");

??????? }

??????? string query = "SELECT " + items[0];

??????? for (int i = 1; i < items.Length; ++i)
??????? {

??????????? query += ", " + items[i];

??????? }

??????? query += " FROM " + tableName + " WHERE " + col[0] + operation[0] + "'" + values[0] + "' ";

??????? for (int i = 1; i < col.Length; ++i)
??????? {

??????????? query += " AND " + col[i] + operation[i] + "'" + values[0] + "' ";

??????? }

??????? return ExecuteQuery(query);

??? }

??? public DataSet UpdateInto(string tableName, string[] cols, string[] colsvalues, string selectkey, string selectvalue)
??? {

??????? string query = "UPDATE " + tableName + " SET " + cols[0] + " = " + colsvalues[0];

??????? for (int i = 1; i < colsvalues.Length; ++i)
??????? {

??????????? query += ", " + cols[i] + " =" + colsvalues[i];
??????? }

??????? query += " WHERE " + selectkey + " = " + selectvalue + " ";

??????? return ExecuteQuery(query);
??? }

??? public DataSet Delete(string tableName, string[] cols, string[] colsvalues)
??? {
??????? string query = "DELETE FROM " + tableName + " WHERE " + cols[0] + " = " + colsvalues[0];

??????? for (int i = 1; i < colsvalues.Length; ++i)
??????? {

??????????? query += " or " + cols[i] + " = " + colsvalues[i];
??????? }
??????? Debug.Log(query);
??????? return ExecuteQuery(query);
??? }

??? public void Close()
??? {

??????? if (dbConnection != null)
??????? {
??????????? dbConnection.Close();
??????????? dbConnection.Dispose();
??????????? dbConnection = null;
??????? }

??? }

??? public static DataSet ExecuteQuery(string sqlString)
??? {
??????? if (dbConnection.State == ConnectionState.Open)
??????? {
??????????? DataSet ds = new DataSet();
??????????? try
??????????? {

??????????????? MySqlDataAdapter da = new MySqlDataAdapter(sqlString, dbConnection); //把修改好的數(shù)據(jù)通過適配器改回?cái)?shù)據(jù)庫,及修改數(shù)據(jù)庫
??????????????? da.Fill(ds);

??????????? }
??????????? catch (Exception ee)
??????????? {
??????????????? throw new Exception("SQL:" + sqlString + "/n" + ee.Message.ToString());
??????????? }
??????????? finally
??????????? {
??????????? }
??????????? return ds;
??????? }
??????? return null;
??? }
}

2.數(shù)據(jù)庫與unity的鏈接

這個(gè)腳本需要掛載在一個(gè)激活的物體上

using System;
using UnityEngine;
using System.Data;
using System.Collections;

public class NewBehaviourScript : MonoBehaviour {

??? string Error = null;
??? void Start()
??? {
??????? try
??????? {

??????????? SqlAccess sql = new SqlAccess(); //獲取對(duì)封裝好的數(shù)據(jù)庫的引用

??????????? //?? sql.CreateTableAutoID("people", new string[] { "idcard", "age", "name" }, new string[] {? "idcard", "age", "name"});
??????????? //sql.CreateTable("momo",new string[]{"name","qq","email","blog"}, new string[]{"text","text","text","text"});
??????????? // sql.InsertInto("people", new string[] { "idcard", "age", "name" }, new string[] { "6789", "22", "qiang" });
??????????? //? sql.InsertInto("people", new string[] { "idcard", "age", "name"}, new string[] { "5", "age2", "name2" });

??????????? DataSet ds = sql.SelectWhere("people", new string[] { "idcard", "age", "name" }, new string[] { "idcard" }, new string[] { "=" }, new string[] { "520" }); //把返回的數(shù)據(jù)集付給ds,此時(shí)的修改已經(jīng)在封裝好的sqlAccess中改回?cái)?shù)據(jù)庫了,此時(shí)通過ds進(jìn)行UI顯示或輸出等
??????????? if (ds != null)
??????????? {

??????????????? DataTable table = ds.Tables[0];

??????????????? foreach (DataRow row in table.Rows)
??????????????? {
??????????????????? foreach (DataColumn column in table.Columns)
??????????????????? {
??????????????????????? Debug.Log(row[column]);
??????????????????? }
??????????????? }
??????????? }
??????????? sql.Close();
??????? }
??????? catch (Exception e)
??????? {
??????????? Error = e.Message;
??????? }

??? }

??? // Update is called once per frame
??? void OnGUI()
??? {

??????? if (Error != null)
??????? {
??????????? GUILayout.Label(Error);
??????? }

??? }
}

轉(zhuǎn)載于:https://www.cnblogs.com/xwwFrank/p/4417840.html

總結(jié)

以上是生活随笔為你收集整理的unity and MySql的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。