Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections;
- using Mono.Data.Sqlite;
- using System.Data;
- using System;
- public class DataConnectionSql : MonoBehaviour {
- // Use this for initialization
- void Start ()
- {
- string conn = "URI=file:" + Application.dataPath + "/CarSimulator.s3db"; //Path to database.
- IDbConnection dbconn;
- dbconn = (IDbConnection) new SqliteConnection(conn);
- dbconn.Open(); //Open connection to the database.
- IDbCommand dbcmd = dbconn.CreateCommand();
- Debug.Log("DB CONNECTED");
- //
- // string sql;
- //
- // sql = "CREATE TABLE highscores (name VARCHAR(20), score INT)";
- // dbcmd.CommandText = sql;
- // dbcmd.ExecuteNonQuery();
- //
- // sql = "INSERT INTO highscores (name, score) VALUES ('Me', 3000)";
- // dbcmd.CommandText = sql;
- // dbcmd.ExecuteNonQuery();
- //
- // sql = "insert into highscores (name, score) values ('Myself', 6000)";
- // dbcmd.CommandText = sql;
- // dbcmd.ExecuteNonQuery();
- //
- // sql = "insert into highscores (name, score) values ('And I', 9001)";
- // dbcmd.CommandText = sql;
- // dbcmd.ExecuteNonQuery();
- string sqlQuery = "select * from highscores order by score desc";
- dbcmd.CommandText = sqlQuery;
- Debug.Log("SQL QUERY RUNNED");
- IDataReader reader = dbcmd.ExecuteReader();
- Debug.Log("SQL READING TABLE");
- while (reader.Read())
- {
- string name = reader.GetString(0);
- int value = reader.GetInt32(1);
- // int rand = reader.GetInt32(2);
- // bool carpurchased = reader.GetBoolean(3);
- Debug.Log( "CarUniquevalue= "+value+" Drivername ="+name+" Speed =" );
- }
- reader.Close();
- reader = null;
- dbcmd.Dispose();
- dbcmd = null;
- dbconn.Close();
- dbconn = null;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement