質問編集履歴
1
コードが冗長であったため、いったん非表示にしました
title
CHANGED
File without changes
|
body
CHANGED
@@ -13,312 +13,7 @@
|
|
13
13
|
|
14
14
|
しかし、この方の質問は解答欄にあるサイト解決されたようですが、同じように試してみてもまったく状況は変わらず、Unity上でデータを追加してもpopSQLiteでは変化なしでした。
|
15
15
|
|
16
|
-
いったい自分のコードのどこに原因があったのでしょうか。
|
17
|
-
お教えいただけますと幸いです。よろしくお願いいたします。
|
18
16
|
|
19
|
-
### SqliteDatabase.cs
|
20
|
-
|
21
|
-
```C#
|
22
|
-
public class SqliteDatabase
|
23
|
-
{
|
24
|
-
|
25
|
-
[DllImport("sqlite3", EntryPoint = "sqlite3_exec")]
|
26
|
-
private static extern int sqlite3_exec(IntPtr db, string sql, IntPtr callback, IntPtr args, out IntPtr errorMessage);
|
27
|
-
|
28
|
-
[DllImport("sqlite3", EntryPoint = "sqlite3_open")]
|
29
|
-
private static extern int sqlite3_open (string filename, out IntPtr db);
|
30
|
-
|
31
|
-
[DllImport("sqlite3", EntryPoint = "sqlite3_close")]
|
32
|
-
private static extern int sqlite3_close (IntPtr db);
|
33
|
-
|
34
|
-
[DllImport("sqlite3", EntryPoint = "sqlite3_prepare_v2")]
|
35
|
-
private static extern int sqlite3_prepare_v2 (IntPtr db, string zSql, int nByte, out IntPtr ppStmpt, IntPtr pzTail);
|
36
|
-
|
37
|
-
[DllImport("sqlite3", EntryPoint = "sqlite3_step")]
|
38
|
-
private static extern int sqlite3_step (IntPtr stmHandle);
|
39
|
-
|
40
|
-
[DllImport("sqlite3", EntryPoint = "sqlite3_finalize")]
|
41
|
-
private static extern int sqlite3_finalize (IntPtr stmHandle);
|
42
|
-
|
43
|
-
[DllImport("sqlite3", EntryPoint = "sqlite3_errmsg")]
|
44
|
-
private static extern IntPtr sqlite3_errmsg (IntPtr db);
|
45
|
-
|
46
|
-
[DllImport("sqlite3", EntryPoint = "sqlite3_column_count")]
|
47
|
-
private static extern int sqlite3_column_count (IntPtr stmHandle);
|
48
|
-
|
49
|
-
[DllImport("sqlite3", EntryPoint = "sqlite3_column_name")]
|
50
|
-
private static extern IntPtr sqlite3_column_name (IntPtr stmHandle, int iCol);
|
51
|
-
|
52
|
-
[DllImport("sqlite3", EntryPoint = "sqlite3_column_type")]
|
53
|
-
private static extern int sqlite3_column_type (IntPtr stmHandle, int iCol);
|
54
|
-
|
55
|
-
[DllImport("sqlite3", EntryPoint = "sqlite3_column_int")]
|
56
|
-
private static extern int sqlite3_column_int (IntPtr stmHandle, int iCol);
|
57
|
-
|
58
|
-
[DllImport("sqlite3", EntryPoint = "sqlite3_column_text")]
|
59
|
-
private static extern IntPtr sqlite3_column_text (IntPtr stmHandle, int iCol);
|
60
|
-
|
61
|
-
[DllImport("sqlite3", EntryPoint = "sqlite3_column_double")]
|
62
|
-
private static extern double sqlite3_column_double (IntPtr stmHandle, int iCol);
|
63
|
-
|
64
|
-
[DllImport("sqlite3", EntryPoint = "sqlite3_column_blob")]
|
65
|
-
private static extern IntPtr sqlite3_column_blob (IntPtr stmHandle, int iCol);
|
66
|
-
|
67
|
-
[DllImport("sqlite3", EntryPoint = "sqlite3_column_bytes")]
|
68
|
-
private static extern int sqlite3_column_bytes (IntPtr stmHandle, int iCol);
|
69
|
-
|
70
|
-
private IntPtr _connection;
|
71
|
-
|
72
|
-
private bool IsConnectionOpen { get; set; }
|
73
|
-
|
74
|
-
private string pathDB;
|
75
|
-
|
76
|
-
#region Public Methods
|
77
|
-
|
78
|
-
public SqliteDatabase(string dbName)
|
79
|
-
{
|
80
|
-
pathDB = System.IO.Path.Combine(Application.persistentDataPath, dbName);
|
81
|
-
string sourcePath = System.IO.Path.Combine(Application.streamingAssetsPath, dbName);
|
82
|
-
if (!System.IO.File.Exists(pathDB) )
|
83
|
-
{
|
84
|
-
if (sourcePath.Contains("://"))
|
85
|
-
{
|
86
|
-
WWW www = new WWW(sourcePath);
|
87
|
-
while (!www.isDone) {; }
|
88
|
-
|
89
|
-
if (String.IsNullOrEmpty(www.error))
|
90
|
-
{
|
91
|
-
System.IO.File.WriteAllBytes(pathDB, www.bytes);
|
92
|
-
}
|
93
|
-
else
|
94
|
-
{
|
95
|
-
CanExQuery = false;
|
96
|
-
}
|
97
|
-
|
98
|
-
}
|
99
|
-
else
|
100
|
-
{
|
101
|
-
if (System.IO.File.Exists(sourcePath))
|
102
|
-
{
|
103
|
-
System.IO.File.Copy(sourcePath, pathDB, true);
|
104
|
-
|
105
|
-
}
|
106
|
-
else
|
107
|
-
{
|
108
|
-
CanExQuery = false;
|
109
|
-
Debug.Log("ERROR: the file DB named " + dbName + " doesn't exist in the StreamingAssets Folder, please copy it there.");
|
110
|
-
}
|
111
|
-
}
|
112
|
-
}
|
113
|
-
}
|
114
|
-
private void Open()
|
115
|
-
{
|
116
|
-
this.Open(pathDB);
|
117
|
-
}
|
118
|
-
|
119
|
-
private void Open(string path)
|
120
|
-
{
|
121
|
-
if (IsConnectionOpen)
|
122
|
-
{
|
123
|
-
throw new SqliteException("There is already an open connection");
|
124
|
-
}
|
125
|
-
|
126
|
-
if (sqlite3_open(path, out _connection) != SQLITE_OK)
|
127
|
-
{
|
128
|
-
throw new SqliteException("Could not open database file: " + path);
|
129
|
-
}
|
130
|
-
|
131
|
-
IsConnectionOpen = true;
|
132
|
-
}
|
133
|
-
private void Close()
|
134
|
-
{
|
135
|
-
if (IsConnectionOpen)
|
136
|
-
{
|
137
|
-
sqlite3_close(_connection);
|
138
|
-
}
|
139
|
-
|
140
|
-
IsConnectionOpen = false;
|
141
|
-
}
|
142
|
-
|
143
|
-
public void ExecuteNonQuery(string query)
|
144
|
-
{
|
145
|
-
if (!CanExQuery)
|
146
|
-
{
|
147
|
-
Debug.Log("ERROR: Can't execute the query, verify DB origin file");
|
148
|
-
return;
|
149
|
-
}
|
150
|
-
|
151
|
-
if (!IsTransaction)
|
152
|
-
{
|
153
|
-
Open();
|
154
|
-
}
|
155
|
-
|
156
|
-
if (!IsConnectionOpen)
|
157
|
-
{
|
158
|
-
throw new SqliteException("SQLite database is not open.");
|
159
|
-
}
|
160
|
-
|
161
|
-
IntPtr stmHandle = Prepare(query);
|
162
|
-
|
163
|
-
if (sqlite3_step(stmHandle) != SQLITE_DONE)
|
164
|
-
{
|
165
|
-
throw new SqliteException("Could not execute SQL statement.");
|
166
|
-
}
|
167
|
-
|
168
|
-
Finalize(stmHandle);
|
169
|
-
|
170
|
-
if (!IsTransaction)
|
171
|
-
{
|
172
|
-
Close();
|
173
|
-
}
|
174
|
-
}
|
175
|
-
public DataTable ExecuteQuery(string query)
|
176
|
-
{
|
177
|
-
if (!CanExQuery)
|
178
|
-
{
|
179
|
-
Debug.Log("ERROR: Can't execute the query, verify DB origin file");
|
180
|
-
return null;
|
181
|
-
}
|
182
|
-
this.Open();
|
183
|
-
if (!IsConnectionOpen)
|
184
|
-
{
|
185
|
-
throw new SqliteException("SQLite database is not open.");
|
186
|
-
}
|
187
|
-
IntPtr stmHandle = Prepare(query);
|
188
|
-
|
189
|
-
int columnCount = sqlite3_column_count(stmHandle);
|
190
|
-
|
191
|
-
var dataTable = new DataTable();
|
192
|
-
for (int i = 0; i < columnCount; i++)
|
193
|
-
{
|
194
|
-
string columnName = Marshal.PtrToStringAnsi(sqlite3_column_name(stmHandle, i));
|
195
|
-
dataTable.Columns.Add(columnName);
|
196
|
-
}
|
197
|
-
while (sqlite3_step(stmHandle) == SQLITE_ROW)
|
198
|
-
{
|
199
|
-
object[] row = new object[columnCount];
|
200
|
-
for (int i = 0; i < columnCount; i++)
|
201
|
-
{
|
202
|
-
switch (sqlite3_column_type(stmHandle, i))
|
203
|
-
{
|
204
|
-
case SQLITE_INTEGER:
|
205
|
-
row[i] = sqlite3_column_int(stmHandle, i);
|
206
|
-
break;
|
207
|
-
|
208
|
-
case SQLITE_TEXT:
|
209
|
-
IntPtr text = sqlite3_column_text(stmHandle, i);
|
210
|
-
row[i] = Marshal.PtrToStringAnsi(text);
|
211
|
-
break;
|
212
|
-
|
213
|
-
case SQLITE_FLOAT:
|
214
|
-
row[i] = sqlite3_column_double(stmHandle, i);
|
215
|
-
break;
|
216
|
-
|
217
|
-
case SQLITE_BLOB:
|
218
|
-
IntPtr blob = sqlite3_column_blob(stmHandle, i);
|
219
|
-
int size = sqlite3_column_bytes(stmHandle, i);
|
220
|
-
byte[] data = new byte[size];
|
221
|
-
Marshal.Copy(blob, data, 0, size);
|
222
|
-
row[i] = data;
|
223
|
-
break;
|
224
|
-
|
225
|
-
case SQLITE_NULL:
|
226
|
-
row[i] = null;
|
227
|
-
break;
|
228
|
-
}
|
229
|
-
}
|
230
|
-
|
231
|
-
dataTable.AddRow(row);
|
232
|
-
}
|
233
|
-
Finalize(stmHandle);
|
234
|
-
this.Close();
|
235
|
-
return dataTable;
|
236
|
-
}
|
237
|
-
public void ExecuteScript(string script)
|
238
|
-
{
|
239
|
-
string[] statements = script.Split(';');
|
240
|
-
|
241
|
-
foreach (string statement in statements)
|
242
|
-
{
|
243
|
-
if (!string.IsNullOrEmpty(statement.Trim()))
|
244
|
-
{
|
245
|
-
ExecuteNonQuery(statement);
|
246
|
-
}
|
247
|
-
}
|
248
|
-
}
|
249
|
-
public void TransactionStart()
|
250
|
-
{
|
251
|
-
Open();
|
252
|
-
|
253
|
-
IsTransaction = true;
|
254
|
-
ExecuteQueryExec("BEGIN");
|
255
|
-
}
|
256
|
-
public void TransactionCommit()
|
257
|
-
{
|
258
|
-
ExecuteQueryExec("COMMIT");
|
259
|
-
|
260
|
-
IsTransaction = false;
|
261
|
-
Close();
|
262
|
-
}
|
263
|
-
public void TransactionRollBack()
|
264
|
-
{
|
265
|
-
ExecuteQueryExec("ROLLBACK");
|
266
|
-
Close();
|
267
|
-
}
|
268
|
-
#endregion
|
269
|
-
|
270
|
-
#region Private Methods
|
271
|
-
private IntPtr Prepare(string query)
|
272
|
-
{
|
273
|
-
IntPtr stmHandle;
|
274
|
-
int byteCount = System.Text.Encoding.UTF8.GetByteCount(query);
|
275
|
-
|
276
|
-
if (sqlite3_prepare_v2(_connection, query, byteCount, out stmHandle, IntPtr.Zero) != SQLITE_OK)
|
277
|
-
{
|
278
|
-
IntPtr errorMsg = sqlite3_errmsg(_connection);
|
279
|
-
throw new SqliteException(Marshal.PtrToStringAnsi(errorMsg));
|
280
|
-
}
|
281
|
-
|
282
|
-
return stmHandle;
|
283
|
-
}
|
284
|
-
|
285
|
-
private void Finalize(IntPtr stmHandle)
|
286
|
-
{
|
287
|
-
if (sqlite3_finalize(stmHandle) != SQLITE_OK)
|
288
|
-
{
|
289
|
-
throw new SqliteException("Could not finalize SQL statement.");
|
290
|
-
}
|
291
|
-
}
|
292
|
-
private bool IsTransaction = false;
|
293
|
-
private void ExecuteQueryExec(string query)
|
294
|
-
{
|
295
|
-
IntPtr stmHandle;
|
296
|
-
|
297
|
-
if (!CanExQuery)
|
298
|
-
{
|
299
|
-
Debug.Log("ERROR: Can't execute the query, verify DB origin file");
|
300
|
-
return;
|
301
|
-
}
|
302
|
-
if (!IsTransaction)
|
303
|
-
{
|
304
|
-
Debug.Log("ERROR: Haven't started a transaction.");
|
305
|
-
return;
|
306
|
-
}
|
307
|
-
if (sqlite3_exec(_connection, query, IntPtr.Zero, IntPtr.Zero, out stmHandle) != SQLITE_OK)
|
308
|
-
{
|
309
|
-
throw new SqliteException("Could not execute SQL statement.");
|
310
|
-
}
|
311
|
-
}
|
312
|
-
#endregion
|
313
|
-
}
|
314
|
-
|
315
|
-
```
|
316
|
-
|
317
|
-
|
318
17
|
### 試したこと
|
319
18
|
|
320
|
-
SQLiteUnityKitに同封されているSqliteDatabase.csを
|
19
|
+
SQLiteUnityKitに同封されているSqliteDatabase.csを書き換えましたが、変化はありませんでした。
|
321
|
-
|
322
|
-
### 補足情報(FW/ツールのバージョンなど)
|
323
|
-
Unity 2020.3.18f1
|
324
|
-
Windows 10
|