My App needs to connect to and grab data from a MySQL database sitting on a server. I've successfully interfaced with local SQLITE databases in my Apps, but this is the first time I've had to interface with a remote MySQL database.
I understand this process involves:
1) the iPhone App sending this request to the server
2) some sort of API gets executed and it does the actual "talking" to the Database on the server, and then it returns the query results as XML (or JSON.)
3) the iPhone then parses the returned information and uses it however it needs to.
What I need help with is step # 2 - the API. I don't know PHP, I don't know PERL - do I really have to learn one of these just to write an API? I thought that this iphone-to-remote-MySQL task would be so commonplace that such API's would already be out there, available freely - but I'm not really seeing anything.
I know Objective-C really well - but should I be using XML or JSON for this? Should I be learning PERL or PHP?
any tips or advice would be highly appreciated.
It's hard to give you a definite answer without knowing more precisely what you are looking at, but it sounds like your web service/API part should be quite light. In this case, it should be quite easy to write it in any language. If you know Objective-C well, it should be very easy to pick up some PHP or Ruby.
You mention PHP and Perl. The good thing I see with PHP is that it's available on any hosting service. For instance, with PHP you can make MySQL queries in a few lines of code.
Otherwise, if the web service lives on a machine that you own or if you have more flexibility, it might be easier to write something using Ruby and a small framework like Sinatra.
Regarding using XML or JSON to serialize the data, this will depend on what type of data your API returns. Generally, JSON will be much simpler and the support is good on both side:
- With Ruby, you can see how easy it is to use JSON here: http://flori.github.com/json/
- With Objective-C, you would use NSJSONSerialization available starting with iOS5.
Well you can talk to remote MySQL server using Objective C client directly from iphone
as show here
Related
I'm trying to find some reading materials on how to connect an IPHONE app to a central DB-Server such as SQL-Server.
I know about SQLite, and the other methods of storing Data in the device itself. What I'm really interested is for the application to interact with a central database server. Are there any sort of objects such as SQLClient in .net or do I need to go through sending off HTTP-Requests and deserializing json/xml?
i dont know the answer to your question, but even if you find that it would be not recommended to do so, since its always better to use an httprequest solution,
Encapsulating the database implementation is always the way to go, since database implementation could change rather quickly adding a layer over it is recommended
That goes without saying that you will not be able to do caching by accessing the database directly, which is also a drawback
am sorry this is not the answer you are looking for, but you should re think on how you are going to implement your solution,
Preform a httprequest to a php script or similar and parse JSON or XML.
You would have to make a REST interface to bridge the 2 together, and as you said deserialize responses depending on the format, if it's json or xml.
From Device side, make GET/POST requests to your script, which responds the query results back to the app and handle them accordingly.
I want the way with the fastest execution time. I'm not feeling comfortable of using web service because i need to create separate php pages and retrieve data as xml. If you think its good to use web service please tell me why. I want to code my database queries right on my c/objective c pages.
I've been searching for libraries. I saw this sequel pro - won't i have any problems on using this - like licensing issues? I also saw this libmysqlclient of cocoa but some say its not working well. I've also read about a library developed by Karl Kraft found here http://www.karlkraft.com/index.php/2010/06/02/mysql-and-objective-c/ but don't know if i could trust this.
I would really appreciate you help.
Definitely build a web service to act as an abstraction layer to your database. Here are some significant reasons in my opinion:
Since you want speed, you will be able to add caching when using the webservice, so you will essentially eliminate the need for identical queries to run (sometimes).
If you need to change your data model later, you just have to modify the webservice backend and don't have to update your app.
You can better control security by not exposing the database to the world, and keep it safe behind the web service.
Your database credentials should not be stored in an app. What if you needed to change those?
I strongly suggest a web service. Hope this helps.
Connect to your DB by PHP and output the result as JSON
is much better and faster then xml and less coding if use JSON Framework.
and never never try to connect to your DB from your iphone because it easy to sniff out the request from iphone.
Being safe then Sorry, keep that in mind
I am a beginner at both iPhone programming and SQL, yet I have basic knowledge of them.
I am planning to do an application that would plot a graph from data taken from a database of the server of my company. I know how to plot, I know how to extract data from an sql table, but what I don't know is how to access the server.
So do I have to go through some kind of oracle-like application ?
This may seem like a very stupid question because it might not even be possible but any answer will be appreciated.
Thanks !
Go through a web service for example a PHP page that returns JSON (or XML, but JSON is easier to parse).
I can highly recommend this tutorial
Once you have your web service, you can use NSURLRequest/NSURLConnection to download the data and use a JSON framework to parse it. Or, if you're using XML you can use NSXMLParser.
See this apple code for more info on downloading using NSURLConnection.
The best way for this will be, using APIs at server end that handle the client request and perform database interaction, so transfer of data among device and server, take place through XMLs that will be secure as well as fast.
It is definitely possible to contact a server (I can't imagine what would happen without that!). What you are looking for is NSURLConnection. Have a look at the example provided by Apple.
could someone please explain to me the process of retrieving data from a remote database? I understand how to parse data from sqlite to an iPhone application however remote databases seem to be more complex. I tried to do research however i get confused about web services,clouds and other words that im not familiar with. So far i gathered that i need to use JSON to convert the data from the database into the appropriate format that is displayed on a php page? then the iPhone application connects to that page and retrieves the data?
Would someone be able to give me a brief explanation (also what database can be used with JSON? in this instance)? would me much appreciated (also if someone has any tutorials/articles about this would be grateful). Sorry if im totally wrong, trying to learn...
That are many ways to build a system where you have a client application that retrieves data from a "remote" system that runs a database. You have many many choices as to what the server runs, and what the client is... and there is a world of technologies involved in this...
Since you are interested in learning, I would suggest you to take into consideration a very common architecture for such systems and dig into it.
One such example is LAMP (Linux, Apache, MySQL, PHP) and you can find a good tutorial at this site.
The tutorial covers everything you need, from setting up the systems, to retrieving the data and passing it back though the web; it's step by step and pretty easy to follow. The only thing that is not covered is JSON, but JSON in the end is just a format to pass data back and forth, and you can easily learn the basics about it with this tutorial.
Finally, I would suggest looking at how building an iPhone app getting JSON data. This would be the point where you join the two previous tutorials.
If you have a web service protocol on the server side, you can use JSON based web service to retrieve your data. If you follow any JSON tutorial, you can see JSON is just a text representation of the data.
Or you can simply use a post request to your server, which can response by simple XML with your own defined tag. So that you can use NSXMLParser to parse the data in your iPhone and decode information from there.
Any kind of database in the remote site will work. I prefer the free open source MySQL database.
With JSON web servie, you need some JSON converter for your database in the server side and also in the client side. A good open source client tool is SBJSONParser.
But if you use XML, you can define your own tag to encode and decode you database fields and information.
I'm in the middle of working on my first native application with networking and I have a question regarding the best way for interacting with remote storage. In a perfect world I'd like to do the following.
Prompt the user for login information from the iPhone.
Verify the users credentials and connect to a MYSQL database hosted by myself.
Parse MYSQL data into a table view.
Allow the user to add or update information in the database.
I've read some similar questions posted, maybe something's lost in translation, but the two most common means I've come across are.
Create a web service for handling these requests using SOAP/REST/JSON (no experience doing this, but would like to learn if it's a better implementation)
Write PHP scripts (enough experience to get by) that will grab data username/password/requests securely from my NSURLRequest and echo the NSData as XML and parse it with an NSXMLParser.
Are there other options? Is one a better implementation over the other? (web services come up more in searches)
Thank you in advance for taking the time to read my question and possibly clearing up any confusion.
Whoa! Web Services! Oh wait, calling a PHP script that returns JSON is actually also a web service. Well, that makes things simpler :-)
Yeah, so I would go for this:
Write a PHP script that returns JSON data (many tutorials available)
Protect the PHP script by setting up 'Basic access authentication' (Apache documentation)
Tell Apache to take the user database from your mysql database
Run your service on secure (HTTPS) web server (important because basic auth is not secure)
This way you can use almost all standard components on the iPhone side. NSURLConnection will talk HTTP(S) and there are excellent open source JSON parsers for Objective-C.