How to use Mysql on Android

Asked

Viewed 36,150 times

24

After some searches, I saw that the only database that Android can use is Sqlite, It is correct this information?

Because I have an application that connects to an online database (Mysql) and would like to create an application on Android using the same database, it is possible?

If yes, how can I connect to a base mysql?

  • 2

    Sqlite is what Android uses on the device. Outside the device you use what you want.

  • Who and why are you voting to close this question? It fits perfectly into the scope of the site.

  • This question is being discussed at the goal: http://meta.pt.stackoverflow.com/questions/971/comparando-a-experi%C3%Aancia-do-so-com-o-sopt

  • 3

    Take great care with external database access directly from Android apps. This leaves a very large security gap between your app and your database. Any kind of access to "external database" (let’s call it that), should be done via Webservices. Take a look at this article http://www.androidpro.com.br/usando-banco-dataos-externo-android/.

3 answers

19


How is it answered here, it is possible to use an online database, in your case Mysql, as long as you have a:

  • Webservice
  • Client

Webservice You must make the request in the bank and return a response to be processed, usually a JSON, which can be sent, for example, with , as follows:

<?php
   echo json_encode($minha_info);
?>

This will return a JSON for the customer.

Client It is possible to use the package JSON from Java itself, as in this example and retrieve the information on the client. The following example will use the reading of a twitter feed:

@Override
public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);
  String readTwitterFeed = readTwitterFeed();
  try {
    JSONArray jsonArray = new JSONArray(readTwitterFeed);
    Log.i(ParseJSON.class.getName(), "Number of entries " + jsonArray.length());
    for (int i = 0; i < jsonArray.length(); i++) {
      JSONObject jsonObject = jsonArray.getJSONObject(i);
      Log.i(ParseJSON.class.getName(), jsonObject.getString("text"));
    }
  } catch (Exception e) {
    e.printStackTrace();
  }
}

public String readTwitterFeed() {
  StringBuilder builder = new StringBuilder();
  HttpClient client = new DefaultHttpClient();
  HttpGet httpGet = new HttpGet("http://twitter.com/statuses/user_timeline/vogella.json"); //Aqui ele pega o json do tutorial, nessa linha que o seu cliente vai declarar o webservice que enviará o json
  try {
    HttpResponse response = client.execute(httpGet);
    StatusLine statusLine = response.getStatusLine();
    int statusCode = statusLine.getStatusCode();
    if (statusCode == 200) {
      HttpEntity entity = response.getEntity();
      InputStream content = entity.getContent();
      BufferedReader reader = new BufferedReader(new InputStreamReader(content));
      String line;
      while ((line = reader.readLine()) != null) {
        builder.append(line);
      }
    } else {
      Log.e(ParseJSON.class.toString(), "Failed to download file");
    }
  } catch (ClientProtocolException e) {
    e.printStackTrace();
  } catch (IOException e) {
    e.printStackTrace();
  }
  return builder.toString();
}

4

Felipe, it actually works! I myself am developing a system for bars and restaurants with an app and I am using direct connection from Mysql to Android, and, contrary to what most say, does not slow down.

The only caveat is that the android API does not allow you to exchange data over the network (either by Wi-Fi or 3G/4G) using the main thread (Uithread). For this there are the auxiliary classes as a Asynctask, present in API, that assists in the execution of processes in the background. All communication process between the app and the bank should be done through a secondary thread, ie a process in the background (including the connection itself).

I forgot to post an example of how to implement the connection of android with Mysql.

Looking on the net I found this tutorial very interesting and very similar to the way I use the connection.

Android connection to Mysql using the Asynctask class

I hope I helped! Hugs

  • 4

    Hi John, welcome to the site. What you posted got in the middle of the way between a comment to Felipe’s answer and a same answer to the question. If you want to [Dit] and make it a more complete answer (including example codes in the body of the answer), it would be a good one. If you prefer, I can convert to comment for you, just let me know (you would need 50 points on the site to be able to comment on the response of Elipe). Thank you.

  • John blz, I agree with you but it depends a lot on the developer and various care to be taken mainly related to transactions, I had a similar problem and decided to use the direct connection was much faster because it is simple queries and access only via local network(wifi) and not have to go through any service.

0

You will have to build an API (Usually uses json/Rest) so that you can communicate with the server, and it can process the data for you.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.