I'm trying to read in data from a database, I have a table "HighScores" with the columns "PlayerName" and "PlayerScore", the code I'm currently using is this.. Code (CSharp): while (SQLReader.Read()) { Debug.Log(SQLReader.GetString(1)); //THIS IS THE PROBLEM, IT RETURNS COLUMN NAME. NOT DATA PlayerInformation nextClass = new PlayerInformation(SQLReader.GetString(1), SQLReader.GetInt32(2)); //Makes a new class and adds the score and values PlayerInformationList.Add(nextClass); //adds this class to the list } The problem is SQLReader.GetString(1) returns the name of the name of column 1 not it's contents. I'm not too familiar with SQL so this has me kind of stumped. Also, I want to be able to read in data from every row, I though putting it into a while SQLReader.Read loop would do that, but now I'm not too sure.. Any thoughts? Thanks, Fred T.A
Your naming convention is confusing, since it's typical to have class names capitalized and local variables lower case. I would include the code above that to help with the context. It'd be good to show your SQL too. FYI, when you do GetString() or whatever, the first column starts with zero, in case you didn't know. That said, I use Simple SQL, which makes working with SQLite very easy: https://www.assetstore.unity3d.com/en/#!/content/3845
Ok, here's the entire method... Code (CSharp): PlayerInformation[] QueryDatabase() { SQLConnection.Open(); IDbCommand SQLCommand = SQLConnection.CreateCommand(); SQLCommand.CommandText = "SELECT 'PlayerName', 'Score' FROM HighScores"; IDataReader SQLReader = SQLCommand.ExecuteReader(); List<PlayerInformation> PlayerInformationList = new List<PlayerInformation>(); while (SQLReader.Read()) { Debug.Log(SQLReader.GetString(1)); //THIS IS THE PROBLEM, IT RETURNS COLUMN NAME. NOT DATA PlayerInformation nextClass = new PlayerInformation(SQLReader.GetString(1), SQLReader.GetInt32(2)); //Makes a new class and adds the score and values PlayerInformationList.Add(nextClass); //adds this class to the list } SQLReader.Close(); SQLConnection.Close(); SQLCommand.Dispose(); PlayerInformation[] PlayerInformationArray = new PlayerInformation[PlayerInformationList.Count]; return PlayerInformationArray; } And this is the PlayerInformation Class: Code (CSharp): public class PlayerInformation { public string PlayerName; public int PlayerScore; public PlayerInformation(string playerNameToSet, int playerScoreToSet) { PlayerName = playerNameToSet; PlayerScore = playerScoreToSet; } } So what your saying is, the problem is that "0" refers to the title of the column, how can I miss out 0?
He's saying that when using GetString and the other Get* methods, the index starts with 0, not 1. GetString(1) will return the second column, not the first. You may very well already know this, but it's not clear and it's a common mistake so was worth mentioning. Still, that is unlikely to be the problem. Your problem actually is the SQL: Code (CSharp): "SELECT 'PlayerName', 'Score' FROM HighScores"; You're passing quotes around PlayerName and Score. This makes them strings, so the result set will contain "PlayerName" and "Score" as values rather than the actual data in the columns. If you want to select the actual values then remove the quotes: Code (CSharp): "SELECT PlayerName, Score FROM HighScores"; And actually your GetString(1) and GetInt32(2) are incorrect with that SQL. Your first column would be GetString(0), and the second GetInt32(1). Kudos for not doing "select * from...".