- Home /
The question is answered, right answer was accepted
How to pass SQL vars?
Hey, I am not that experienced with SQL in Unity (or rather with c#)
However I am trying to use my webserver (MySQL server) for storing the user data, here's my function a good friend wrote for me to do SQL queries:
// MySQL Query
public static void doQuery(string sqlQuery)
{
IDbCommand dbCommand = dbConnection.CreateCommand();
dbCommand.CommandText = sqlQuery;
IDataReader reader = dbCommand.ExecuteReader();
reader.Close();
reader = null;
dbCommand.Dispose();
dbCommand = null;
}
which works very well for storing data, but In order to store data, there need to be users in the database.
As for the Login/Register, I am trying to use this codesegment:
DB.doQuery("SELECT ID, PlayerPassword FROM users WHERE PlayerName = '"+username+"';");
And that should actually work, but my main concern is:
How are the variables (ID and PlayerPassword) passed to C#/Unity? Or do I have to alter the doQuery function, as it actually returns no values whatsoever (which is unsurprising since the function is void.)
Thanks in advance :3
Just like you said, you should alter the doQuery function in order to get access to the reader.
// just a little example (its faaar from being good but it shouldgive an idea of how to proceed):
//in doQuery, after reader has been initialized.
string[] str = new string[2];
reader.Read() // only one element
str[0] = reader.GetString(reader.GetOrdinal("ID")));
str[1] = reader.GetString(reader.GetOrdinal("PlayerPassword")));
..
..
//end of doQuery
return str;
Answer by Cerbion_ · Jul 14, 2013 at 02:51 AM
Thanks to 'supercouge' the solution is:
(..)
reader.Read ();
int returnValue;
try
{
result[0] = reader.GetString(reader.GetOrdinal("PlayerID"));
returnValue = int.Parse(result[0]);
}
catch (Exception ex)
{
returnValue = -1;
Debug.Log ("Error: "+ex);
}
(..)
And then DB.doQuery(..) returns either -1 if there is no player with that name, or the ID as an integer.
You should also test, in case, that there is one and only one element which is returned by your SQL request.
Follow this Question
Related Questions
MYSQL How do get all of my SELECT information? 2 Answers
Is it possible to create GameObjects dinamically from a server? 1 Answer
using SQL db instead of playerprefs (playerlocation) 0 Answers
Microsoft SQL Server error. 0 Answers
Updating variables from dictionary database in edit mode C# 0 Answers