2012-09-03 103 views
0

我想在我的数据库中存储4个值。经度,纬度电话号码和时间戳。插入数据到mysql - ios

我明白了人想出如何将它们与本地的存储的NSLog,

但我想将它发送到我的MySQL数据库。

有人有一些教程或想帮助我吗?

我需要这个不好,

您的帮助,将不胜感激。

电贺

UPDATE

下面是一些代码,我需要在我的数据库

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 
} 

存储,我需要从一个文本框,经度,纬度&时间戳存储的电话号码。

迎接(再次)

+1

http://whathaveyoutried.com? – Luke

+0

我试图做SQLite,但直到现在,这是行不通的。 –

+0

如果你发布代码,那么人们更有可能协助修复它并帮助你。 ;) – Luke

回答

1

对您有帮助吗?

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

嗯,我需要PHP代码和IO代码来实现它在我的应用程序 –

1

好吧,我不知道你想要什么,但正如我在GENRAL理解,如果你要更新的MySQL服务器上的本地数据,那么你刚刚做简单的事情

添加ASIHTTPrequest或AF和sbjeson框架并将你的数据编码到json中,并用post或get方法发送到php页面,其中php可以将字典解码为关联数组,然后循环并更新到mysql。

如果你只是想存储数据,那么你应该使用Codedata或sqlite。

2

使用下面的代码

//Create String 
    NSString *var1 [email protected]"waitTime="; 
    NSString *var2 [email protected]"&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]; 

你可以得到状态码explained in this link

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

状态代码被定义at this link

在POST命令之后,这表示成功,但响应行的文本部分指示应该知道新创建文档的URI。

如果连接超时,则应在连接:didFailWithError:delegate方法被调用时看到此情况。

使用PHP tutorial通过PHP将POST数据(从iOS接收)发送到MYSQL数据库。