Connecting an Android application to a database - php

I need to connect my mobile app to a database so it can send information from the app to the database.
I have looked at a ton of tutorials but they seem to irrelevant to my situation or write the entire application in XML. Can I connect it through script tags in the app or do I need to connect to via PHP and if I do where do I place that file in the application?

It is not clear to me if it is a local database on your phone or not.
If it is a local database on your phone I would use android's api.
If it is not, the best option is to write a SOAP or Rest service which need to run on a server. Personally I would go for a REST service, because it is less complicated. Although if there is sensitive data it might be better to use a SOAP service. You might take a look at this.
For calling your Rest services you could use a httpwebrequest. I belief this is an good example.

Write ur database related code in php and from your app send the post or get parametized url so it can be stored to ur phpmyadmin db

Related

Share database between app and web

I'm involved into an app that have to retrieve queries from an existing database. This db have to be implemented in php in a remote server, and will be the place where the admin will have to insert content into the shared db, but I don't know how to implement that correctly or what is the best solution.
I'm thinking about make a sqlite database with php and connect directly the app to the db, but I haven't found yet the best way to do that.
What do you think? Thank guys.
FYI, without any server communication (Web services) you can't use the website database in android application.
If you want to manage the communication between the website and the android app means you need a centralized server (probably my sql dab) which having the data of your web/mobile applications. So, after that by using the web service methods (REST/SOAP) you can achieve the communication between them
I'm thinking about make a sqlite database with php and connect
directly the app to the db, but I haven't found yet the best way to do
that.
This is not the right approach to connect mobile applications with the database. Instead you should create a middleware(REST/SOAP webservice) which runs on server. This middleware will in turn connect with the database and your mobile app whether it is android, iOS or windows app can communicate with this middleware.

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.

Connect webhost database to android database

I developed a web application based on PHP, now I built an android application too.
I know I can use SQLite for storing information, but I want to connect android database to my webhost database so that the information can be accessible by two applications.
Is it possible? If yes, how?
There is no direct way to access database outside device, you need to write some web services which will return data in json (better option) or xml format and then you can parse it and use information.
You could use PHP script in order to accomplish that.
http://www.basic4ppc.com/android/forum/threads/connect-android-to-mysql-database-tutorial.8339/
Although Full implementation requires lot of code and instead of reinventing the wheel its better u checkout smart implementation given in the links below which will help u out.
The following Tutorial #01 and Tutorial #02 might help you out.
1- Connect Android to Remote Database
2- Using Json (API) to parse data from and into android to server
3- Parse Request and Process
4- Save Information in Local Android Sqllite database.

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

Silverlight with PHP and MySQL: can I send data from the Silverlight app back to the server?

I'm creating a website using PHP and MySQL, and I'm trying to add some Silverlight stuff to the site. I need the users to be able to do stuff in the Silverlight app, and for that data to be saved in the MySQL database. I'm wondering about how I could do this...
Can I get the client side Silverlight app to log in to the server side MySQL database? Would this be a security risk? And if it's possible, how would I do it?
Or is there a better way anyone can think of? And any ideas how I'd implement it?
Thanks everyone! :)
No you should not have the app interact with your MySQL database directly. In this type of situation you will need to implement some form of web service, which exposes your data to the client. The Silverlight client code will then send requests to the web service, which runs on the server and has access to the database. Using a Microsoft technology on the server will make this a lot easier, because the IDE will do a lot of the wiring up for you, but I'm sure it could be done with PHP as well.
I would look into how to create a PHP, REST or SOAP based webservice and go from there.
Here is a video explaining how to consume the service, once you have that sorted out on the server.
http://silverlight.net/learn/videos/silverlight-videos/http-communication/

Categories