国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 網(wǎng)站 > 建站經(jīng)驗(yàn) > 正文

iOS開(kāi)發(fā):Unity3D 使用C_#語(yǔ)言建立本地?cái)?shù)據(jù)庫(kù)

2019-11-02 15:09:52
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

   首先你需要得到Mono.Data.Sqlite.dll 文件 與System.Data.dll文件。如果你在Mac 操作系統(tǒng)下使用Unity那么很悲劇,找不到這兩個(gè)文件,至少我沒(méi)能找到。后來(lái)我在Windows下的Unity安裝路徑中找到了它。為了方便大家我將這兩個(gè)文件上傳至網(wǎng)盤(pán)中,如果沒(méi)有這兩個(gè)文件的朋友請(qǐng)下載。Unity數(shù)據(jù)庫(kù)文件.zip

  .zip文件下載完畢后直接解壓,然后將Mono.Data.Sqlite.dll 文件 與System.Data.dll文件放在Unity工程中的Assets文件夾中。如下圖所示,兩個(gè)文件已經(jīng)放置在Project視圖當(dāng)中。

iOS開(kāi)發(fā):Unity3D 使用C#語(yǔ)言建立本地?cái)?shù)據(jù)庫(kù) 電腦高手

  Ok ,我們編寫(xiě)C#腳本,原始文章沒(méi)有Unity數(shù)據(jù)庫(kù)更新與刪除的方法,我在這里加上更新與刪除的方法,方便大家開(kāi)發(fā)時(shí)使用。因?yàn)槠鋵?shí)Unity中更新與刪除數(shù)據(jù)庫(kù)也是個(gè)比較重要的功能。

  注意:下面腳本不要綁定在任何游戲對(duì)象身上,大家無(wú)需把它當(dāng)作腳本可以當(dāng)作一個(gè)工具類來(lái)使用。

  [代碼]java代碼:

001 using UnityEngine; 002   003 using System; 004 using System.Collections; 005 using Mono.Data.Sqlite; 006   007 public class DbAccess 008   009 { 010   011 private SqliteConnection dbConnection; 012   013 private SqliteCommand dbCommand; 014   015 private SqliteDataReader reader; 016   017 public DbAccess (string connectionString) 018   019 { 020   021 OpenDB (connectionString); 022   023 } 024 public DbAccess () 025 { 026   027 } 028   029 public void OpenDB (string connectionString) 030   031 { 032 try 033 { 034 dbConnection = new SqliteConnection (connectionString); 035   036 dbConnection.Open (); 037   038 Debug.Log ("Connected to db"); 039 } 040 catch(Exception e) 041 { 042 string temp1 = e.ToString(); 043 Debug.Log(temp1); 044 } 045   046 } 047   048 public void CloseSqlConnection () 049   050 { 051   052 if (dbCommand != null) { 053   054 dbCommand.Dispose (); 055   056 } 057   058 dbCommand = null; 059   060 if (reader != null) { 061   062 reader.Dispose (); 063   064 } 065   066 reader = null; 067   068 if (dbConnection != null) { 069   070 dbConnection.Close (); 071   072 } 073   074 dbConnection = null; 075   076 Debug.Log ("Disconnected from db."); 077   078 } 079   080 public SqliteDataReader ExecuteQuery (string sqlQuery) 081   082 { 083   084 dbCommand = dbConnection.CreateCommand (); 085   086 dbCommand.CommandText = sqlQuery; 087   088 reader = dbCommand.ExecuteReader (); 089   090 return reader; 091   092 } 093   094 public SqliteDataReader ReadFullTable (string tableName) 095   096 { 097   098 string query = "SELECT * FROM " + tableName; 099   100 return ExecuteQuery (query); 101   102 } 103   104 public SqliteDataReader InsertInto (string tableName, string[] values) 105   106 { 107   108 string query = "INSERT INTO " + tableName + " VALUES (" + values[0]; 109   110 for (int i = 1; i < values.Length; ++i) { 111   112 query += ", " + values[i]; 113   114 } 115   116 query += ")"; 117   118 return ExecuteQuery (query); 119   120 } 121   122 public SqliteDataReader UpdateInto (string tableName, string []cols,string []colsvalues,string selectkey,string selectvalue) 123 { 124   125 string query = "UPDATE "+tableName+" SET "+cols[0]+" = "+colsvalues[0]; 126   127 for (int i = 1; i < colsvalues.Length; ++i) { 128   129 query += ", " +cols[i]+" ="+ colsvalues[i]; 130 } 131   132 query += " WHERE "+selectkey+" = "+selectvalue+" "; 133   134 return ExecuteQuery (query); 135 } 136   137 public SqliteDataReader Delete(string tableName,string []cols,string []colsvalues) 138 { 139 string query = "DELETE FROM "+tableName + " WHERE " +cols[0] +" = " + colsvalues[0]; 140   141 for (int i = 1; i < colsvalues.Length; ++i) { 142   143 query += " or " +cols[i]+" = "+ colsvalues[i]; 144 } 145 Debug.Log(query); 146 return ExecuteQuery (query); 147 } 148   149 public SqliteDataReader InsertIntoSpecific (string tableName, string[] cols, string[] values) 150   151 { 152   153 if (cols.Length != values.Length) { 154   155 throw new SqliteException ("columns.Length != values.Length"); 156   157 } 158   159 string query = "INSERT INTO " + tableName + "(" + cols[0]; 160   161 for (int i = 1; i < cols.Length; ++i) { 162   163 query += ", " + cols[i]; 164   165 } 166   167 query += ") VALUES (" + values[0]; 168   169 for (int i = 1; i < values.Length; ++i) { 170   171 query += ", " + values[i]; 172   173 } 174   175 query += ")"; 176   177 return ExecuteQuery (query); 178   179 } 180   181 public SqliteDataReader DeleteContents (string tableName) 182   183 { 184   185 string query = "DELETE FROM " + tableName; 186   187 return ExecuteQuery (query); 188   189 } 190   191 public SqliteDataReader CreateTable (string name, string[] col, string[] colType) 192   193 { 194   195 if (col.Length != colType.Length) { 196   197 throw new SqliteException ("columns.Length != colType.Length"); 198   199 } 200   201 string query = "CREATE TABLE " + name + " (" + col[0] + " " + colType[0]; 202   203 for (int i = 1; i < col.Length; ++i) { 204   205 query += ", " + col[i] + " " + colType[i]; 206   207 } 208   209 query += ")"; 210   211 return ExecuteQuery (query); 212   213 } 214   215 public SqliteDataReader SelectWhere (string tableName, string[] items, string[] col, string[] operation, string[] values) 216   217 { 218   219 if (col.Length != operation.Length || operation.Length != values.Length) { 220   221 throw new SqliteException ("col.Length != operation.Length != values.Length"); 222   223 } 224   225 string query = "SELECT " + items[0]; 226   227 for (int i = 1; i < items.Length; ++i) { 228   229 query += ", " + items[i]; 230   231 } 232   233 query += " FROM " + tableName + " WHERE " + col[0] + operation[0] + "'" + values[0] + "' "; 234   235 for (int i = 1; i < col.Length; ++i) { 236   237 query += " AND " + col[i] + operation[i] + "'" + values[0] + "' "; 238   239 } 240   241 return ExecuteQuery (query); 242   243 } 244   245 }
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 新竹市| 万山特区| 老河口市| 理塘县| 思南县| 启东市| 蕉岭县| 教育| 桓台县| 乳山市| 曲沃县| 杂多县| 松原市| 黑水县| 武清区| 墨竹工卡县| 尖扎县| 英超| 慈溪市| 红安县| 类乌齐县| 武定县| 雷波县| 潼南县| 漠河县| 临汾市| 科尔| 筠连县| 定襄县| 临夏市| 沙坪坝区| 扎鲁特旗| 含山县| 汉源县| 勐海县| 道孚县| 方城县| 平舆县| 黑龙江省| 无棣县| 桑植县|