- Home /
How to connect Mysql from Android build.
Mysql db connection is working perfectly from unity editor but when I tested from Android device getting the following error, "Exception has been thrown by the target of an invocation." In the InnerException -"System.ArgumentNullException:Argument cannot be null.Paramerter name:value at System.string.Join(System.String seperator,System.String[] value) in < filename unknown>"
I am using AWS for my database environment. Using the following code to connect MySQL : private void Awake() { if (Instance == null) Instance = this; else if (Instance != null) Destroy(gameObject);
serverMainName =""
serverDatabase =""
serverUsername = ""
serverPassword = ""
FormManager.Instance.outcomeText.text = "Awake";
FormManager.Instance.errorPage.SetActive(true);
InitializeConnection();
}
// Used for creating initial connection to the server
public void InitializeConnection()
{
string connectionString;
connectionString = "SERVER=" + serverMainName
+ ";DATABASE=" + serverDatabase + ";UID="
+ serverUsername + ";PASSWORD=" + serverPassword + ";";
FormManager.Instance.outcomeText.text = "InitializeConnection"+ connectionString;
FormManager.Instance.errorPage.SetActive(true);
connection = new MySqlConnection(connectionString);
}
// Open the connection to send a message
private bool OpenConnection()
{
try
{
connection.Open();
return true;
}
catch (MySqlException e)
{
FormManager.Instance.outcomeText.text = "Open Connection: " + e.Number + " || " + e.Message;
FormManager.Instance.errorPage.SetActive(true);
return false;
}
}
Any help on this will be thankful ...
Answer by GothSeiDank · Oct 31, 2017 at 08:48 PM
NEVER, EVER do that. NEVER expose your database in a game script. C# scripts can be decompiled easily and this just screams for getting hacked. Access your DB indirectly through a game server and don't trust any sent data, which means, verify that the data is actually valid before you store it.
Read more here:
Thank you for your quick response.Unfortunately, I am not a unity programmer by experience. the above code was written by my X-colleague and I am trying to make it work.It would be great if you can direct me to any useful tutorial to put the code for $$anonymous$$ySQL connection
I am sorry, but this really isn't something you can explain well in a simple forums post or tutorial. But, for a VERY basic rundown, you need to program a game server that accepts connections, the game sends the data to the game server (for example as a JSON string), the server must validate it and then the game server makes the connection to the DB and stores it, if it is deemed valid. That way the access data for the database cannot fall into wrong hands. For access to the game server you can also create accounts for your players, for example via Google Play Games Authentication on Android.
Do you recommend Rest web service for the server side to handle DB activities?