Advertisement
summitgames

DataConnectionSql

Nov 29th, 2015
506
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.68 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3. using Mono.Data.Sqlite;
  4. using System.Data;
  5. using System;
  6.  
  7. public class DataConnectionSql : MonoBehaviour {
  8.  
  9.  
  10.     // Use this for initialization
  11.     void Start ()
  12.     {  
  13.         string conn = "URI=file:" + Application.dataPath + "/CarSimulator.s3db"; //Path to database.
  14.         IDbConnection dbconn;
  15.         dbconn = (IDbConnection) new SqliteConnection(conn);
  16.         dbconn.Open(); //Open connection to the database.
  17.         IDbCommand dbcmd = dbconn.CreateCommand();
  18.         Debug.Log("DB CONNECTED");
  19.  
  20. //
  21. //      string sql;
  22. //     
  23. //      sql = "CREATE TABLE highscores (name VARCHAR(20), score INT)";
  24. //      dbcmd.CommandText = sql;
  25. //      dbcmd.ExecuteNonQuery();
  26. //     
  27. //      sql = "INSERT INTO highscores (name, score) VALUES ('Me', 3000)";
  28. //      dbcmd.CommandText = sql;
  29. //      dbcmd.ExecuteNonQuery();
  30. //     
  31. //      sql = "insert into highscores (name, score) values ('Myself', 6000)";
  32. //      dbcmd.CommandText = sql;
  33. //      dbcmd.ExecuteNonQuery();
  34. //     
  35. //      sql = "insert into highscores (name, score) values ('And I', 9001)";
  36. //      dbcmd.CommandText = sql;
  37. //      dbcmd.ExecuteNonQuery();
  38.  
  39.         string sqlQuery = "select * from highscores order by score desc";
  40.         dbcmd.CommandText = sqlQuery;
  41.         Debug.Log("SQL QUERY RUNNED");
  42.  
  43.         IDataReader reader = dbcmd.ExecuteReader();
  44.  
  45.         Debug.Log("SQL READING TABLE");
  46.  
  47.         while (reader.Read())
  48.         {
  49.  
  50.             string name = reader.GetString(0);
  51.             int value = reader.GetInt32(1);
  52. //          int rand = reader.GetInt32(2);
  53. //          bool carpurchased = reader.GetBoolean(3);
  54.            
  55.             Debug.Log( "CarUniquevalue= "+value+"  Drivername ="+name+"  Speed =" );
  56.         }
  57.         reader.Close();
  58.         reader = null;
  59.         dbcmd.Dispose();
  60.         dbcmd = null;
  61.         dbconn.Close();
  62.         dbconn = null;
  63.     }
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement