- Home /
How does one connect an Android app to a remote database?
The question should make it clear. I've got a local database running and in the editor it works perfectly, but now I wonder how to make it connect to a remotely hosted database.
Here is the code I use to make it connect:
private static void openSqlConnection()
{
string connectionString = "Server=localhost;" +
"Database=pokedex;" +
"User ID=root;" +
"Password=;" +
"Pooling=false";
dbConnection = new MySqlConnection(connectionString);
dbConnection.Open();
Debug.Log("Connected to database.");
}
I get that User ID and Password would be the user and pass to the database, but I don't know what to fill in in the Server spot. I've practiced a little bit of PHP and with that a database always is on localhost, since usually the PHP files are also on that same server.
But since I'm making an Android app, the database will not be local, but hosted somewhere else. Could anyone give me a clear idea of where I should look? (I've been looking all over the internet, but I can't find anything remotely helping)
Answer by zharramadar · Aug 06, 2014 at 07:30 PM
Well, I don't know if an Android device will have any problems or not directly acessing a MySQL database, I would surely put a webservice layer between the game and the database, but answering to your question, you should put the IP or the URL to the server where the MySQL engine is hosted. Also, the MySQL ports in the server should be open and correctly accepting external requests.
Still, not sure if Android would require anything security-wise to allow you to remotely access ports rather than 80, you would have to check this out too.
Thank you, it worked like a charm. I don't really think extra security would be necessary, since it is not really a game, more like an information app. People will only click on things and information will appear, they will not enter their own.
I can't connect my android build with my remote DB :S.. it works on editor but after build the game do not connect. Do you have some example to share? :$
Your answer
![](https://koobas.hobune.stream/wayback/20220613155958im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
SQLite Transaction Update Problem on Android 0 Answers
Unity Remote 5 not connecting to HTC 10 0 Answers
ultility app 0 Answers
Android Mysql Database online 3 Answers
Saving Database from server to local and read from there (android) 0 Answers