Unity (五) Sqlite数据库之:Framework
时间:2021-07-01 10:21:17
帮助过:9人阅读
using System.Collections;
2 using System.Collections.Generic;
3 using UnityEngine;
4 using Mono.Data.Sqlite;
5
6 public class SQLFramework
7 {
8
9
10 private static SQLFramework instance;
11 private SQLFramework() { };
12
13 public static SQLFramework GetInstance()
14 {
15 if (instance ==
null)
16 {
17 instance =
new SQLFramework();
18 }
19 return instance;
20 }
21
22
23
24 #region 数据库基础对象
25
26 private SqliteConnection conn;
27
28 private SqliteCommand command;
29
30 private SqliteDataReader reader;
31
32 #endregion
33
34 private string sqlConnectionPath;
35
36 /// <summary>
37 /// 设置数据库名称
38 /// </summary>
39 public void SetDatabaseName(
string databaseName)
40 {
41 //添加后缀
42 if (!databaseName.Contains(
".sqlite"))
43 {
44 databaseName +=
".sqlite";
45 }
46 //拼凑路径
47 sqlConnectionPath =
"Data Source = " + Application.streamingAssetsPath +
"/" +
databaseName;
48 //实例对象
49 conn =
new SqliteConnection(sqlConnectionPath);
50 command =
conn.CreateCommand();
51 }
52
53 /// <summary>
54 /// 打开数据库
55 /// </summary>
56 public void OpenDatabase()
57 {
58 try
59 {
60 conn.Open();
61 }
62 catch (SqliteException ex)
63 {
64 Debug.LogWarning(
"数据库打开异常:" +
ex.ToString());
65 }
66 }
67
68 /// <summary>
69 /// 关闭数据库
70 /// </summary>
71 public void CloseDatabase()
72 {
73 try
74 {
75 //读取连接关闭
76 if (!
reader.IsClosed)
77 {
78 reader.Close();
79 }
80 //数据库关闭
81 conn.Close();
82 }
83 catch (SqliteException ex)
84 {
85 Debug.LogWarning(
"数据库关闭异常:" +
ex.ToString());
86 }
87 }
88
89 /// <summary>
90 /// 无返回式的执行(增删改系列)
91 /// </summary>
92 /// <param name="query">SQL语句.</param>
93 public virtual void NullReturnExcute(
string query)
94 {
95 try
96 {
97 command.CommandText =
query;
98 command.ExecuteNonQuery();
99 }
100 catch (SqliteException ex)
101 {
102 Debug.LogWarning(
"语句执行异常:" +
ex.ToString());
103 }
104 }
105
106
107 /// <summary>
108 /// 执行查询单个数据
109 /// </summary>
110 /// <returns>数据.</returns>
111 /// <param name="query">SQL语句.</param>
112 public virtual object SingleDataExcute(
string query)
113 {
114 try
115 {
116 command.CommandText =
query;
117 object result =
command.ExecuteScalar();
118
119 return result;
120 }
121 catch (SqliteException ex)
122 {
123 Debug.LogWarning(
"语句执行异常:" +
ex.ToString());
124 return null;
125 }
126 }
127
128 /// <summary>
129 /// 执行查询多个数据
130 /// </summary>
131 /// <returns>The data excute.</returns>
132 /// <param name="query">Query.</param>
133 protected virtual List<ArrayList> MultipleDataExcute(
string query)
134 {
135 try
136 {
137 command.CommandText =
query;
138 reader =
command.ExecuteReader();
139 //结果
140 List<ArrayList> result =
new List<ArrayList>
();
141 while (reader.Read())
142 {
143 //存储单行数据
144 ArrayList tempList =
new ArrayList();
145 //循环存储每一列
146 for (
int i =
0; i < reader.FieldCount; i++
)
147 {
148 tempList.Add(reader.GetValue(i));
149 }
150 //存储当前行
151 result.Add(tempList);
152 }
153 reader.Close();
154 //返回结果
155 return result;
156 }
157 catch (SqliteException ex)
158 {
159 Debug.LogWarning(
"语句执行异常:" +
ex.ToString());
160 //读取连接关闭
161 if (!
reader.IsClosed)
162 {
163 reader.Close();
164 }
165 return null;
166 }
167 }
168 }
Unity (五) Sqlite数据库之:Framework
标签:sed work text except warning tee new ade system