Advertisement
Guest User

Untitled

a guest
Oct 23rd, 2019
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.41 KB | None | 0 0
  1. using System.Collections.Generic;
  2. using System.Data;
  3. using Mono.Data.Sqlite;
  4. using UnityEngine;
  5.  
  6. public class DBWrapper {
  7.  
  8. private static SqliteConnection conn = null;
  9. private static SqliteCommand readCmd = null;
  10.  
  11. private static string DBPath() {
  12. return "URI=file:Assets/Resources/database.sqlite";
  13. }
  14.  
  15. private static void MakeHandle() {
  16. if (conn == null) {
  17. conn = new SqliteConnection(DBPath());
  18. conn.Open();
  19. }
  20. }
  21.  
  22. public static SqliteDataReader Query(string query, List<SqliteParameter> queryParams) {
  23. MakeHandle();
  24. if (DBWrapper.readCmd == null) {
  25. DBWrapper.readCmd = conn.CreateCommand();
  26. }
  27. readCmd.CommandType = CommandType.Text;
  28. readCmd.CommandText = query;
  29. if (queryParams != null) {
  30. foreach (var qp in queryParams) {
  31. readCmd.Parameters.Add(qp);
  32. }
  33. }
  34. return readCmd.ExecuteReader();
  35. }
  36. }
  37.  
  38.  
  39. ...
  40.  
  41.  
  42. using (var reader = DBWrapper.Query("SELECT * FROM Ships;", null)) {
  43. Debug.Log("Query time");
  44. while (reader.Read()) {
  45. var fields = new object[reader.FieldCount];
  46. Debug.Log("Got read line " + string.Join(",", fields));
  47. var ship = new ShipAsset(reader);
  48. ships[ship.dbName] = ship;
  49. }
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement