Advertisement
Guest User

Untitled

a guest
Mar 31st, 2017
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.06 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using MySql.Web;
  5. using MySql.Data.MySqlClient;
  6. using System.Data;
  7.  
  8.  
  9.  
  10.  
  11.  
  12. public class DatabaseConnector : MonoBehaviour
  13. {
  14. public struct HighscoreInfo
  15. {
  16. string username;
  17. int playerScore;
  18. }
  19.  
  20. private MySqlConnection connection;
  21. private string server;
  22. private string database;
  23. private string uid;
  24. private string password;
  25.  
  26.  
  27. // use this for initialization
  28. void DBconnect()
  29. {
  30. Initialize();
  31.  
  32. }
  33. void Initialize()
  34. {
  35. server = "fornow";
  36. database = "fornow";
  37. uid = "u408607036_score";
  38. password = "killerswag";
  39. string connectionstring;
  40. connectionstring = "server=" + server + ";" + "database=" +
  41. database + ";" + "uid=" + uid + ";" + "password=" + password + ";";
  42.  
  43. connection = new MySqlConnection(connectionstring);
  44. }
  45. // update is called once per frame
  46. private bool OpenConnection()
  47. {
  48. try
  49. {
  50. connection.Open();
  51. return true;
  52. }
  53. catch (MySqlException ex)
  54. {
  55. switch (ex.Number)
  56. {
  57. case 0:
  58. Debug.LogWarning("Cannot connect to the server");
  59. break;
  60. case 1045:
  61. Debug.LogWarning("Invalid username/password, please try again");
  62. break;
  63. }
  64. return false;
  65. }
  66.  
  67. }
  68. private bool CloseConnection()
  69. {
  70. try
  71. {
  72. connection.Close();
  73. return true;
  74. }
  75. catch (MySqlException ex)
  76. {
  77. Debug.LogWarning(ex.Message);
  78. return false;
  79. }
  80. }
  81. public void Insert()
  82. {
  83. string query = "INSERT INTO playerscores (username, playerscore)";
  84. if (this.OpenConnection() == true)
  85. {
  86. MySqlCommand cmd = new MySqlCommand();
  87. cmd.CommandText = query;
  88. cmd.Connection = connection;
  89.  
  90.  
  91. cmd.ExecuteNonQuery();
  92.  
  93. this.CloseConnection();
  94. }
  95. }
  96. public void UpdateInfo()
  97. {
  98. string query = string.Format("UPDATE playerscores SET username='{0}', playerscore='{1}'", "namnet på splearen", 400000);
  99. int count = -1;
  100. }
  101. public HighscoreInfo[] Select()
  102. {
  103. string query = "SELECT * FROM playerscores";
  104. }
  105. public int Count()
  106. {
  107. string query = "SELECT count(*) FROM playerscores";
  108. int count = -1;
  109. if (this.OpenConnection() == true)
  110. {
  111. MySqlCommand cmd = new MySqlCommand(query, connection);
  112. count = int.Parse(cmd.ExecuteScalar() + "");
  113. this.CloseConnection();
  114. return count;
  115.  
  116.  
  117. }
  118. else
  119. {
  120. return count;
  121. }
  122. }
  123. public void Backup()
  124. {
  125. try
  126. {
  127. DateTime
  128. }
  129.  
  130.  
  131. }
  132. public void Restore()
  133. {
  134.  
  135. }
  136. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement