Solved

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

Posted on 2011-03-19
8
1,003 Views
Last Modified: 2013-11-25
what code i have to use to connect ios programm to a data base server?
0
Comment
Question by:a_sane3
8 Comments
 
LVL 5

Expert Comment

by:mad_mac
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.
0
 
LVL 13

Accepted Solution

by:
SpeedyApocalypse earned 500 total points
ID: 35212227
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
 
LVL 3

Expert Comment

by:gawhemant
ID: 35212941
0
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 

Author Comment

by:a_sane3
ID: 35516108
Thanx allot
ahmad
0
 
LVL 13

Expert Comment

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

Expert Comment

by:SpeedyApocalypse
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

Thanks,

SpeedyApocalypse
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
InTunes and IOS issue 5 172
My iPhone 5S (IOS 10.1) imo not working 8 561
Android development question 2 35
tableview is not updating 1 7
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
There is a security feature on iOS devices that is nearly impenetrable when it has been activated.  This article will provide some possible solutions as well as necessary steps to take to ensure you do not end up with a locked device.
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

786 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