- Home /
Cannot run database when convert to exe or play in android devices?
Hi all, im trying to make online game. i already can connect to online MySQL server.
When i test it on game view in unity it's working, but when i convert it into exe or when i played in my android devices it's not working.
Can anyone tell me why it's not working when i played it in exe or in my mobile device?
This is my code :
using UnityEngine;
using System.Collections;
using System.Data;
using MySql.Data.MySqlClient;
public class DatabaseCS : MonoBehaviour {
private static IDbConnection dbConnection;
// Use this for initialization
void Start () {
}
public void OnGUI () {
openSqlConnection();
IDataReader reader = doQuery("SELECT Name FROM unity3d WHERE Name = 'Cube'");
while (reader.Read()) {
string myVar = (string) reader["Name"];
GUI.Button(new Rect(Screen.width / 2 - 50,Screen.height / 2 - 50,100,30), myVar);
}
}
public void OnApplicationQuit() {
closeSqlConnection();
}
private static void openSqlConnection() {
print ("Open");
string connectionString =
"Server=myserver;" +
"Database=mydatabase;" +
"User ID = myuser;" +
"Password=mypassword;" +
"Pooling=false";
dbConnection = new MySqlConnection(connectionString);
dbConnection.Open();
Debug.Log("Connected to database.");
}
private static void closeSqlConnection() {
dbConnection.Close();
dbConnection = null;
Debug.Log("Disconnected from database.");
}
public static IDataReader doQuery(string sqlQuery) {
IDbCommand dbCommand = dbConnection.CreateCommand();
dbCommand.CommandText = sqlQuery;
IDataReader reader = dbCommand.ExecuteReader();
dbCommand.Dispose();
dbCommand = null;
return reader;
}
// Update is called once per frame
void Update () {
}
}
Thanks
What errors do you get from the PC standalone build? Do you have an Android version of $$anonymous$$ySql.Data.$$anonymous$$ySqlClient?
Answer by quantum_rez · Dec 11, 2012 at 07:45 AM
i don't know what's the error, i can't see it.
what i know is my gui button not appear when i play in pc standalone and my android.
this is the code : while (reader.Read()) {
string myVar = (string) reader["Name"];
GUI.Button(new Rect(Screen.width / 2 - 50,Screen.height / 2 - 50,100,30), myVar);
}
but when i change my gui button without database it's work in pc standalone and my android.
Like this : GUI.Button(new Rect(Screen.width / 2 - 50,Screen.height / 2 - 50,100,30), "My Button");
where i can get MySql.Data.MySqlClient for android?
Thanks
Answer by Bunny83 · Dec 11, 2012 at 11:54 AM
First: NEVER direct connect to a database from a client. The client have to know your username / password and can change everything in your database. Usually you would use a webserver with a phph script which handles database access on the server.
Next thing is OnGUI is called multiple times per frame. You create, destroy and query the database at least 2 times per frame!
Your answer
Follow this Question
Related Questions
Workflow Question with Unity and online Database 0 Answers
Android Mysql Database online 3 Answers
Mobile Database for Unity 0 Answers
mystery MYSQL error .. 2 Answers
Update mySQL at game runtime ??? 2 Answers