Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17


what code i have to use to connect ios programm to a data base server?

Posted on 2011-03-19
Medium Priority
Last Modified: 2013-11-25
what code i have to use to connect ios programm to a data base server?
Question by:a_sane3
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions

Expert Comment

ID: 35180483
hi, you will need to be a bit more specific about what you are trying to achieve.  are you looking to run an application on a private network connecting to a DB host, does the app want to work over the internet...?

normall you would place some form of REST based web service in front of the database and connect via those published services.  well that's the model i have followed in my applications.
LVL 13

Accepted Solution

SpeedyApocalypse earned 2000 total points
ID: 35212227

Could you please specify your type of database server that you have?

Generally, the method of connection to an online program through an iOS Application would be using NSURLConnection.  This allows you to send POST and GET data toward a certain URL and receive information back through NSData.  For example, suppose you have a script that receives a variable and does something.  For example:



$myvar = $_GET['v'];
$query = mysql_query("SELECT * FROM table WHERE variable =  '$myvar'");
$array = mysql_fetch_array($query);
echo json_encode($array);


Open in new window

That script simply returns a JSON array of your data... you could call it using a get parameter like so:

On the iPhone Side, your NSURLConnection may look like this:

	NSData *data;
    NSURLResponse *response;
    NSError *error;
	NSString *urlString = [NSString stringWithFormat:@""];
	urlString = [urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    NSURL *url = [NSURL URLWithString:urlString];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];
    data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
    [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
	NSString *resultString = [[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding] autorelease];
    NSLog(@"The returned data: %@",resultString);

Open in new window

So, the returned data would be whatever was echo'd or displayed on your server.

Does this help answer your question?

Expert Comment

ID: 35212941
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


Author Comment

ID: 35516108
Thanx allot
LVL 13

Expert Comment

ID: 35517481
The original asker has said "thanks" but has not assigned any points.  Clearly he has received his answer.
LVL 13

Expert Comment

ID: 35518131
3) Accept one or more Expert posts as the answer

I have explained the act of connecting to a database, on the site - instead of a simple link. POST ID: 35212227



Featured Post

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this article I'm going to cover the basics and usages of NSInvocation. What is NSInvocation? Apple Developer Reference describes NSInvocation this way: An NSInvocation is an Objective-C message rendered static, that is, it is an action tur…
The Basics of Storyboard Storyboards were first introduced to developers in the release of iOS 5. They save the developer the trouble of designing each screen’s interface in a different file. The storyboard allows you to see the conceptual mockup o…
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at

722 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question