unity+mysql-connector_unity中连接MySQL数据库的方法封装

本文介绍如何在Unity中使用mysql-connector连接并操作MySQL数据库。内容包括初始化数据库连接字符串,测试连接,创建表,插入、更新、删除数据,以及查询数据的详细方法封装。
摘要由CSDN通过智能技术生成

usingMySql.Data.MySqlClient;usingSystem;usingSystem.Data;usingSystem.Collections;public class DatabaseManager : CSingleton{protected static string m_databaseIP = "localhost"; //IP地址

protected static string m_databasePort = "3306"; //端口号

protected static string m_userID = "root"; //MySQL数据库用户名

protected static string m_password = "123456"; //MySQL登陆密码

protected static string m_databaseName = "crh_emergency"; //链接的数据库的库名

protected static string m_connectionString; //数据库连接字符串

///

///测试是否链接上数据库///

///

public boolTestConnection()

{bool isConnected = true;//发送数据库连接字段 创建连接通道

using (MySqlConnection connection = newMySqlConnection(m_connectionString))

{try{//打开连接通道

connection.Open();

}catch(MySqlException E)

{//如果有异常 则连接失败

isConnected = false;throw newException(E.Message);

}finally{//关闭连接通道

connection.Close();

}

}returnisConnected;

}///

///利用字符串组拼方式来编写数据库的连接///

public static voidInit()

{

m_connectionString= string.Format("Server = {0}; port = {1}; Database = {2}; User ID = {3}; Password = {4}; Pooling=true; Charset = utf8;", m_databaseIP, m_databasePort, m_databaseName, m_userID, m_password);

}///

///创建表///

/// 表名

/// 字段名

/// 字段类型

///

public static int CreateTable(string name, string[] cols, string[] colType)

{if (cols.Length !=colType.Length)

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

}string query = "CREATE TABLE" + name + "(" + cols[0] + " " + colType[0];for (int i = 1; i < cols.Length; ++i)

{

query+= "," + cols[i] + " " +colType[i];

}

query+= ")";returnExecuteNonQuery(query);

}///

///创建表默认ID自动增长///

/// 表名

/// 字段名

/// 字段类型

///

public static int CreateTableAutoID(string name, string[] cols, string[] colType)

{if (cols.Length !=colType.Length)

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

}string query = "CREATE TABLE" + name + "(" + cols[0] + " " + colType[0] + "NOT NULL AUTO_INCREMENT";for (int i = 1; i < cols.Length; ++i)

{

query+= "," + cols[i] + " " +colType[i];

}

query+= ", PRIMARY KEY (" + cols[0] + ")" + ")";returnExecuteNonQuery(query);

}///

///插入一条数据,包括所有,不适用自动累加ID。///

/// 表名

///

///

public static int 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+= ")";returnExecuteNonQuery(query);

}///

///插入部分ID///

/// 表名

///

///

///

public static int InsertInto(string tableName, string[] cols, string[] values)

{if (cols.Length !=values.Length)

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

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

{

query+= "," +cols[i];

}

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

{

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

}

query+= ")";returnExecuteNonQuery(query);

}///

///当指定字段满足一定条件时,更新指定字段的数据///例如更新在user这个表中字段名为userAccount的值等于10086时,将对应userPwd字段的值改成newMd5SumPassword///("users", new string[] { "userPwd" }, new string[] { newMd5SumPassword }, "userAccount", "10086")///

/// 表名

/// 字段

/// 字段值

/// 指定的字段

/// 指定字段满足的条件

///

public static int UpdateInto(string tableName, string[] cols, string[] colsValues, string selectKey, stringselectValue)

{string query = "UPDATE" + tableName + "SET" + cols[0] + "=" + "'" + colsValues[0] + "'";for (int i = 1; i < colsValues.Length; ++i)

{

query+= "," + cols[i] + "=" &

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值