Unity 3D 连接Mysql数据库

要想使用Unity直接连接数据库需要以下几个动态库

连接数据库前

将关于数据库的方法进行封装:MySqlConnection
.cs


using UnityEngine;
using System;
using System.Data;
using System.Collections;
using MySql.Data.MySqlClient;
using MySql.Data;
using System.IO;
public class SqlAccess
{
public static MySqlConnection dbConnection;
//如果只是在本地的话,写localhost就可以。
// static string host = "localhost";
//如果是局域网,那么写上本机的局域网IP
static string host = "127.0.0.1";
static string id = "root";
static string pwd = "123456";
static string database = "test";

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");
dbConnection = new MySqlConnection(connectionString);
dbConnection.Open();
}catch (Exception e)
{
throw new Exception("服务器连接失败,请重新检查是否打开MySql服务。" + e.Message.ToString());
}
}

public DataSet CreateTable (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];
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);
}

//插入一条数据,包括所有,不适用自动累加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 SelectAll(string Name)
{
string query ="select * from "+" "+Name;
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;
}
}

  //执行sql语句
public static DataSet ExecuteQuery(string sqlString)
{
if(dbConnection.State==ConnectionState.Open)
{
DataSet ds = new DataSet(); //表的集合
try
{
MySqlDataAdapter da = new MySqlDataAdapter(sqlString, dbConnection);
da.Fill(ds);
}
catch (Exception ee)
{
throw new Exception("SQL:" + sqlString + "/n" + ee.Message.ToString());
}
finally
{
}
return ds;
}
return null;
}
}

连接数据库的代码如下:NewBehaviourScript.cs


using UnityEngine;
using System;
using System.Data;
using System.Collections;
using MySql.Data.MySqlClient;
using MySql.Data;
using System.IO;

public class NewBehaviourScript : MonoBehaviour
{
string Error = null;

void Start ()
{
try {
SqlAccess sql = new SqlAccess (); //创建SqlAccess 对象
sql.CreateTableAutoID("stu",new string[]{"id","name","qq"}, new string[]{"int","text","text"}); //主键自增长
//sql.CreateTable("stu",new string[]{"name","qq"}, new string[]{"text","text"}); //建普通表
sql.InsertInto("stu",new string[]{"name","qq"},new string[]{"aa","12345678"});//插入数据
sql.InsertInto("stu",new string[]{"name","qq"},new string[]{"bb","98765432"});
DataSet ds = sql.SelectAll ("stu");
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);
}
}
}

将脚本NewBehaviourScript.cs放到MainCamera上并运行:

在Unity中已执行,那就去数据库中查看是否产生:

对比发现表已经产生,查看数据:

没问题了,其它操作也可测试了。

时间: 2024-11-10 17:59:51

Unity 3D 连接Mysql数据库的相关文章

Unity3D连接MySQL数据库所需要的DLL

Unity3D连接MySQL数据库所需要的DLL 纠结了很久终于搞定 需要一下DLL D:\Program Files\Unity\Editor\Data\Mono\lib\mono\unity 目录下的 I18N.dll   I18N.West.dll   I18N.CJK.dll D:\Program Files\Unity\Editor\Data\Mono\lib\mono\2.0 目录下的 System.Data.dll  System.Drawing.dll 还需要 mysql5.0版

两种方法连接MySql数据库

1.用MySQLDriverCS连接MySQL数据库 先下载和安装MySQLDriverCS,在安装文件夹下面找到MySQLDriver.dll,然后将MySQLDriver.dll添加引用到项目中. using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Data.Odbc; using System.Drawing; using S

Unity3D_WebPlayer_PHP连接MySQL数据库_1_php服务器配置

前面出了一个 Unity3D连接MYSQL数据库的教程,有网友问我关于Webplayer下载如何连接MYSQL数据库,这个出一个系列的教程 1,首先处理好虚拟主机下MYSQL数据库初始化 MYSQL管理器下 创建--数据库--表SQL 脚本 CREATE TABLE `getfh_15188775_linmdb`.`u_table` ( `pname` TEXT NOT NULL , `ppwd` TEXT NOT NULL ) 分别插入3条数据 INSERT INTO `getfh_15188

Unity3D 连接MySQL数据库笔记4-Unity3d代码

新建Unity3D项目 新建一个 Plugins文件夹 把所需要的DLL放到 Plugins中 新建C#脚本 命名为ToMysql.cs 不多说了 ,直接上代码,很简单  有注释 ,大家一看就明白了 using UnityEngine; using System; using System.Collections; using System.Data; using MySql.Data.MySqlClient; public class ToMysql : MonoBehaviour { pub

mybatis连接mysql数据库插入中文乱码

对于mysql数据库的乱码问题,有两中情况: 1. mysql数据库编码问题(建库时设定). 2. 连接mysql数据库的url编码设置问题. 对于第一个问题,目前个人发现只能通过重新建库解决,建库的时候,选择UTF-8字符集.我试过修改现有数据库字符集为UFT -8,但是根本不起作用,插入的中文仍然乱码(中文显示成:???).重建库时选择字符集为UTF-8之后,中文正常显示了. 对于第二个问题,是这样 的情况:我建库时设置了数据库默认字符集为UTF-8,通过mysql workbench直接插

用MyEclipse10.0远程连接Mysql数据库服务器

说明:本文档所有的操作均在满足以下条件的情况下操作, A.远程Linux服务器已经安装好MySQL数据库 B.本地电脑可以ping通远程服务器 C.已经成功安装了Myeclipse 一.下载mysql-connector-java-3.1.14-bin.jar 这个是连接mysql数据库的驱动器. 下载地址: http://dev.mysql.com/downloads/connector/j/5.0.html 也可以到官方网站下载,网址如下:www.mysql.com 二.在Myeclipse

Linux系统Java连接Mysql数据库

Linux系统下Java连接Mysql数据库方法与Windows下类似,具体步骤如下: 下载JDBC(驱动):mysql-connector-java-5.1.37.tar.gz 解压下载的数据包:tar -zxvf mysql-connector-java-5.1.37.tar.gz 解压后,mysql-connector-java-5.1.29-bin.jar位于mysql-connector-java-5.1.29目录下. 配置解压后,mysql-connector-java-5.1.29

C#连接MySql数据库的方法

1.要连接MySql数据库必须首先下载MySql的连接.net的文件, 文件下载地址为http://download.csdn.net/detail/xiaoliu123586/91455792.解压缩刚才下载的mysql-connector-net-6.6.6-noinstall.zip文件,里面有几个版本选择,在这里我选V4, 选中V4.0中所有文件几个文件,然后添加到C#项目的引用中,然后就可以编写程序进行数据库的操作了. 3.数据库操作代码 InsertMySql.cs 1 using

hibernate简单连接mysql数据库配置

使用hibernate连接mysql数据库 1:项目搭建好之后,在lib包中添加必要的jar包,和mysql数据库驱动jar包: jar包可以在hibernate的下载包(hibernate3.3.2.GA)中找到,这里所需要的jar包是: hibernate3.jar,lib/required目录下的所有jar包; 连接数据库所需要的jar包:mysql-connector-java-5.1.7-bin.jar; 如图: 2:jar包引入后编写实体类及映射文件: 实体类是xx.java文件;映