【发布时间】:2012-02-08 20:59:25
【问题描述】:
我在尝试使用目标 C 中的 NSURLRequest 类传递和 SQL 查询时遇到了一个非常奇怪的问题。我能够发送一个简单的查询,该查询可以正常工作并返回正确的内容(JSON 格式)。 这是我的代码:
NSString *URLWithSQLQuery = [NSString stringWithString:@"http://localhost/querydatabase.php?query=INSERT+INTO+Table+(ID,Column1,Column2,Column3,Column4,Column5,Column6,Column7,Column8)+VALUES+(NULL,'a','b','','c','d','','1','2')"];
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:URLWithSQLQuery]];
NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSString *data = [[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding];
NSLog(@"%@", data);
这不会将虚拟值插入表中。 以下代码返回正确的内容:
NSString *URLWithSQLQuery = [NSString stringWithString:@"http://localhost/querydatabase.php?query=SELECT+*+FROM+Table"];
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:URLWithSQLQuery]];
NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSString *data = [[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding];
NSLog(@"%@", data);
当我将 url http://localhost/querydatabase.php?query=INSERT+INTO+Table+(ID,Column1,Column2,Column3,Column4,Column5,Column6,Column7,Column8)+VALUES+(NULL,'a','b','','c','d','','1','2') 粘贴到浏览器中时,查询会正确执行。所以我真的不知道问题出在哪里,因为它不是来自 PHP 脚本或 URLWithSQLQuery,也不是来自我的 Objective C 代码。
任何想法将不胜感激。感谢大家的帮助。
斯科特
【问题讨论】:
-
没关系,我的 URL 字符串是一个带格式的字符串,它使用了其他包含空格的字符串,我需要用“+”替换那些空格。无论如何,谢谢。
-
我更改了它,现在使用 POST 方法而不是 URL
标签: php objective-c sql get nsurlrequest