public static KeyCard GetRandomKeyCard() { Debug.Log(connectionString); IDbConnection dbcon = new SqliteConnection(connectionString); dbcon.Open(); IDbCommand dbcmd = dbcon.CreateCommand(); dbcmd.CommandText = Constants.GET_RANDOM_KEY_CARD; IDataReader reader = dbcmd.ExecuteReader(); KeyCard keyCard = null; while (reader.Read()) { int id = reader.GetInt32(0); int firstToMove = reader.GetInt32(1); string data = reader.GetString(2); int size = reader.GetInt32(3); keyCard = new KeyCard(id, firstToMove, data, size); } reader.Dispose(); dbcmd.Dispose(); dbcon.Close(); return(keyCard); }
void LoadKeyFiles() { string[] files = Directory.GetFiles(Path.Combine(new string[] { Application.dataPath, "Data", "keys" })); // Connect to database string connection = "URI=file:" + Path.Combine(new string[] { Application.dataPath, "Data", "codenames.db" }); IDbConnection dbcon = new SqliteConnection(connection); dbcon.Open(); // Drop and create table IDbCommand dropCmd = dbcon.CreateCommand(); string q_dropTable = "DROP TABLE IF EXISTS keys"; dropCmd.CommandText = q_dropTable; dropCmd.ExecuteReader(); IDbCommand createCmd = dbcon.CreateCommand(); string q_createTable = "CREATE TABLE keys (id INTEGER PRIMARY KEY, firstToMove INTEGER, data TEXT, redCount INTEGER, blueCount INTEGER, blackCount INTEGER, size INTEGER)"; createCmd.CommandText = q_createTable; createCmd.ExecuteReader(); KeyCard _keyCard = new KeyCard(); foreach (string file in files) { if (file.Contains(".meta")) { continue; } _keyCard = JsonUtility.FromJson <KeyCard>(File.ReadAllText(Path.Combine(new string[] { Application.dataPath, "Data", "keys", file }))); IDbCommand cmnd = dbcon.CreateCommand(); cmnd.CommandText = $"INSERT INTO keys (id, firstToMove, data, size) VALUES ({_keyCard.id}, {_keyCard.firstToMove}, '{string.Join(", ", _keyCard.data)}', {_keyCard.size});"; cmnd.ExecuteNonQuery(); } }
private void LoadKeyCardFromDB() { Debug.Log("Drawing KeyCard"); deck = new Deck(); keyCard = SqliteApi.GetRandomKeyCard(); }