【问题标题】:Insert data into mysql - ios将数据插入mysql - ios
【发布时间】:2012-09-03 09:33:19
【问题描述】:

我想在我的数据库中存储 4 个值。 经度、纬度电话号码和时间戳。

我知道如何使用 NSLOG 将它们存储在本地,

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

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

我非常需要这个,

您的帮助将不胜感激。

问候

更新

这是我需要存储在数据库中的一些代码

    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
}

我需要存储文本字段中的电话号码、经度、纬度和时间戳。

问候(再次)

【问题讨论】:

  • 我尝试过使用 SQLite,但直到现在这不起作用..
  • 如果您发布代码,那么人们更有可能协助修复它并帮助您。 ;)

标签: php mysql ios http-post http-get


【解决方案1】:

使用以下代码

  //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];

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

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

状态代码定义为at this link

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

如果连接超时,你应该在调用 connection:didFailWithError: 委托方法时看到这个。

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

【讨论】:

    【解决方案2】:

    这有用吗?

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

    【讨论】:

    • 嗯,我需要 PHP 代码和 iOs 代码在我的应用程序中实现它
    【解决方案3】:

    好吧,我不确定你想要什么,但正如我一般理解的那样,如果你想在 mysql 服务器上更新你的本地数据,那么你只需做简单的事情

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-09-19
      • 2023-04-10
      • 2013-05-29
      • 1970-01-01
      • 1970-01-01
      • 2017-12-30
      • 2014-09-12
      相关资源
      最近更新 更多