Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1008
  • Last Modified:

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

what code i have to use to connect ios programm to a data base server?
0
a_sane3
Asked:
a_sane3
1 Solution
 
mad_macCommented:
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.
0
 
SpeedyApocalypseCommented:
Hello,

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:

<?php

include("database.php");

$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:

http://www.example.com/?v=tom

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

	NSData *data;
    NSURLResponse *response;
    NSError *error;
	NSString *urlString = [NSString stringWithFormat:@"http://www.example.com/?v=tom"];
	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?
0
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!

 
a_sane3Author Commented:
Thanx allot
ahmad
0
 
SpeedyApocalypseCommented:
The original asker has said "thanks" but has not assigned any points.  Clearly he has received his answer.
0
 
SpeedyApocalypseCommented:
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

Thanks,

SpeedyApocalypse
0

Featured Post

Industry Leaders: 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!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now