質問編集履歴

1

コードが冗長であったため、いったん非表示にしました

2021/11/21 08:11

投稿

blueyellow
blueyellow

スコア4

test CHANGED
File without changes
test CHANGED
@@ -28,620 +28,10 @@
28
28
 
29
29
 
30
30
 
31
- いったい自分のコードのどこに原因があったのでしょうか。
32
-
33
- お教えいただけますと幸いです。よろしくお願いいたします。
34
-
35
-
36
-
37
- ### SqliteDatabase.cs
38
-
39
-
40
-
41
- ```C#
42
-
43
- public class SqliteDatabase
44
-
45
- {
46
-
47
-
48
-
49
- [DllImport("sqlite3", EntryPoint = "sqlite3_exec")]
50
-
51
- private static extern int sqlite3_exec(IntPtr db, string sql, IntPtr callback, IntPtr args, out IntPtr errorMessage);
52
-
53
-
54
-
55
- [DllImport("sqlite3", EntryPoint = "sqlite3_open")]
56
-
57
- private static extern int sqlite3_open (string filename, out IntPtr db);
58
-
59
-
60
-
61
- [DllImport("sqlite3", EntryPoint = "sqlite3_close")]
62
-
63
- private static extern int sqlite3_close (IntPtr db);
64
-
65
-
66
-
67
- [DllImport("sqlite3", EntryPoint = "sqlite3_prepare_v2")]
68
-
69
- private static extern int sqlite3_prepare_v2 (IntPtr db, string zSql, int nByte, out IntPtr ppStmpt, IntPtr pzTail);
70
-
71
-
72
-
73
- [DllImport("sqlite3", EntryPoint = "sqlite3_step")]
74
-
75
- private static extern int sqlite3_step (IntPtr stmHandle);
76
-
77
-
78
-
79
- [DllImport("sqlite3", EntryPoint = "sqlite3_finalize")]
80
-
81
- private static extern int sqlite3_finalize (IntPtr stmHandle);
82
-
83
-
84
-
85
- [DllImport("sqlite3", EntryPoint = "sqlite3_errmsg")]
86
-
87
- private static extern IntPtr sqlite3_errmsg (IntPtr db);
88
-
89
-
90
-
91
- [DllImport("sqlite3", EntryPoint = "sqlite3_column_count")]
92
-
93
- private static extern int sqlite3_column_count (IntPtr stmHandle);
94
-
95
-
96
-
97
- [DllImport("sqlite3", EntryPoint = "sqlite3_column_name")]
98
-
99
- private static extern IntPtr sqlite3_column_name (IntPtr stmHandle, int iCol);
100
-
101
-
102
-
103
- [DllImport("sqlite3", EntryPoint = "sqlite3_column_type")]
104
-
105
- private static extern int sqlite3_column_type (IntPtr stmHandle, int iCol);
106
-
107
-
108
-
109
- [DllImport("sqlite3", EntryPoint = "sqlite3_column_int")]
110
-
111
- private static extern int sqlite3_column_int (IntPtr stmHandle, int iCol);
112
-
113
-
114
-
115
- [DllImport("sqlite3", EntryPoint = "sqlite3_column_text")]
116
-
117
- private static extern IntPtr sqlite3_column_text (IntPtr stmHandle, int iCol);
118
-
119
-
120
-
121
- [DllImport("sqlite3", EntryPoint = "sqlite3_column_double")]
122
-
123
- private static extern double sqlite3_column_double (IntPtr stmHandle, int iCol);
124
-
125
-
126
-
127
- [DllImport("sqlite3", EntryPoint = "sqlite3_column_blob")]
128
-
129
- private static extern IntPtr sqlite3_column_blob (IntPtr stmHandle, int iCol);
130
-
131
-
132
-
133
- [DllImport("sqlite3", EntryPoint = "sqlite3_column_bytes")]
134
-
135
- private static extern int sqlite3_column_bytes (IntPtr stmHandle, int iCol);
136
-
137
-
138
-
139
- private IntPtr _connection;
140
-
141
-
142
-
143
- private bool IsConnectionOpen { get; set; }
144
-
145
-
146
-
147
- private string pathDB;
148
-
149
-
150
-
151
- #region Public Methods
152
-
153
-
154
-
155
- public SqliteDatabase(string dbName)
156
-
157
- {
158
-
159
- pathDB = System.IO.Path.Combine(Application.persistentDataPath, dbName);
160
-
161
- string sourcePath = System.IO.Path.Combine(Application.streamingAssetsPath, dbName);
162
-
163
- if (!System.IO.File.Exists(pathDB) )
164
-
165
- {
166
-
167
- if (sourcePath.Contains("://"))
168
-
169
- {
170
-
171
- WWW www = new WWW(sourcePath);
172
-
173
- while (!www.isDone) {; }
174
-
175
-
176
-
177
- if (String.IsNullOrEmpty(www.error))
178
-
179
- {
180
-
181
- System.IO.File.WriteAllBytes(pathDB, www.bytes);
182
-
183
- }
184
-
185
- else
186
-
187
- {
188
-
189
- CanExQuery = false;
190
-
191
- }
192
-
193
-
194
-
195
- }
196
-
197
- else
198
-
199
- {
200
-
201
- if (System.IO.File.Exists(sourcePath))
202
-
203
- {
204
-
205
- System.IO.File.Copy(sourcePath, pathDB, true);
206
-
207
-
208
-
209
- }
210
-
211
- else
212
-
213
- {
214
-
215
- CanExQuery = false;
216
-
217
- Debug.Log("ERROR: the file DB named " + dbName + " doesn't exist in the StreamingAssets Folder, please copy it there.");
218
-
219
- }
220
-
221
- }
222
-
223
- }
224
-
225
- }
226
-
227
- private void Open()
228
-
229
- {
230
-
231
- this.Open(pathDB);
232
-
233
- }
234
-
235
-
236
-
237
- private void Open(string path)
238
-
239
- {
240
-
241
- if (IsConnectionOpen)
242
-
243
- {
244
-
245
- throw new SqliteException("There is already an open connection");
246
-
247
- }
248
-
249
-
250
-
251
- if (sqlite3_open(path, out _connection) != SQLITE_OK)
252
-
253
- {
254
-
255
- throw new SqliteException("Could not open database file: " + path);
256
-
257
- }
258
-
259
-
260
-
261
- IsConnectionOpen = true;
262
-
263
- }
264
-
265
- private void Close()
266
-
267
- {
268
-
269
- if (IsConnectionOpen)
270
-
271
- {
272
-
273
- sqlite3_close(_connection);
274
-
275
- }
276
-
277
-
278
-
279
- IsConnectionOpen = false;
280
-
281
- }
282
-
283
-
284
-
285
- public void ExecuteNonQuery(string query)
286
-
287
- {
288
-
289
- if (!CanExQuery)
290
-
291
- {
292
-
293
- Debug.Log("ERROR: Can't execute the query, verify DB origin file");
294
-
295
- return;
296
-
297
- }
298
-
299
-
300
-
301
- if (!IsTransaction)
302
-
303
- {
304
-
305
- Open();
306
-
307
- }
308
-
309
-
310
-
311
- if (!IsConnectionOpen)
312
-
313
- {
314
-
315
- throw new SqliteException("SQLite database is not open.");
316
-
317
- }
318
-
319
-
320
-
321
- IntPtr stmHandle = Prepare(query);
322
-
323
-
324
-
325
- if (sqlite3_step(stmHandle) != SQLITE_DONE)
326
-
327
- {
328
-
329
- throw new SqliteException("Could not execute SQL statement.");
330
-
331
- }
332
-
333
-
334
-
335
- Finalize(stmHandle);
336
-
337
-
338
-
339
- if (!IsTransaction)
340
-
341
- {
342
-
343
- Close();
344
-
345
- }
346
-
347
- }
348
-
349
- public DataTable ExecuteQuery(string query)
350
-
351
- {
352
-
353
- if (!CanExQuery)
354
-
355
- {
356
-
357
- Debug.Log("ERROR: Can't execute the query, verify DB origin file");
358
-
359
- return null;
360
-
361
- }
362
-
363
- this.Open();
364
-
365
- if (!IsConnectionOpen)
366
-
367
- {
368
-
369
- throw new SqliteException("SQLite database is not open.");
370
-
371
- }
372
-
373
- IntPtr stmHandle = Prepare(query);
374
-
375
-
376
-
377
- int columnCount = sqlite3_column_count(stmHandle);
378
-
379
-
380
-
381
- var dataTable = new DataTable();
382
-
383
- for (int i = 0; i < columnCount; i++)
384
-
385
- {
386
-
387
- string columnName = Marshal.PtrToStringAnsi(sqlite3_column_name(stmHandle, i));
388
-
389
- dataTable.Columns.Add(columnName);
390
-
391
- }
392
-
393
- while (sqlite3_step(stmHandle) == SQLITE_ROW)
394
-
395
- {
396
-
397
- object[] row = new object[columnCount];
398
-
399
- for (int i = 0; i < columnCount; i++)
400
-
401
- {
402
-
403
- switch (sqlite3_column_type(stmHandle, i))
404
-
405
- {
406
-
407
- case SQLITE_INTEGER:
408
-
409
- row[i] = sqlite3_column_int(stmHandle, i);
410
-
411
- break;
412
-
413
-
414
-
415
- case SQLITE_TEXT:
416
-
417
- IntPtr text = sqlite3_column_text(stmHandle, i);
418
-
419
- row[i] = Marshal.PtrToStringAnsi(text);
420
-
421
- break;
422
-
423
-
424
-
425
- case SQLITE_FLOAT:
426
-
427
- row[i] = sqlite3_column_double(stmHandle, i);
428
-
429
- break;
430
-
431
-
432
-
433
- case SQLITE_BLOB:
434
-
435
- IntPtr blob = sqlite3_column_blob(stmHandle, i);
436
-
437
- int size = sqlite3_column_bytes(stmHandle, i);
438
-
439
- byte[] data = new byte[size];
440
-
441
- Marshal.Copy(blob, data, 0, size);
442
-
443
- row[i] = data;
444
-
445
- break;
446
-
447
-
448
-
449
- case SQLITE_NULL:
450
-
451
- row[i] = null;
452
-
453
- break;
454
-
455
- }
456
-
457
- }
458
-
459
-
460
-
461
- dataTable.AddRow(row);
462
-
463
- }
464
-
465
- Finalize(stmHandle);
466
-
467
- this.Close();
468
-
469
- return dataTable;
470
-
471
- }
472
-
473
- public void ExecuteScript(string script)
474
-
475
- {
476
-
477
- string[] statements = script.Split(';');
478
-
479
-
480
-
481
- foreach (string statement in statements)
482
-
483
- {
484
-
485
- if (!string.IsNullOrEmpty(statement.Trim()))
486
-
487
- {
488
-
489
- ExecuteNonQuery(statement);
490
-
491
- }
492
-
493
- }
494
-
495
- }
496
-
497
- public void TransactionStart()
498
-
499
- {
500
-
501
- Open();
502
-
503
-
504
-
505
- IsTransaction = true;
506
-
507
- ExecuteQueryExec("BEGIN");
508
-
509
- }
510
-
511
-  public void TransactionCommit()
512
-
513
- {
514
-
515
- ExecuteQueryExec("COMMIT");
516
-
517
-
518
-
519
- IsTransaction = false;
520
-
521
- Close();
522
-
523
- }
524
-
525
- public void TransactionRollBack()
526
-
527
- {
528
-
529
- ExecuteQueryExec("ROLLBACK");
530
-
531
- Close();
532
-
533
- }
534
-
535
- #endregion
536
-
537
-
538
-
539
- #region Private Methods
540
-
541
- private IntPtr Prepare(string query)
542
-
543
- {
544
-
545
- IntPtr stmHandle;
546
-
547
- int byteCount = System.Text.Encoding.UTF8.GetByteCount(query);
548
-
549
-
550
-
551
- if (sqlite3_prepare_v2(_connection, query, byteCount, out stmHandle, IntPtr.Zero) != SQLITE_OK)
552
-
553
- {
554
-
555
- IntPtr errorMsg = sqlite3_errmsg(_connection);
556
-
557
- throw new SqliteException(Marshal.PtrToStringAnsi(errorMsg));
558
-
559
- }
560
-
561
-
562
-
563
- return stmHandle;
564
-
565
- }
566
-
567
-
568
-
569
- private void Finalize(IntPtr stmHandle)
570
-
571
- {
572
-
573
- if (sqlite3_finalize(stmHandle) != SQLITE_OK)
574
-
575
- {
576
-
577
- throw new SqliteException("Could not finalize SQL statement.");
578
-
579
- }
580
-
581
- }
582
-
583
- private bool IsTransaction = false;
584
-
585
- private void ExecuteQueryExec(string query)
586
-
587
- {
588
-
589
- IntPtr stmHandle;
590
-
591
-
592
-
593
- if (!CanExQuery)
594
-
595
- {
596
-
597
- Debug.Log("ERROR: Can't execute the query, verify DB origin file");
598
-
599
- return;
600
-
601
- }
602
-
603
- if (!IsTransaction)
604
-
605
- {
606
-
607
- Debug.Log("ERROR: Haven't started a transaction.");
608
-
609
- return;
610
-
611
- }
612
-
613
- if (sqlite3_exec(_connection, query, IntPtr.Zero, IntPtr.Zero, out stmHandle) != SQLITE_OK)
614
-
615
- {
616
-
617
- throw new SqliteException("Could not execute SQL statement.");
618
-
619
- }
620
-
621
- }
622
-
623
- #endregion
624
-
625
- }
626
-
627
-
628
-
629
- ```
630
-
631
-
632
-
633
31
 
634
32
 
635
33
  ### 試したこと
636
34
 
637
35
 
638
36
 
639
- SQLiteUnityKitに同封されているSqliteDatabase.csを上記のように書き換えましたが、書き換える前と変化はありませんでした。
37
+ SQLiteUnityKitに同封されているSqliteDatabase.csを書き換えましたが、変化はありませんでした。
640
-
641
-
642
-
643
- ### 補足情報(FW/ツールのバージョンなど)
644
-
645
- Unity 2020.3.18f1
646
-
647
- Windows 10