调用MySqlHelper内容:
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 |
string[] col = new string [3] { "id", "name", "age" }; string[] colType = new string [3] { "int(5) primary key", "varchar(8)", "int(5)" }; string[] values = new string [3] {"1", "'a'", "28" }; // Use this for initialization void Start () { MySqlHelper sql = new MySqlHelper (); sql.Create( "tableTest1", col, colType); sql.Insert( "tableTest1", values); sql.Update( "tableTest1", new string[] { "age" }, new string[] { "18" }, "id" , "1"); sql.Delete( "tableTest1", new string[] { "age" }, new string[] { "18" }); // 获取查询结果保存到DataSet变量 DataSet ds = sql.Select("tableTest1" , "age", "id = 1"); if (ds != null ) { // 创建临时表保存查询结果 DataTable table = ds.Tables[0]; // 遍历查询结果 并输出 foreach (DataRow row in table.Rows) { foreach (DataColumn column in table.Columns) { Debug.Log(row[column]); } } } } |
MySqlHelper内容:
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 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 |
using UnityEngine; using System; using System.Data; using System.Collections; using MySql.Data.MySqlClient; using MySql.Data; using System.IO; public class MySqlHelper { public static MySqlConnection dbConnection; //如果只是在本地的话,写localhost就可以。 // static string host = "localhost"; //如果是局域网,那么写上本机的局域网IP static string host = "qdm16206948.my3w.com" ; static string id = "qdm16206948" ; static string pwd = "woaijia345" ; static string database = "qdm16206948_db" ; publicMySqlHelper() { Initialize(); } /// <summary> /// 新建并打开连接 /// </summary> public static void Initialize() { 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()); } } /// <summary> /// 创建表 /// 参数:表名称、属性列、 属性列类型 /// </summary> public DataSet Create (string _TableName, string[] _ColsName, string [] _ColsType) { if (_ColsName.Length != _ColsType.Length) { throw new Exception ( "columns.Length != colType.Length" ); } try{ string query = "CREATE TABLE " + _TableName + " (" + _ColsName[0] + " " + _ColsType[0]; for (int i = 1; i < _ColsName.Length; ++i) { query += ", " + _ColsName[i] + " " + _ColsType[i]; } query += ")"; Debug.Log("~~创建成功~~" ); return ExecuteQuery(query); } catch { return null ; } } /// <summary> /// 创建表 自动ID /// 参数:表名称、属性列、 属性列类型 /// </summary> public DataSet CreateAutoID (string _TableName, string[] _ColsName, string [] _ColsType) { if (_ColsName.Length != _ColsType.Length) { throw new Exception ( "columns.Length != colType.Length" ); } try { string query = "CREATE TABLE " + _TableName + " (" + _ColsName[0] + " " + _ColsType[0] + " NOT NULL AUTO_INCREMENT"; for (int i = 1; i < _ColsName.Length; ++i) { query += ", " + _ColsName[i] + " " + _ColsType[i]; } query += ", PRIMARY KEY (" + _ColsName[0] + ")" + ")"; Debug.Log("~~创建成功~~" ); return ExecuteQuery(query); } catch { return null ; } } /// <summary> /// 插入数据 /// 参数:表名称、值 /// 特点:不适用自动ID /// </summary> public DataSet Insert (string _TableName, string[] _Values) { try { string query = "INSERT INTO " + _TableName + " VALUES (" + _Values[0]; for (int i = 1; i < _Values.Length; ++i) { query += ", " + _Values[i]; } query += ")"; Debug.Log("~~添加成功~~" ); return ExecuteQuery(query); } catch { Debug.Log("添加失败,请检查错误后重新再试" ); return null ; } } /// <summary> /// 插入数据 /// 参数:表名称、属性列、值 /// 特点:可选择性添加值 /// </summary> public DataSet Insert (string _TableName, string[] _ColsName, string [] _Values) { try { if (_ColsName.Length != _Values.Length) { throw new Exception( "columns.Length != colType.Length" ); } string query = "INSERT INTO " + _TableName + " (" + _ColsName[0]; for (int i = 1; i < _ColsName.Length; ++i) { query += ", " + _ColsName[i]; } query += ") VALUES (" + _Values[0]; for (int i = 1; i < _Values.Length; ++i) { query += ", " + _Values[i]; } query += ")"; Debug.Log("~~添加成功~~" ); return ExecuteQuery(query); } catch { Debug.Log("添加失败,请检查错误后重新再试" ); return null ; } } /// <summary> /// 查询数据 /// 参数:表名称、查询内容 /// </summary> public DataSet Select(string _TableName, string _Select) { try { string query = "select " + _Select + " from " + _TableName; return ExecuteQuery(query); } catch { Debug.Log("查询失败,请检查错误后重新再试" ); return null ; } } /// <summary> /// 查询数据 /// 参数:表名称、查询内容、查询条件 /// </summary> public DataSet Select(string _TableName, string _Select, string _Condition) { try { string query = "select " + _Select + " from " + _TableName + " where " + _Condition; return ExecuteQuery(query); } catch { Debug.Log("查询失败,请检查错误后重新再试" ); return null ; } } /// <summary> /// 修改数据 /// 参数:表名称、准备修改的属性列、准备修改的属性列的值、条件信息属性列、条件信息属性列的值 /// </summary> public DataSet Update(string _TableName, string[] _SetColsName, string[] _SetColsValues, string _ConditionColName, string _ConditionColValue) { try { string query = "UPDATE " + _TableName + " SET " + _SetColsName[0] + " = " + _SetColsValues[0]; for (int i = 1; i < _SetColsValues.Length; ++i) { query += ", " + _SetColsName[i] + " =" + _SetColsValues[i]; } query += " WHERE " + _ConditionColName + " = " + _ConditionColValue + " "; Debug.Log("~~修改成功~~" ); return ExecuteQuery(query); } catch { Debug.Log("修改失败,请检查错误后重新再试" ); return null ; } } /// <summary> /// 删除数据 /// 参数:表名称、属性列、值 /// </summary> public DataSet Delete(string _TableName, string [] _ColsName,string [] _ColsValue) { try { string query = "DELETE FROM " + _TableName + " WHERE " + _ColsName[0] + " = " + _ColsValues[0]; for (int i = 1; i < _ColsValues.Length; ++i) { query += " or " + _ColsName[i] + " = " + _ColsValues[i]; } Debug.Log("~~删除成功~~" ); return ExecuteQuery(query); } catch { Debug.Log("删除失败,请检查错误后重新再试" ); return null ; } } 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); da.Fill(ds); } catch (Exception ee) { throw new Exception( "SQL:" + sqlString + "/n" + ee.Message.ToString()); } return ds; } return null ; } } |
MySqlHelper 头文件下载地址:
链接: http://pan.baidu.com/s/1uaQAi 密码: 3tsy
- 本文固定链接: http://www.u3d8.com/?p=135
- 转载请注明: 网虫虫 在 u3d8.com 发表过