What's the best way to connect an Android App to MySQL? - php

I would like to write an Android App (with Android Studio) which uses a MySQL database.
What's the best way to implement it?
I have found on internet that I should write php files which send queries to the database. Then the php page return a JSON object which should be processed by the Android App (or something like that...).
Is this solution safe? Because I have to handle sensitive data like emails or GPS position.
Or else, are there other safe ways to let Android App connect to and retrieve information from the database?

You can call a web service which connects to your server, and a back-end program written in nodejs or PHP or Java or Python or any other programming language which has support (connectors/libraries) for MySQL database.

Related

Connect to Python and Mysql from Android

I am writing an assistant like Siri in Android. The app will perform natural language processing, for which I need NLTK in Python and I already developed python code for it. Now, based on the NLP processing, I also need to search in MySQL database and then send the result back to my android app.
So, what will happen is as follow:
User provides a query in android app, it needs to be transmitted to Python for NLP processing and after that I have to develop some SQL queries for searching in MYSQL, I have to send back the result in Android again.
Here, one big problem is to identify, how I will send data from Android to Python. I found the web framework of Python which is Flask and using it I can pass data through url to python. In this way, I can call the URL from android app and pass the query to the URL, which will be received by flask. This is my plan to build the connection with python. But so far Flask only give me one single ip. Is it possible to assign a domain to it so that my app can work from anywhere?
Secondly, How will send data back from Flask to android? Is there any better way for it? Should I send the SQL queries which I generate in python back to android and then process it again from there? Because connecting to MYSQL is much easier with android>php script>mysql than python>mysql, as it seems now. For python 3.5, which I am using, I am not finding any good framework which will allow me remote access to MySQL. (I tried PyMysql, Python MySQL connector has no support for python 3)
It would be great if anyone have any better idea. This implementation seems very complex to me.

Importing php json file to swift

I have a website that uses MySQL database.
I'm trying to build an iOS app for it, so I wanted to use Swift to import information from the database and insert new information to it. I read that for the reading part I should use a PHP file to create a Json file, read that and extract the data. But I can't find the way to do it with swift.
Is there a way to connect iOS apps (written in Swift) to MySQL?
In the client-server architecture, the client is completely separate from the server, and they just exchange data thanks to a common "language" (in your case, certain fields encoded in JSON).
Your client is your iPhone app, written in Swift (but that's irrelevant).
You now need to build a server, which is entirely separate from the app. You don't need Swift for that. You will need a server (for example a cheap cloud VPS on Amazon EC2, Rackspace Cloud Servers, Microsoft Azure...) and you will have to create another application that runs on that server.
If it's just to pull data from a MySQL database, you can easily make that in PHP. Or you could use Node.js (which uses JavaScript: it's among the "trendiest" technologies at the moment), Ruby, Python, etc.For an example that uses PHP, you can check: https://stackoverflow.com/a/22367600/192024
To read the JSON data in your iOS app, then, you can use the builtin libraries: https://developer.apple.com/library/ios/DOCUMENTATION/Foundation/Reference/NSJSONSerialization_Class/index.html (it's available in Swift too)
I don't know what your app is doing, but if you want to ignore all the things with the backend (the server) you can always consider something like Parse Core and let somebody else take care of the backend.

Does an android app follow client side/server side architechture with regards to database?

