Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using MySql.Web;
- using MySql.Data.MySqlClient;
- using System.Data;
- public class DatabaseConnector : MonoBehaviour
- {
- public struct HighscoreInfo
- {
- string username;
- int playerScore;
- }
- private MySqlConnection connection;
- private string server;
- private string database;
- private string uid;
- private string password;
- // use this for initialization
- void DBconnect()
- {
- Initialize();
- }
- void Initialize()
- {
- server = "fornow";
- database = "fornow";
- uid = "u408607036_score";
- password = "killerswag";
- string connectionstring;
- connectionstring = "server=" + server + ";" + "database=" +
- database + ";" + "uid=" + uid + ";" + "password=" + password + ";";
- connection = new MySqlConnection(connectionstring);
- }
- // update is called once per frame
- private bool OpenConnection()
- {
- try
- {
- connection.Open();
- return true;
- }
- catch (MySqlException ex)
- {
- switch (ex.Number)
- {
- case 0:
- Debug.LogWarning("Cannot connect to the server");
- break;
- case 1045:
- Debug.LogWarning("Invalid username/password, please try again");
- break;
- }
- return false;
- }
- }
- private bool CloseConnection()
- {
- try
- {
- connection.Close();
- return true;
- }
- catch (MySqlException ex)
- {
- Debug.LogWarning(ex.Message);
- return false;
- }
- }
- public void Insert()
- {
- string query = "INSERT INTO playerscores (username, playerscore)";
- if (this.OpenConnection() == true)
- {
- MySqlCommand cmd = new MySqlCommand();
- cmd.CommandText = query;
- cmd.Connection = connection;
- cmd.ExecuteNonQuery();
- this.CloseConnection();
- }
- }
- public void UpdateInfo()
- {
- string query = string.Format("UPDATE playerscores SET username='{0}', playerscore='{1}'", "namnet på splearen", 400000);
- int count = -1;
- }
- public HighscoreInfo[] Select()
- {
- string query = "SELECT * FROM playerscores";
- }
- public int Count()
- {
- string query = "SELECT count(*) FROM playerscores";
- int count = -1;
- if (this.OpenConnection() == true)
- {
- MySqlCommand cmd = new MySqlCommand(query, connection);
- count = int.Parse(cmd.ExecuteScalar() + "");
- this.CloseConnection();
- return count;
- }
- else
- {
- return count;
- }
- }
- public void Backup()
- {
- try
- {
- DateTime
- }
- }
- public void Restore()
- {
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement