原始文章主要是使用JavaScript語言建立本地?cái)?shù)據(jù)庫

采用html5+css3國際標(biāo)準(zhǔn)網(wǎng)站建設(shè),讓網(wǎng)站自動(dòng)適應(yīng)用戶使用終端設(shè)備,PC、平板、手機(jī)等,一個(gè)網(wǎng)址適應(yīng),一套內(nèi)容統(tǒng)一戰(zhàn)略,節(jié)約企業(yè)資源。創(chuàng)新互聯(lián)還提供網(wǎng)站后期營銷如:軟文發(fā)稿、賣友情鏈接、廣告投放平臺(tái)等。一般建站公司不為企業(yè)填充資料,更談不上內(nèi)容策劃,結(jié)果導(dǎo)致網(wǎng)站界面優(yōu)秀,內(nèi)容卻十分空泛或整體不協(xié)調(diào),內(nèi)容策劃、內(nèi)容填充請(qǐng)交給我們。
以前在開發(fā)中一直使用iOS源生的數(shù)據(jù)庫,通過傳遞消息的形式在與Unity3D中進(jìn)行交互。本文我在詳細(xì)說說如何使用C#語言來在MAC 操作系統(tǒng)下創(chuàng)建Unity本地?cái)?shù)據(jù)庫,我是C#控.
首先你需要得到Mono.Data.Sqlite.dll 文件 與System.Data.dll文件。如果你在Mac 操作系統(tǒng)下使用Unity那么很悲劇,找不到這兩個(gè)文件,至少我沒能找到。后來我在Windows下的Unity安裝路徑中找到了它。為了方便大家我將這兩個(gè)文件上傳至網(wǎng)盤中,如果沒有這兩個(gè)文件的朋友請(qǐng)下載。Unity數(shù)據(jù)庫文件.zip
.zip文件下載完畢后直接解壓,然后將Mono.Data.Sqlite.dll 文件 與System.Data.dll文件放在Unity工程中的Assets文件夾中。如下圖所示,兩個(gè)文件已經(jīng)放置在Project視圖當(dāng)中。
Ok ,我們編寫C#腳本,原始文章沒有Unity數(shù)據(jù)庫更新與刪除的方法,我在這里加上更新與刪除的方法,方便大家開發(fā)時(shí)使用。因?yàn)槠鋵?shí)Unity中更新與刪除數(shù)據(jù)庫也是個(gè)比較重要的功能。
注意:下面腳本不要綁定在任何游戲?qū)ο笊砩?,大家無需把它當(dāng)作腳本可以當(dāng)作一個(gè)工具類來使用。
[代碼]java代碼:
| 004 | using System.Collections; |
| 005 | using Mono.Data.Sqlite; |
| 011 | private SqliteConnection dbConnection; |
| 013 | private SqliteCommand dbCommand; |
| 015 | private SqliteDataReader reader; |
| 017 | public DbAccess (string connectionString) |
| 021 | OpenDB (connectionString); |
| 029 | public void OpenDB (string connectionString) |
| 034 | dbConnection = new SqliteConnection (connectionString); |
| 036 | dbConnection.Open (); |
| 038 | Debug.Log ("Connected to db"); |
| 042 | string temp1 = e.ToString(); |
| 048 | public void CloseSqlConnection () |
| 052 | if (dbCommand != null) { |
| 054 | dbCommand.Dispose (); |
| 060 | if (reader != null) { |
| 068 | if (dbConnection != null) { |
| 070 | dbConnection.Close (); |
| 076 | Debug.Log ("Disconnected from db."); |
| 080 | public SqliteDataReader ExecuteQuery (string sqlQuery) |
| 084 | dbCommand = dbConnection.CreateCommand (); |
| 086 | dbCommand.CommandText = sqlQuery; |
| 088 | reader = dbCommand.ExecuteReader (); |
| 094 | public SqliteDataReader ReadFullTable (string tableName) |
| 098 | string query = "SELECT * FROM " + tableName; |
| 100 | return ExecuteQuery (query); |
| 104 | public SqliteDataReader InsertInto (string tableName, string[] values) |
| 108 | string query = "INSERT INTO " + tableName + " VALUES (" + values[0]; |
| 110 | for (int i = 1; i < values.Length; ++i) { |
| 112 | query += ", " + values[i]; |
| 118 | return ExecuteQuery (query); |
| 122 | public SqliteDataReader UpdateInto (string tableName, string []cols,string []colsvalues,string selectkey,string selectvalue) |
| 125 | string query = "UPDATE "+tableName+" SET "+cols[0]+" = "+colsvalues[0]; |
| 127 | for (int i = 1; i < colsvalues.Length; ++i) { |
| 129 | query += ", " +cols[i]+" ="+ colsvalues[i]; |
| 132 | query += " WHERE "+selectkey+" = "+selectvalue+" "; |
| 134 | return ExecuteQuery (query); |
| 137 | public SqliteDataReader Delete(string tableName,string []cols,string []colsvalues) |
| 139 | string query = "DELETE FROM "+tableName + " WHERE " +cols[0] +" = " + colsvalues[0]; |
| 141 | for (int i = 1; i < colsvalues.Length; ++i) { |
| 143 | query += " or " +cols[i]+" = "+ colsvalues[i]; |
| 146 | return ExecuteQuery (query); |
| 149 | public SqliteDataReader InsertIntoSpecific (string tableName, string[] cols, string[] values) |
| 153 | if (cols.Length != values.Length) { |
| 155 | throw new SqliteException ("columns.Length != values.Length"); |
| 159 | string query = "INSERT INTO " + tableName + "(" + cols[0]; |
| 161 | for (int i = 1; i < cols.Length; ++i) { |
| 163 | query += ", " + cols[i]; |
| 167 | query += ") VALUES (" + values[0]; |
| 169 | for (int i = 1; i < values.Length; ++i) { |
| 171 | query += ", " + values[i]; |
| 177 | return ExecuteQuery (query); |
| 181 | public SqliteDataReader DeleteContents (string tableName) |
| 185 | string query = "DELETE FROM " + tableName; |
| 187 | return ExecuteQuery (query); |
| 191 | public SqliteDataReader CreateTable (string name, string[] col, string[] colType) |
| 195 | if (col.Length != colType.Length) { |
| 197 | throw new SqliteException ("columns.Length != colType.Length"); |
| 201 | string query = "CREATE TABLE " + name + " (" + col[0] + " " + colType[0]; |
| 203 | for (int i = 1; i < col.Length; ++i) { |
| 205 | query += ", " + col[i] + " " + colType[i]; |
| 211 | return ExecuteQuery (query); |
| 215 | public SqliteDataReader SelectWhere (string tableName, string[] items, string[] col, string[] operation, string[] values) |
| 219 | if (col.Length != operation.Length || operation.Length != values.Length) { |
| 221 | throw new SqliteException ("col.Length != operation.Length != values.Length"); |
| 225 | string query = "SELECT " + items[0]; |
| 227 | for (int i = 1; i < items.Length; ++i) { |
| 229 | query += ", " + items[i]; |
| 233 | query += " FROM " + tableName + " WHERE " + col[0] + operation[0] + "'" + values[0] + "' "; |
| 235 | for (int i = 1; i < col.Length; ++i) { |
| 237 | query += " AND " + col[i] + operation[i] + "'" + values[0] + "' "; |
| 241 | return ExecuteQuery (query); |
首先是創(chuàng)建本地?cái)?shù)據(jù)庫,我們創(chuàng)建C#腳本Test.cs直接綁定在攝像機(jī)中。
[代碼]java代碼:
| 02 | using System.Collections; |
| 04 | public class Test : MonoBehaviour |
| 10 | //創(chuàng)建數(shù)據(jù)庫名稱為xuanyusong.db |
| 11 | DbAccess db = new DbAccess("data source=xuanyusong.db"); |
| 13 | //創(chuàng)建數(shù)據(jù)庫表,與字段 |
| 14 | db.CreateTable("momo",new string[]{"name","qq","email","blog"}, new string[]{"text","text","text","text"}); |
| 16 | db.CloseSqlConnection(); |
運(yùn)行游戲后,數(shù)據(jù)庫對(duì)象會(huì)自動(dòng)生成在項(xiàng)目的根目錄中。查看數(shù)據(jù)庫的軟件我使用的是Navicat Premium,如果沒有請(qǐng)大家下載,然后繼續(xù)。如下圖所示,數(shù)據(jù)庫文件xuanyusong.db已經(jīng)生成在項(xiàng)目的根目錄中,接著我使用Navicat Premium軟件將這個(gè)數(shù)據(jù)庫打開。數(shù)據(jù)庫的表名為momo 打開表后字段包含name、 qq 、email、 blog。都是我們?cè)诖a中創(chuàng)建的。
OK,我們繼續(xù)。首先是插入數(shù)據(jù),記得將編碼修改成UTF-16 不然中文會(huì)亂碼。
[代碼]java代碼:
| 02 | using System.Collections; |
| 04 | public class Test : MonoBehaviour |
| 10 | //創(chuàng)建數(shù)據(jù)庫名稱為xuanyusong.db |
| 11 | DbAccess db = new DbAccess("data source=xuanyusong.db"); |
| 12 | //請(qǐng)注意 插入字符串是 已經(jīng)要加上'宣雨松' 不然會(huì)報(bào)錯(cuò) |
| 13 | db.InsertInto("momo", new string[]{ "'宣雨松'","'289187120'","'[email protected]'","'www.xuanyusong.com'" }); |
| 14 | db.CloseSqlConnection(); |
接著是更新數(shù)據(jù)。UpdateInto是我新寫的方法,接受更新多條數(shù)據(jù)。
[代碼]java代碼:
| 02 | using System.Collections; |
| 04 | public class Test : MonoBehaviour |
| 10 | //創(chuàng)建數(shù)據(jù)庫名稱為xuanyusong.db |
| 11 | DbAccess db = new DbAccess("data source=xuanyusong.db"); |
| 13 | db.UpdateInto("momo",new string[]{"name","qq"},new string[]{"'xuanyusong'","'11111111'"}, "email", "'[email protected]'" ); |
| 15 | db.CloseSqlConnection(); |
然后是刪除數(shù)據(jù)DELETE也是我封裝的方法。
[代碼]java代碼:
| 02 | using System.Collections; |
| 04 | public class Test : MonoBehaviour |
| 10 | //創(chuàng)建數(shù)據(jù)庫名稱為xuanyusong.db |
| 11 | DbAccess db = new DbAccess("data source=xuanyusong.db"); |
| 12 | //請(qǐng)注意 插入字符串是 已經(jīng)要加上'宣雨松' 不然會(huì)報(bào)錯(cuò) |
| 13 | db.CreateTable("momo",new string[]{"name","qq","email","blog"}, new string[]{"text","text","text","text"}); |
| 14 | //我在數(shù)據(jù)庫中連續(xù)插入三條數(shù)據(jù) |
| 15 | db.InsertInto("momo", new string[]{ "'宣雨松'","'289187120'","'[email protected]'","'www.xuanyusong.com'" }); |
| 16 | db.InsertInto("momo", new string[]{ "'雨松MOMO'","'289187120'","'[email protected]'","'www.xuanyusong.com'" }); |
| 17 | db.InsertInto("momo", new string[]{ "'哇咔咔'","'289187120'","'[email protected]'","'www.xuanyusong.com'" }); |
| 22 | db.CloseSqlConnection(); |
***是查找數(shù)據(jù)。
[代碼]java代碼:
| 02 | using System.Collections; |
| 04 | using Mono.Data.Sqlite; |
| 05 | public class Test : MonoBehaviour |
| 11 | //創(chuàng)建數(shù)據(jù)庫名稱為xuanyusong.db |
| 12 | DbAccess db = new DbAccess("data source=xuanyusong.db"); |
| 13 | //請(qǐng)注意 插入字符串是 已經(jīng)要加上'宣雨松' 不然會(huì)報(bào)錯(cuò) |
| 14 | db.CreateTable("momo",new string[]{"name","qq","email","blog"}, new string[]{"text","text","text","text"}); |
| 15 | //我在數(shù)據(jù)庫中連續(xù)插入三條數(shù)據(jù) |
| 16 | db.InsertInto("momo", new string[]{ "'宣雨松'","'289187120'","'[email protected]'","'www.xuanyusong.com'" }); |
| 17 | db.InsertInto("momo", new string[]{ "'雨松MOMO'","'289187120'","'[email protected]'","'www.xuanyusong.com'" }); |
| 18 | db.InsertInto("momo", new string[]{ "'哇咔咔'","'289187120'","'[email protected]'","'www.xuanyusong.com'" }); |
| 24 | SqliteDataReader sqReader = db.SelectWhere("momo",new string[]{"name","email"},new string[]{"qq"},new string[]{"="},new string[]{"289187120"}); |
| 26 | while (sqReader.Read()) |
| 28 | Debug.Log(sqReader.GetString(sqReader.GetOrdinal("name")) + sqReader.GetString(sqReader.GetOrdinal("email"))); |
| 31 | db.CloseSqlConnection(); |
注解1:這里的結(jié)構(gòu)非常像安卓的數(shù)據(jù)庫指針,然后while循環(huán)把每一條數(shù)據(jù)都取出來。 sqReader.Gerordinal()方法就是拿到對(duì)應(yīng)列名稱的數(shù)據(jù)。如下圖所示,經(jīng)過一些列的添加與刪除的操作***數(shù)據(jù)庫的內(nèi)容如下。
如下圖所示,我使用Log也將數(shù)據(jù)庫name 與 email的字段打印了出來。***我在強(qiáng)調(diào)一點(diǎn),我們?cè)贠nStart方法中db.CreateTable創(chuàng)建數(shù)據(jù)庫表,如果重復(fù)創(chuàng)建系統(tǒng)會(huì)拋出錯(cuò)誤。避免這個(gè)情況請(qǐng)保證你的數(shù)據(jù)庫表只會(huì)被創(chuàng)建一次。祝大家學(xué)習(xí)愉快嘎嘎嘎~~~
如下圖所示,請(qǐng)先在PlaySettings中修改Api Compatibility Level 改成.NET 2.0,如果不修改會(huì)報(bào)錯(cuò)
注意:Error building Player: Extracting referenced dlls failed.
無論你編譯任何平臺(tái)都請(qǐng)修改一下這里, 留言中有朋友在編譯PC平臺(tái)中 因?yàn)闆]有修改這里導(dǎo)致無法編譯成功。
IOS平臺(tái)SQLite的使用:
然后需要修改Test.cs的腳本,在修改一下數(shù)據(jù)庫保存的路徑,我們將數(shù)據(jù)庫放在沙盒當(dāng)中。這樣IOS中才可以讀取數(shù)據(jù)庫。
[代碼]java代碼:
| 02 | using System.Collections; |
| 04 | using Mono.Data.Sqlite; |
| 05 | public class Test : MonoBehaviour |
| 10 | //數(shù)據(jù)庫文件儲(chǔ)存地址 |
| 11 | string appDBPath = Application.persistentDataPath + "/xuanyusong.db"; |
| 13 | DbAccess db = new DbAccess(@"Data Source=" + appDBPath); |
| 15 | //請(qǐng)注意 插入字符串是 已經(jīng)要加上'宣雨松' 不然會(huì)報(bào)錯(cuò) |
| 16 | db.CreateTable("momo",new string[]{"name","qq","email","blog"}, new string[]{"text","text","text","text"}); |
| 17 | //我在數(shù)據(jù)庫中連續(xù)插入三條數(shù)據(jù) |
| 18 | db.InsertInto("momo", new string[]{ "'宣雨松'","'289187120'","'[email protected]'","'www.xuanyusong.com'" }); |
| 19 | db.InsertInto("momo", new string[]{ "'雨松MOMO'","'289187120'","'[email protected]'","'www.xuanyusong.com'" }); |
| 20 | db.InsertInto("momo", new string[]{ "'哇咔咔'","'289187120'","'[email protected]'","'www.xuanyusong.com'" }); |
| 26 | using (SqliteDataReader sqReader = db.SelectWhere("momo",new string[]{"name","email"},new string[]{"qq"},new string[]{"="},new string[]{"289187120"})) |
| 29 | while (sqReader.Read()) |
| 32 | Debug.Log(sqReader.GetString(sqReader.GetOrdinal("name"))); |
| 34 | Debug.Log(sqReader.GetString(sqReader.GetOrdinal("email"))); |
| 41 | db.CloseSqlConnection(); |
下面開始打包成IOS版本,直接運(yùn)行如下圖所示,已經(jīng)在XCODE的控制臺(tái)中將字符串信息打印出來。目前我不知道如何讀取中文,但是可以確定的是中文信息已經(jīng)寫入數(shù)據(jù)庫中。不信大家可以打開沙盒看看。
Android平臺(tái)SQLite的使用: Android與IOS在使用SQLite數(shù)據(jù)庫時(shí)有點(diǎn)區(qū)別,Android需要將第三方DLL放在Plugins當(dāng)中。腳本也需要修改一下,先看看Test.cs的改動(dòng)。
[代碼]java代碼:
| 02 | using System.Collections; |
| 04 | using Mono.Data.Sqlite; |
| 05 | public class Test : MonoBehaviour |
| 10 | //數(shù)據(jù)庫文件儲(chǔ)存地址 |
| 12 | string appDBPath = Application.persistentDataPath + "/xuanyusong.db"; |
| 14 | //注意?。。。。。。∵@行代碼的改動(dòng) |
| 15 | DbAccess db = new DbAccess("URI=file:" + appDBPath); |
| 17 | //請(qǐng)注意 插入字符串是 已經(jīng)要加上'宣雨松' 不然會(huì)報(bào)錯(cuò) |
| 18 | db.CreateTable("momo",new string[]{"name","qq","email","blog"}, new string[]{"text","text","text","text"}); |
| 19 | //我在數(shù)據(jù)庫中連續(xù)插入三條數(shù)據(jù) |
| 20 | db.InsertInto("momo", new string[]{ "'宣雨松'","'289187120'","'[email protected]'","'www.xuanyusong.com'" }); |
| 21 | db.InsertInto("momo", new string[]{ "'雨松MOMO'","'289187120'","'[email protected]'","'www.xuanyusong.com'" }); |
| 22 | db.InsertInto("momo", new string[]{ "'哇咔咔'","'289187120'","'[email protected]'","'www.xuanyusong.com'" }); |
| 28 | using (SqliteDataReader sqReader = db.SelectWhere("momo",new string[]{"name","email"},new string[]{"qq"},new string[]{"="},new string[]{"289187120"})) |
| 31 | while (sqReader.Read()) |
| 33 | Debug.Log("xuanyusong" + sqReader.GetString(sqReader.GetOrdinal("name"))); |
| 35 | Debug.Log("xuanyusong" + sqReader.GetString(sqReader.GetOrdinal("email"))); |
| 42 | db.CloseSqlConnection(); |
| 47 | if (Input.GetKeyDown(KeyCode.Escape) ||Input.GetKeyDown(KeyCode.Home) ) |
如下圖所示,Player Settings 請(qǐng)和我保持一致。
值得慶幸的是在Android下讀取數(shù)據(jù)庫時(shí)正常的顯示了中文。如下圖所示,運(yùn)行打包后的程序后在Eclipse的后臺(tái)已經(jīng)能看到數(shù)據(jù)庫顯示的中文與英文,呵呵。 由于工程中需要一些DLL,所以我將工程的下載地址放出,請(qǐng)大家下載。AndroidSQL.unitypackage.zip MAC平臺(tái)下的使用: 請(qǐng)先下載原始版本 SQLite (1).unitypackage.zip 我們只需在原始版本之上進(jìn)行修改即可。 修改Test.cs文件 ,請(qǐng)注意我在代碼中標(biāo)注的內(nèi)容。
[代碼]java代碼:
| 02 | using System.Collections; |
| 04 | using Mono.Data.Sqlite; |
| 05 | public class Test : MonoBehaviour |
| 10 | string appDBPath = null; |
| 18 | appDBPath = Application.dataPath + "/xuanyusong.db"; |
| 20 | DbAccess db = new DbAccess(@"Data Source=" + appDBPath); |
| 24 | //請(qǐng)注意 插入字符串是 已經(jīng)要加上'宣雨松' 不然會(huì)報(bào)錯(cuò) |
| 25 | db.CreateTable("momo",new string[]{"name","qq","email","blog"}, new string[]{"text","text","text","text"}); |
| 26 | //我在數(shù)據(jù)庫中連續(xù)插入三條數(shù)據(jù) |
| 27 | db.InsertInto("momo", new string[]{ "'宣雨松'","'289187120'","'[email protected]'","'www.xuanyusong.com'" }); |
| 28 | db.InsertInto("momo", new string[]{ "'雨松MOMO'","'289187120'","'[email protected]'","'www.xuanyusong.com'" }); |
| 29 | db.InsertInto("momo", new string[]{ "'哇咔咔'","'289187120'","'[email protected]'","'www.xuanyusong.com'" }); |
| 34 | SqliteDataReader sqReader = db.SelectWhere("momo",new string[]{"name","email"},new string[]{"qq"},new string[]{"="},new string[]{"289187120"}); |
| 36 | while (sqReader.Read()) |
| 39 | Debug.Log(sqReader.GetString(sqReader.GetOrdinal("name")) + sqReader.GetString(sqReader.GetOrdinal("email"))); |
| 42 | name = sqReader.GetString(sqReader.GetOrdinal("name")); |
| 43 | email = sqReader.GetString(sqReader.GetOrdinal("email")); |
| 46 | db.CloseSqlConnection(); |
| 52 | ///為了讓大家看的更清楚 我將數(shù)據(jù)庫取出的內(nèi)容顯示在屏幕中 |
| 55 | GUILayout.Label("XXXXXXXXXXXXX" + name); |
| 61 | GUILayout.Label("XXXXXXXXXXXXX" + email); |