David Raijmakers
David Raijmakers

Reputation: 1379

Insert data into mysql - ios

I would like to store 4 values in my database. longitude,latitude phone number and timestamp.

I got it al figured out how to store them locally with the NSLOG,

But i want to send it to my MySQL database.

Does somebody have some tutorials or want to help me out here?

I need this badly,

Your help would be appreciated.

Greets

UPDATE

Here's some code i need to store in my database

    resultsLabel.text = [NSString stringWithFormat:@"(%@) %@ Location %.06f %.06f %@", ([UIApplication sharedApplication].applicationState == UIApplicationStateBackground) ? @"bg" : @"fg", resultsLabel.tag == 0 ? @"gps:" : @"sig" , newLocation.coordinate.latitude, newLocation.coordinate.longitude,  [formatter stringFromDate:newLocation.timestamp]];

LocationTestAppDelegate * appDelegate = (LocationTestAppDelegate *)[UIApplication sharedApplication].delegate;
[appDelegate log:resultsLabel.text];

NSString* encodedValue = [newLocation.coordinate.latitude
                          stringByAddingPercentEscapesUsingEncoding:
                          NSASCIIStringEncoding];

//construct an URL for your script, containing the encoded text for parameter value
NSURL* url = [NSURL urlWithString:
              [NSString stringWithFormat:
               @"http://yourdomain.com/locatie.php?id=%@",
               encodedValue]];

NSError* error = nil;
//send the request synchronously, store the response in result
NSString* result = [NSString stringWithContentsOfURL:url
                                            encoding:NSUTF8StringEncoding
                                               error:&error];

if (!error && ([result isEqualToString:@"OK"])) {
    // everything went well
}

And i need to store the phone number from a textfield, longitude, latitude & timestamp.

Greets(again)

Upvotes: 0

Views: 1805

Answers (3)

Vimal Venugopalan
Vimal Venugopalan

Reputation: 4091

Use the following code

  //Create String
    NSString *var1 =@"waitTime=";
    NSString *var2 =@"&cover=";
    NSString *wait = [var1 stringByAppendingString:waitTime.text];
    NSString *co = [var2 stringByAppendingString:cover.text];


    NSMutableURLRequest *request = 
    [[NSMutableURLRequest alloc] initWithURL:
     [NSURL URLWithString:@"http://mysite.net/index.php"]];

    [request setHTTPMethod:@"POST"];

    NSString *postString = [wait stringByAppendingString:co];

    [request setValue:[NSString 
                       stringWithFormat:@"%d", [postString length]] 
   forHTTPHeaderField:@"Content-length"];

    [request setHTTPBody:[postString 
                          dataUsingEncoding:NSUTF8StringEncoding]];

    [[NSURLConnection alloc] initWithRequest:request delegate:self];

You can get the status code as explained in this link

- (void) connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{
   NSHTTPURLResponse* httpResponse = (NSHTTPURLResponse*)response;
   int code = [httpResponse statusCode];
}

Status codes are defined at this link.

Following a POST command, this indicates success, but the textual part of the response line indicates the URI by which the newly created document should be known.

If the connection times out, you should see this when the connection:didFailWithError: delegate method is called.

Use the PHP tutorial to send the POST data (received from iOS) to MYSQL Database via PHP.

Upvotes: 2

aashish.acs
aashish.acs

Reputation: 23

Well i am not sure what you want but as i understand in genral if you want to update your local data on the mysql server then u just do simple thing

add ASIHTTPrequest or AF and sbjeson framework and encode ur data into json and send with post or get method to php page where php can decode the dictionary as a associative array and just loop through and update into mysql.

and if u just want to store data then you should use Codedata or sqlite.

Upvotes: 1

Louis
Louis

Reputation: 2900

Is this helpful ?

INSERT INTO table_name (column1, column2, column3,...)
 VALUES (value1, value2, value3,...)

Upvotes: 1

Related Questions