I'm trying to create an Android App that connects to a MySQL database. Is there an easy way to do this? Maybe a guide that I have missed? This seems like a common task but for some reason I can't find much information about it.
-
2http://www.vogella.com/articles/AndroidSQLite/article.html – Ahmad Oct 21 '12 at 14:30
-
5@Ahmad: SQLite != MySQL. – Paul-Jan Oct 21 '12 at 14:39
-
1I've heard from a development team - but have _not_ tried it myself - that plain old JDBC works just fine in that configuration. You'll need to include the driver in your project and target the connection to your MySQL server. – full.stack.ex Oct 21 '12 at 14:50
-
@inzajt do you mean a web based mysql db? if you mean the internal db api then like ahmad said you probably mean sqlite – MikeIsrael Oct 21 '12 at 14:50
-
I mean a web based mysql db, so I can use the same db for a webapp – Inzajt Oct 21 '12 at 14:56
-
AFAIK I know `android` will not allow this. You need an `API` for this. – Mohsin Naeem Oct 21 '12 at 15:15
-
1@full.stack.ex is probably right! JDBC should work fine. :) – Adarsh H S Oct 21 '12 at 15:58
-
Look at a related link: it seems to have worked for someone: http://stackoverflow.com/a/6554930/1665128 – full.stack.ex Oct 21 '12 at 16:37
3 Answers
You post data to a PHP script
which will store the data in your application. This is a pretty neat guide

- 8,861
- 16
- 75
- 143
The original Android code from this question really works:
Android + MySQL using com.mysql.jdbc.Driver
You should use a compatible driver (I guess, without Java 1.7 advanced features). As recommended here (mysql-connector-java-3.0.17-ga-bin.jar):
https://stackoverflow.com/a/6554930/1665128
And add a permission to your manifest:
<uses-permission android:name="android.permission.INTERNET"/>
Of course, alternatively, you can wrap your DB into a web service or whatever.

- 1
- 1

- 1,747
- 2
- 11
- 13
You need two things
- Web service
- Client
Web service: This is basically a website, that will get information from MySQL and display it as JSON. You can use XML or something else, but JSON has proven to be the easiest for me.
PHP & MySQL tutorial: http://www.w3schools.com/php/php_mysql_intro.asp
I will assume you can do that, it's not all that hard. What you usualy get is an array from database. You use that array to make JSON out of it, using
<?php
echo json_encode($my_array);
?>
What you will get is something like this:
{
"employees": [
{ "firstName":"John" , "lastName":"Doe" },
{ "firstName":"Anna" , "lastName":"Smith" },
{ "firstName":"Peter" , "lastName":"Jones" }
]
}
Client: So far I have only used library called GSON, and it worked great for me. I think you will find all examples there on how to use it. What it does is read that JSON through URL you have provided and fills up your objects.

- 5,321
- 3
- 35
- 57
-
It's hard to choose a "correct" answer for this type of question but this one was the best explanation – Inzajt Oct 29 '12 at 09:50