Getting data from remote sql table on iphone - iphone

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.

Related

Centra DB to IPhone App / Objective-C

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.

How to get data into Core-Data from an SQL Database?

I'm about to start an iOS project that requires pulling user's data from an SQL Database and viewing it within the App. Before I begin I'm looking for conformation that I'm taking the right (best) route.
My Plan:
App starts on login page (app will display data from another service)
App uses AFNetworking to post request to web service
Web service gets user data from SQL Database and sends back JSON
App uses JSONKit to parse the feed and load into Core-Data
App uses info from core-data to populate UI
Does this seem like an appropriate way to get the info into Core-Data from SQL? Any suggestions for doing things differently?
Thanks.
Are you receiving the response from the web server in JSON? If so, the fact that the server is using an SQL database is immaterial. What you need to know is how to parse JSON for inclusion in a core data store. Cocoa is my Girlfriend has a pretty good tutorial up.
Part 1
Part 2
To answer your comment, here's what I've done.
Display a login screen. The login credentials should be stored in the keychain for security. I've used SSKeychain for this.
To handle sending and receiving data from a web request your best option is to use a pre-built library. I've always used ASIHTTPRequest, but since it is no longer under active development, you should probably look around a bit before you commit to anything. I'm sure there are nicer and cleaner libraries out there.
You need to parse the JSON responses. I'm a fan of JSONKit. It's very fast, very easy to use, very robust.
Pulling data out of the core data store and displaying it in the interface will be no problem for you. If you create a new project in Xcode most of the setup will be done for you.
Now, there are a lot of projects out there that attempt to combine web requests, json parsing and core data loading into one framework. I've tried to use a few of these and haven't had much luck. The ones I've tried haven't been robust and very difficult to debug. Setting up your own request/parse/load code is not difficult at all, just a bit time consuming.
I am sure that there are a lot os ways to make implement this problem. Your solution is one of the popular solutions I guess but you could connect to the DB via a socket and talk with the database directly e.g. Going over a port 80 web site has the advantage that the possibility of some kind of firewall blocking the communication is very low. I would solve this kind of problem the same way I guess.

What is the best way to connect an iphone app to a mysql database?

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

iOS webservice xml help

i am trying to create an app that connects to a mysql database, downloads the records in the table for the user then displays them in a UITableview which can be drilled down with the data that as downloaded from the mysql database. I would also like the data to be stored locally in something like sqlite. The data would then be modified then uploaded back to the mysql database.
My questions is where do i start? I have read alot of posts on google and they say that its best to connect to a xml which acts as a middle man between the database and the app. How can i create this xml file? is it something that is generated every time the app is launched or something done daily on the server?
If anyone can help me out. i know its a very broad question so if someone can point me in the right direction im not asking someone to right this app just a hand as im lost.
Thanks,
Aaron
I know others have mentioned links to libraries that let you connect to MySQL from iOS. I've not tried them myself but don't rule that out.
To answer your quest, you do not "create" the XML files. Web services are developed in a language, and written as applications. You would in essence need to write another application that runs on a server. That application would connect to your MySQL database. That application would also publish methods for getting at, and updating, data. By virtue of making your server a SOAP web service (in whatever platform), the data will be sent over the wire as XML.
If I have understood you want a kind of ORM?
If so you can check for Restkit and more specifically on the side of the object mapping system. It allows us to synchronize remote object/data locally with the coredata.
I have never used it, but I have seen a great tuto which talking about that here: Advanced RestKit Development (However I think it works only with json messages).
I hope it'll help you in your reflection.
I would recommend looking at XML Parser, Webservice, and Core Data tutorials.
SOAP and XML Response Parsing Samples for iPhone/iPad?
http://www.techotopia.com/index.php/An_iOS_4_iPhone_Core_Data_Tutorial_%28Xcode_4%29
That would be a good start, and I could give you some more material if you would like. As far as setting up your actual web service, I've never done that so I couldn't help you there.

Retrieving data from a remote database

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.