1

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.

Ahmad
  • 69,608
  • 17
  • 111
  • 137
Inzajt
  • 135
  • 2
  • 13

3 Answers3

0

You post data to a PHP script which will store the data in your application. This is a pretty neat guide

Tobias Moe Thorstensen
  • 8,861
  • 16
  • 75
  • 143
0

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.

Community
  • 1
  • 1
full.stack.ex
  • 1,747
  • 2
  • 11
  • 13
0

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.

Bojan Kogoj
  • 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