How do I send the data I have in my sqlite database to the server with HTTP and JSON

二次信任 提交于 2020-02-02 15:59:38

问题


I'm storing id,lat,lng, and timestamp in sqlite and now need to send it in an JSON object to the server.

Here's where I'm inserting the data.

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);

And here is the code that I'm working on right now. I'm not sure where to go from here:

 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) {

Here is my JSON/HTTP code that I want to use to send the data

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

Any help would be appreciated!


回答1:


i am using same code but not post data in database using 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];


来源:https://stackoverflow.com/questions/10288489/how-do-i-send-the-data-i-have-in-my-sqlite-database-to-the-server-with-http-and

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!