当前位置:   article > 正文

unity3d,C#使用sqlite作为数据库解决方案思路_orm框架 unity sqlite

orm框架 unity sqlite


1,编辑器建立好数据库结构,生成sqlite数据库文件,可以用navicat图形界面编辑器来操作。
2,建立好的数据库,后缀名变为.txt格式(方便unity3d加载),放文件放到Assest/Resources目录下(新建目录)。
放在Resources目录下的文件,在Pc/ios/android端均可以不作区分的用Resource来加载,假设数据库文件名位:data.txt,语句如下:
TextAsset txt = Resources.Load ("data", typeof(TextAsset))as TextAsset;
3, 将读取到的TextAsset文件写入对应平台的沙盒路径下, 代码为:
   databaseFilePath = Application.persistentDataPath+"//"+data.db;(沙盒,各平台路径不同,均为可读写)
      File.WriteAllBytes(databaseFilePath,txt.bytes);
4,加载沙盒路径下的数据库文件进行读写操作。DbAccess封装了数据库操作。其中需要两个dll文件,一个so文件(android平台需要libsq


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

  1. using System.Collections;
  2. using Mono.Data.Sqlite;
  3. using System;
  4. using System.IO;
  5. public class DbAccess{
  6. private SqliteConnection dbConnection;
  7. private SqliteCommand dbCommand;
  8. private SqliteDataReader reader;
  9. public DbAccess (string connectionString)
  10. {
  11. OpenDB (connectionString);
  12. }
  13. public DbAccess ()
  14. {
  15. }
  16. public void OpenDB (string connectionString)
  17. {
  18. try
  19. {
  20. dbConnection = new SqliteConnection (connectionString);
  21. dbConnection.Open ();
  22. Debug.Log ("Connected to db");
  23. }
  24. catch(Exception e)
  25. {
  26. string temp1 = e.ToString();
  27. Debug.Log(temp1);
  28. }
  29. }
  30. public void CloseSqlConnection ()
  31. {
  32. if (dbCommand != null) {
  33. dbCommand.Dispose ();
  34. }
  35. dbCommand = null;
  36. if (reader != null) {
  37. reader.Dispose ();
  38. }
  39. reader = null;
  40. if (dbConnection != null) {
  41. dbConnection.Close ();
  42. }
  43. dbConnection = null;
  44. Debug.Log ("Disconnected from db.");
  45. }
  46. public SqliteDataReader ExecuteQuery (string sqlQuery)
  47. {
  48. dbCommand = dbConnection.CreateCommand ();
  49. dbCommand.CommandText = sqlQuery;
  50. reader = dbCommand.ExecuteReader ();
  51. return reader;
  52. }
  53. public SqliteDataReader ReadFullTable (string tableName)
  54. {
  55. string query = "SELECT * FROM " + tableName;
  56. return ExecuteQuery (query);
  57. }
  58. public SqliteDataReader InsertInto (string tableName, string[] values)
  59. {
  60. string query = "INSERT INTO " + tableName + " VALUES (" + values[0];
  61. for (int i = 1; i < values.Length; ++i) {
  62. query += ", " + values[i];
  63. }
  64. query += ")";
  65. return ExecuteQuery (query);
  66. }
  67. public SqliteDataReader UpdateInto (string tableName, string []cols,string []colsvalues,string selectkey,string selectvalue)
  68. {
  69. string query = "UPDATE "+tableName+" SET "+cols[0]+" = "+colsvalues[0];
  70. for (int i = 1; i < colsvalues.Length; ++i) {
  71. query += ", " +cols[i]+" ="+ colsvalues[i];
  72. }
  73. query += " WHERE "+selectkey+" = "+selectvalue+" ";
  74. return ExecuteQuery (query);
  75. }
  76. public SqliteDataReader Delete(string tableName,string []cols,string []colsvalues)
  77. {
  78. string query = "DELETE FROM "+tableName + " WHERE " +cols[0] +" = " + colsvalues[0];
  79. for (int i = 1; i < colsvalues.Length; ++i) {
  80. query += " or " +cols[i]+" = "+ colsvalues[i];
  81. }
  82. Debug.Log(query);
  83. return ExecuteQuery (query);
  84. }
  85. public SqliteDataReader InsertIntoSpecific (string tableName, string[] cols, string[] values)
  86. {
  87. if (cols.Length != values.Length) {
  88. throw new SqliteException ("columns.Length != values.Length");
  89. }
  90. string query = "INSERT INTO " + tableName + "(" + cols[0];
  91. for (int i = 1; i < cols.Length; ++i) {
  92. query += ", " + cols[i];
  93. }
  94. query += ") VALUES (" + values[0];
  95. for (int i = 1; i < values.Length; ++i) {
  96. query += ", " + values[i];
  97. }
  98. query += ")";
  99. return ExecuteQuery (query);
  100. }
  101. public SqliteDataReader DeleteContents (string tableName)
  102. {
  103. string query = "DELETE FROM " + tableName;
  104. return ExecuteQuery (query);
  105. }
  106. public SqliteDataReader CreateTable (string name, string[] col, string[] colType)
  107. {
  108. if (col.Length != colType.Length) {
  109. throw new SqliteException ("columns.Length != colType.Length");
  110. }
  111. string query = "CREATE TABLE " + name + " (" + col[0] + " " + colType[0];
  112. for (int i = 1; i < col.Length; ++i)
  113. {
  114. query += ", " + col[i] + " " + colType[i];
  115. }
  116. query += ")";
  117. return ExecuteQuery (query);
  118. }
  119. public SqliteDataReader SelectWhere (string tableName, string[] items, string[] col, string[] operation, string[] values)
  120. {
  121. if (col.Length != operation.Length ||operation.Length != values.Length) {
  122. throw new SqliteException ("col.Length != operation.Length != values.Length");
  123. }
  124. string query = "SELECT " + items[0];
  125. for (int i = 1;i < items.Length; ++i) {
  126. query += ", " + items[i];
  127. }
  128. if (col.Length == 0) {
  129. query += " FROM " + tableName;
  130. } else
  131. {
  132. query += " FROM " + tableName + " WHERE " + col[0] + operation[0] + "'" + values[0] + "' ";
  133. for (int i = 1; i < col.Length; ++i) {
  134. query += " AND " + col[i] + operation[i] + "'" + values[0] + "' ";
  135. }
  136. }
  137. return ExecuteQuery (query);
  138. }
  139. }
  140. public class DataCenter{
  141. private string databaseFilename = "data.db";
  142. private string databaseFilePath;
  143. private DbAccess dbaccess;
  144. public DataCenter()
  145. {
  146. databaseFilePath = Application.persistentDataPath+"//"+databaseFilename;
  147. if (!File.Exists (databaseFilePath))
  148. {
  149. TextAsset txt = Resources.Load ("data", typeof(TextAsset))as TextAsset;
  150. File.WriteAllBytes(databaseFilePath,txt.bytes);
  151. //copy data file to sandbox
  152. }
  153. dbaccess = new DbAccess (@"Data Source=" + databaseFilePath);
  154. }
  155. }


声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/你好赵伟/article/detail/127148
推荐阅读
相关标签
  

闽ICP备14008679号