日本搞逼视频_黄色一级片免费在线观看_色99久久_性明星video另类hd_欧美77_综合在线视频

國(guó)內(nèi)最全I(xiàn)T社區(qū)平臺(tái) 聯(lián)系我們 | 收藏本站
阿里云優(yōu)惠2
您當(dāng)前位置:首頁(yè) > php開(kāi)源 > 綜合技術(shù) > unity3d,C#使用sqlite作為數(shù)據(jù)庫(kù)解決方案思路

unity3d,C#使用sqlite作為數(shù)據(jù)庫(kù)解決方案思路

來(lái)源:程序員人生   發(fā)布時(shí)間:2015-04-23 07:48:04 閱讀次數(shù):4173次


1,編輯器建立好數(shù)據(jù)庫(kù)結(jié)構(gòu),生成sqlite數(shù)據(jù)庫(kù)文件,可以用navicat圖形界面編輯器來(lái)操作。
2,建立好的數(shù)據(jù)庫(kù),后綴名變成.txt格式(方便unity3d加載),放文件放到Assest/Resources目錄下(新建目錄)。
放在Resources目錄下的文件,在Pc/ios/android端都可以不作辨別的用Resource來(lái)加載,假定數(shù)據(jù)庫(kù)文件名位:data.txt,語(yǔ)句以下:
TextAsset txt = Resources.Load ("data", typeof(TextAsset))as TextAsset;
3, 將讀取到的TextAsset文件寫(xiě)入對(duì)應(yīng)平臺(tái)的沙盒路徑下, 代碼為:
   databaseFilePath = Application.persistentDataPath+"//"+data.db;(沙盒,各平臺(tái)路徑不同,均為可讀寫(xiě))
      File.WriteAllBytes(databaseFilePath,txt.bytes);
4,加載沙盒路徑下的數(shù)據(jù)庫(kù)文件進(jìn)行讀寫(xiě)操作。DbAccess封裝了數(shù)據(jù)庫(kù)操作。其中需要兩個(gè)dll文件,1個(gè)so文件(android平臺(tái)需要libsq


<p><span style="font-family: Arial, Helvetica, sans-serif;"> using UnityEngine;</span></p>

using System.Collections; using Mono.Data.Sqlite; using System; using System.IO; public class DbAccess{ private SqliteConnection dbConnection; private SqliteCommand dbCommand; private SqliteDataReader reader; public DbAccess (string connectionString) { OpenDB (connectionString); } public DbAccess () { } public void OpenDB (string connectionString) { try { dbConnection = new SqliteConnection (connectionString); dbConnection.Open (); Debug.Log ("Connected to db"); } catch(Exception e) { string temp1 = e.ToString(); Debug.Log(temp1); } } public void CloseSqlConnection () { if (dbCommand != null) { dbCommand.Dispose (); } dbCommand = null; if (reader != null) { reader.Dispose (); } reader = null; if (dbConnection != null) { dbConnection.Close (); } dbConnection = null; Debug.Log ("Disconnected from db."); } public SqliteDataReader ExecuteQuery (string sqlQuery) { dbCommand = dbConnection.CreateCommand (); dbCommand.CommandText = sqlQuery; reader = dbCommand.ExecuteReader (); return reader; } public SqliteDataReader ReadFullTable (string tableName) { string query = "SELECT * FROM " + tableName; return ExecuteQuery (query); } public SqliteDataReader 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 += ")"; return ExecuteQuery (query); } public SqliteDataReader 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 SqliteDataReader 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 SqliteDataReader InsertIntoSpecific (string tableName, string[] cols, string[] values) { if (cols.Length != values.Length) { throw new SqliteException ("columns.Length != values.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 += ")"; return ExecuteQuery (query); } public SqliteDataReader DeleteContents (string tableName) { string query = "DELETE FROM " + tableName; return ExecuteQuery (query); } public SqliteDataReader CreateTable (string name, string[] col, string[] colType) { if (col.Length != colType.Length) { throw new SqliteException ("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 SqliteDataReader SelectWhere (string tableName, string[] items, string[] col, string[] operation, string[] values) { if (col.Length != operation.Length ||operation.Length != values.Length) { throw new SqliteException ("col.Length != operation.Length != values.Length"); } string query = "SELECT " + items[0]; for (int i = 1;i < items.Length; ++i) { query += ", " + items[i]; } if (col.Length == 0) { query += " FROM " + tableName; } else { 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 class DataCenter{ private string databaseFilename = "data.db"; private string databaseFilePath; private DbAccess dbaccess; public DataCenter() { databaseFilePath = Application.persistentDataPath+"//"+databaseFilename; if (!File.Exists (databaseFilePath)) { TextAsset txt = Resources.Load ("data", typeof(TextAsset))as TextAsset; File.WriteAllBytes(databaseFilePath,txt.bytes); //copy data file to sandbox } dbaccess = new DbAccess (@"Data Source=" + databaseFilePath); } }


生活不易,碼農(nóng)辛苦
如果您覺(jué)得本網(wǎng)站對(duì)您的學(xué)習(xí)有所幫助,可以手機(jī)掃描二維碼進(jìn)行捐贈(zèng)
程序員人生
------分隔線----------------------------
分享到:
------分隔線----------------------------
關(guān)閉
程序員人生
主站蜘蛛池模板: 欧美伦理一区二区 | 亚洲欧美日韩一区 | 激情五月激情综合网 | 最新国产精品精品视频 | 国产麻豆| 亚洲一区观看 | www.国产一区 | 男人都懂得网站 | 国产精品污www在线观看 | 亚洲精品免费在线 | 欧美日韩激情在线一区二区三区 | 香蕉成人在线 | 最近中文免费字幕 | 国产一区二区三区精品久久久 | 国产91免费看 | 欧美综合第一页 | 免费看成人吃奶视频在线 | 久久最新网址 | 日韩一区二区三区在线 | 精品视频亚洲 | 99久久精品一区字幕狠狠婷婷 | 国产精品入口麻豆九色 | 不卡一区二区在线 | 亚洲一区二区免费视频 | 狠狠干狠狠干 | 久久激情网 | 岛国av免费看 | 激情欧美一区二区三区中文字幕 | 中文字幕日韩视频 | 91综合久久 | 亚洲精品福利电影 | 久久精品a| 午夜视频黄色 | 激情天堂 | 欧美日韩中文在线 | 色婷婷精品国产一区二区三区 | 9999精品| 欧美日韩一区二区三区在线视频 | 成人黄色在线播放 | 成人欧美一区二区三区视频xxx | 尤物yw |