2012-04-23 60 views
0

我在sqlite中存储id,lat,lng和timestamp,现在需要将它发送到服务器的JSON对象中。我如何将我的sqlite数据库中的数据发送到HTTP和JSON服务器

这里是我插入数据的地方。

NSString *insertStatement = [NSString stringWithFormat:@"INSERT INTO location (latitude, longitude, timeStamp) VALUES (%g, %g, %@)",newLocation.coordinate.latitude, newLocation.coordinate.longitude, newLocation.timestamp];  
char *error; 
if (sqlite3_exec(databasehandle, [insertStatement UTF8String], NULL, NULL, &error) == SQLITE_OK) 
{ 
    NSLog(@"Location inserted. %@", newLocation.timestamp); 
}  

else NSLog(@"Error: %s", error); 

这里是我的工作,现在的代码。我不知道在哪里何去何从:

NSString *queryStatement = [NSString stringWithFormat:@"SELECT ID, latitude, longitude, timeStamp FROM LOCATION"]; 

// Prepare the query for execution 
sqlite3_stmt *statement; 


if (sqlite3_prepare_v2(databasehandle, [queryStatement UTF8String], -1, &statement, NULL) == SQLITE_OK) 
{ 
    // Iterate over all returned rows 
    while (sqlite3_step(statement) == SQLITE_ROW) { 

这里是我想用来发送数据

responseData = [NSMutableData data]; 

//whatever your server address is 
NSURL *url = [NSURL URLWithString:@"http://www.resturl.com/whatever"]; 

NSError *jsonError; 
//NSJSONSerialization is Apple's new json serialization class so we can use it to convert to and from json and foundation objects 
NSData *requestdata = [NSJSONSerialization dataWithJSONObject:returnArray options:0 error:&jsonError]; 

NSMutableURLRequest *request; 
request = [NSMutableURLRequest requestWithURL:url]; 
[request setHTTPMethod:@"POST"]; 
[request setValue:[NSString stringWithFormat:@"%d", [requestdata length]] forHTTPHeaderField:@"Content-Length"]; 
[request setValue:@"application/json" forHTTPHeaderField:@"Accept"]; 
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"]; 
[request setHTTPBody:requestdata]; 

//this kicks off the request asynchronously 
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self]; 

任何帮助,将不胜感激我的JSON/HTTP代码!

+0

考虑周围sqlite3的使用Objective-C的包装一样FMDB - https://github.com/ccgus/fmdb - 这可能让你试图做 – 2012-04-23 22:55:43

+0

是啊,我觉得有些东西更容易关于使用它,但我已经用sqlite直接做了很多事情,并且不想重写。 – michael03m 2012-04-24 19:17:42

+0

任何人都可以添加任何输入? – michael03m 2012-04-24 19:18:34

回答

1

我使用相同的代码,但不使用webservice在数据库中发布数据。

NSURL *url = [NSURL URLWithString:@"someurl/try_ws_iphone/insertRecords.php"]; 
NSArray *res=[[NSArray alloc] initWithObjects:txtname.text, nil]; 
NSError *jsonError; 
//NSJSONSerialization is Apple's new json serialization class so we can use it to convert to and from json and foundation objects 
NSData *requestdata = [NSJSONSerialization dataWithJSONObject:res options:0 error:&jsonError]; 


NSMutableURLRequest *request; 
request = [NSMutableURLRequest requestWithURL:url]; 
[request setHTTPMethod:@"POST"]; 
[request setValue:[NSString stringWithFormat:@"%d", [requestdata length]] forHTTPHeaderField:@"Content-Length"]; 
[request setValue:@"application/json" forHTTPHeaderField:@"Accept"]; 
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"]; 
[request setHTTPBody:requestdata]; 

//this kicks off the request asynchronously 
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self]; 
+0

什么是共同发生的家伙..我也是Mehul Mistry ...:D – 2013-04-19 04:46:32

相关问题