当前位置:   article > 正文

unity之连接SQLite数据库_怎么确认unity连接上sqlite

怎么确认unity连接上sqlite

创建数据库,可以选择SQLiteManager或者其他的sqlite工具,我创建了一个MyDataBase.sqlite,里面含有一个People表,内含2列,分别为name 和sex



为其添加2个字段


准备工作就做完了,下面就可以写代码了

打开unity, 把Mono.Data.Sqlite文件和System.Data.dll放到Assets目录下,在unity5.1的版本还要额外导入一个sqlite3.dll文件

与数据库有关的类

https://msdn.microsoft.com/zh-cn/library/system.data.sqlclient.sqlconnection(v=vs.110).aspx
  1. using UnityEngine;
  2. using System.Collections;
  3. using Mono.Data.Sqlite;
  4. using System.Data.SqlClient;
  5. public class Mydata {
  6. private SqliteConnection sqlConnection;
  7. private SqliteCommand sqlCommand;//数据库指令对象,也就是来执行SQLite语句的
  8. private SqliteDataReader sqlReader;//数据库读取对象,
  9. private string dataName = "Data Source = " + Application.dataPath + "/MyDataBase.sqlite";//你数据库所在的位置
  10. public Mydata() {
  11. sqlConnection = new SqliteConnection(dataName);
  12. sqlConnection.Open();
  13. }
  14. public void OpenData(){
  15. sqlConnection = new SqliteConnection(dataName);
  16. sqlConnection.Open();
  17. }
  18. public void QueryAll(){
  19. OpenData();
  20. sqlCommand = sqlConnection.CreateCommand();
  21. sqlCommand.CommandText = "SELECT * FROM People;";
  22. sqlCommand.ExecuteNonQuery();//执行语句
  23. sqlReader = sqlCommand.ExecuteReader();//读取
  24. ShowData();
  25. Close();
  26. }
  27. public void QueryOne() {
  28. OpenData();
  29. //查询name列,可能有多个值
  30. sqlCommand = sqlConnection.CreateCommand();
  31. sqlCommand.CommandText = "Select name from People;";
  32. sqlCommand.ExecuteNonQuery();
  33. sqlReader = sqlCommand.ExecuteReader();
  34. ShowData();
  35. Close();
  36. Debug.Log("**********************************");
  37. }
  38. public void QuerySpecalData() {
  39. //查询满足条件的一个值
  40. OpenData();
  41. sqlCommand = sqlConnection.CreateCommand();
  42. sqlCommand.CommandText = "select sex from People where name = 'cb';";
  43. sqlCommand.ExecuteNonQuery();
  44. sqlReader = sqlCommand.ExecuteReader();
  45. sqlReader.Read();
  46. Debug.Log( sqlReader.GetValue(0));
  47. Close();
  48. Debug.Log("**********************************");
  49. }
  50. //插入
  51. public void InsertData() {
  52. OpenData();
  53. //Reader如果不管,就执行不了新的读取操作
  54. sqlCommand = sqlConnection.CreateCommand();
  55. sqlCommand.CommandText = "insert into People values('bb','m');";
  56. int n = sqlCommand.ExecuteNonQuery();
  57. Debug.Log(n);
  58. //一个SqliteCommand只能执行一条语句,如果想要在次执行,必须重新创建
  59. sqlCommand = sqlConnection.CreateCommand();
  60. sqlCommand.CommandText = "select name from People where sex = m;";
  61. sqlCommand.ExecuteNonQuery();
  62. sqlReader = sqlCommand.ExecuteReader();
  63. sqlReader.Read();
  64. Debug.Log(sqlReader.GetValue(0));
  65. Close();
  66. Debug.Log("**********************************");
  67. }
  68. public void updateData() {
  69. OpenData();
  70. sqlCommand = sqlConnection.CreateCommand();
  71. sqlCommand.CommandText = "update people set name = 'mm' where name = 'bb';";
  72. int n = sqlCommand.ExecuteNonQuery();
  73. Debug.Log(n);
  74. sqlCommand = sqlConnection.CreateCommand();
  75. sqlCommand.CommandText = "select name from People;";
  76. sqlCommand.ExecuteNonQuery();
  77. sqlReader = sqlCommand.ExecuteReader();
  78. ShowData();
  79. Close();
  80. Debug.Log("**********************************");
  81. }
  82. public void delectData() {
  83. OpenData();
  84. sqlCommand = sqlConnection.CreateCommand();
  85. sqlCommand.CommandText = "delete from People where name = 'wal';";
  86. sqlCommand.ExecuteNonQuery();
  87. sqlCommand = sqlConnection.CreateCommand();
  88. sqlCommand.CommandText = "select name from People;";
  89. sqlCommand.ExecuteNonQuery();
  90. sqlReader = sqlCommand.ExecuteReader();
  91. ShowData();
  92. Close();
  93. Debug.Log("**********************************");
  94. }
  95. public void ShowData() {
  96. while(sqlReader.Read()){
  97. for (int i = 0; i < sqlReader.FieldCount; i++)
  98. {
  99. if (sqlReader.GetValue(i) == null)
  100. {
  101. Debug.Log("该列值为null");
  102. }
  103. else
  104. {
  105. Debug.Log(sqlReader.GetValue(i));
  106. }
  107. }
  108. }
  109. }
  110. public void Close() {
  111. if(sqlReader!=null){
  112. sqlReader.Dispose();//释放所占用的资源
  113. }
  114. if(sqlCommand!=null){
  115. sqlCommand.Dispose();
  116. }
  117. if(sqlConnection!=null){
  118. sqlConnection.Close();//关闭数据库
  119. }
  120. }
  121. }


Test(添加给照相机或随你喜欢)

  1. using UnityEngine;
  2. using System.Collections;
  3. public class Test : MonoBehaviour {
  4. Mydata mydata;
  5. void Start () {
  6. mydata = new Mydata();
  7. mydata.QueryAll();
  8. mydata.QueryOne();
  9. mydata.QuerySpecalData();
  10. mydata.InsertData();
  11. mydata.updateData();
  12. mydata.delectData();
  13. }
  14. void Update () {
  15. }
  16. }

注意:

1.sqliteReader,sqliteCommand用过一次必须要关,否则不能接受新的指令

2.sqlite不支持删除整列...
3.如果数据库的位置获取错误,可能出现找不到表的情况,先检查数据库里有没有这个表,再检查代码中获取数据库所在位置的地方是否正确


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

闽ICP备14008679号