I am an android newbie who is coming from PHP background. In PHP basically what happens is that
all of the database connections happen in php code(server side) and then it later coverts in html code(client side).
From what I have seen in android for database connection, we write a service in php using mysql. And from our Android java class, we make the service call(always aysnc I think), and then when the result comes, we update the UI(kinda like Ajax architecture and gwt).
The system above makes sense to me. I read somewhere that even though using JDBC is not practical on android, it can be still be used. Let' say for example's sake, we want to make the database call through JDBC. In a normal web app, I would put it in a servelet. But in android, we don't have that. All we have is UI widgets code.
So just to the understand the architecture, could someone please explain to me where I would make the JDBC call in the code? or in broader terms does it's architecture differ from traditional client/server side?
Like Php:
A brief recap of what you used to do in PHP is as follows:
Connects to MySQL server using PHP
Query the server using PHP function
MySQL server receives, parse the query and send the results back
PHP parses the response and then you displays (or do anything with) it
PHP has built in support for MySQL library.
Now in android we have built in support for SQLite. But the difference is SQLite stores its data on the device itself not on the server.
For database operations same procedure is followed using SQLite:
Using Android(Java) activity it connects to SQLite database
A query is sent to database
SQLite engine receives, parses the query and sends the results back
Android activity receives the result and then you can do whatever you want with it
But, Here comes the issue
This is only good if you want to store the data locally, like saving a users score.
Suppose, you want to have a leader-board in a game you built. Then you can't do it this way, because data is stored locally on all devices. For this we need to have all the users data stored on one/same place.
The Solution (like~in PHP)
We will save our data on an online server and will retrieve it whenever required. We can connect to any database engine on the server but MySQL is fast as compared to others. So, we will use MySQL as our database server and connect to it using a PHP web service.
That web service will do all (mostly CREATE and READ) database operations for you. This way you can save the data in the server and retrieve it globally whenever you want.
But, how it is done? Here are the steps:
Create a web service
A collection of PHP scripts which can read and write, to and from database
For security, plug in something like OAuth to perform transactions and encrypt data being transferred (Best will be to write this type of service in a framework)
Send READ or WRITE request from Android activity to web service
Web service receives, authorize, parse the request
Web service then sends the appropriate request to MySQL server
MySQL server receive, parse the query and send the results back to web service
Web service receive, parse the response and send the results back to Android activity
Android Activity receives the data and then you can play with it :)
You might have private databases in Android itself without using JSON etc. Just like you used in PHP with MySQL.
You might find lots of tutorials about SQLLite. But, here is official documentation of Google's Android page: http://developer.android.com/reference/android/database/sqlite/package-summary.html
The short answer is you can't use JDBC in android. The reason is JDBC is too heavy for mobile.
But you can use built in SqLite support to work with your local sqlite database. It has some limitations comparing to JDBC MySql driver but it should fit your needs.
Usage of SqLite is simple:
Connect to database
Send query
Get your data
Process data
The only thing you have to remember is that you should not use SqLite in main thread. You may create your own with Thread but the simplest solution is to use AsyncThread.
If you want to make calls to remote MySql database then your way is a bit more complicated
Write server side code that will handle requests from android and send it to database(e.g. using JSON)
Send request to remote server from your android device
Receive server's answer
Process it
As in above method with SqLite you should do it in a background thread to not to block UI.
-If needed you can use a local database (using SQLite)
-Server/client side in android are similar to any other application: you need a WebService handler which is usually (best practice) an AsyncTask.
Solution: nice and easy...
Create a .php that does the job for you, parse the result as a json workflow. Use your asynctask to get and parse the result. Add data to your database if needed. You can finally display a nice UI in the onPostExecute method.

Communicate with MySQL database using ASP.net

I am building flash web based application using as3 and I want to receive and send data to MySQL server. I found out that I cannot communicate directly with MySQL from as3 and I have to use a PHP script. So I am trying to find a way to communicate with database with asp.net solution instead of PHP. thanks

Connecting MySql with Android without using PHP

I want to connect a MySql DB with my android application.
However, I DON'T want to/CAN'T use PHP for doing this.
Almost all solution for MySql connection with android on internet uses PHP.
I read somewhere that, If one don't want to use PHP then web service should be used.
But I'm not able to find any tutorial/sample example for the same.
Any help appreciated.
It seems you're mixing up some things.
A web service is simply some code on the internet (web) which allows you to receive and send information to a server, where it is saved per example in a database.
PHP is just a language, in which you can write a web service.
You can use a vast array of languages to create a web service ( read: expose your database) to other devices. Among others, you can easily do this in Java, .NET, Python ...
If you're looking for a way to connect to an external database without any web service / API in between, i'll have to disappoint you with the news that this is not supported by Android.
Most examples of a simple web service / a bunch of scripts contain PHP since this is probably the easiest and can be used on pretty much any server.
A webservice, is as it's called, a service, meaning that you have one side consuming it (the android client). if all you want is a persistent storage, you could use SQLite which is an SQL compliant solution which exists within android.
If it's possible to SSH to a server via Android, you could use that to connect to mysql, because the only other solution involves having mysql binaries installed locally on your android machine, and that's not possible AS FAR AS I KNOW, on Android.
One major reason for using a webservice (e.g. written in PHP) to connect to a remote DB is that you don't want to store the database login credentials inside your app. Because otherwise it'll be easy to extract your login for that database and access and edit it in a way you might not have planned (eg. delete stuff).
Its Possible to connect mysql database .
I have done with out using php file . I have used an spring configuration file to establish an connection to the database and dao to access the data from the database.
Create an Web Application that access the Server through the Spring Framework and an Servlet .
Create an Android Client Application tat make an get / post request to the Servlet , process the results in the servlet and return the response to the Android Client Application (json format ) Process the json format reponse in the Android Client Side and use it to your application

Categories