1

这是关于我的另一个问题:Saving Latitude and Longitude values to a MySQL Database iOS

你能告诉我这段代码是否将我的纬度和经度值保存为字符串或数字。如果它保存为字符串,你能告诉我如何将它们保存为数字吗?

我还为它们设置了 mysql 字段作为浮点类型。

PHP

<?php

$username = "username";
$database = "database";
$password = "password";

mysql_connect(localhost, $username, $password);

@mysql_select_db($database) or die("unable to find database");

$name = $_GET["name"];
$address = $_GET["address"];
$city = $_GET["city"];
$county = $_GET["county"];
$country = $_GET["country"];
$postcode = $_GET["postcode"];
$date = $_GET["date"];
$time = $_GET["time"];
$latitude = $_GET["latitude"];
$longitude = $_GET["longitude"];

$query = "INSERT INTO map VALUES ('', '$name', '$address', '$city', '$county', '$country', '$postcode', '$date', '$time', '$latitude', '$longitude')";

mysql_query($query) or die(mysql_error("error"));
mysql_close();

?>

AddSessionViewController.h

#import <UIKit/UIKit.h>

#define kpostURL @"http://myurl.com/map.php"
#define kname @"name"
#define kaddress @"address"
#define kcity @"city"
#define kcounty @"county"
#define kcountry @"country"
#define kpostcode @"postcode"
#define kdate @"date"
#define ktime @"time"
#define klatitude @"latitude"
#define klongitude @"longitude"

@interface addSessionViewController : UIViewController {

IBOutlet UITextField *nameText;
IBOutlet UITextField *addressText;
IBOutlet UITextField *cityText;
IBOutlet UITextField *countyText;
IBOutlet UITextField *countryText;
IBOutlet UITextField *postcodeText;
IBOutlet UITextField *dateText;
IBOutlet UITextField *timeText;
IBOutlet UITextField *latitudeText;
IBOutlet UITextField *longitudeText;
NSURLConnection *postConnection;

}

-(IBAction)post:(id)sender;
-(IBAction)getLatLon:(id)sender;


-(void) postMessage:(NSString*) postcode withName:(NSString *) name withAddress:(NSString *) address withCity:(NSString *) city withCounty:(NSString *) county withCountry:(NSString *) country  withDate:(NSString *) date withTime:(NSString *) time withLatitude:(NSString *) latitude withLongitude:(NSString *) longitude;

添加SessionViewController.m

-(void) postMessage:(NSString*) postcode withName:(NSString *) name withAddress:(NSString *) address withCity:(NSString *) city withCounty:(NSString *) county withCountry:(NSString *) country withDate:(NSString *) date withTime:(NSString *) time withLatitude:(NSString *)latitude withLongitude:(NSString *)longitude; {

if (name != nil && address != nil && city != nil && county != nil && country != nil && postcode !=nil && date != nil && time != nil && latitude != nil && longitude != nil){

    NSMutableString *postString = [NSMutableString stringWithString:kpostURL];

    [postString appendString:[NSString stringWithFormat:@"?%@=%@", kname, name]];

    [postString appendString:[NSString stringWithFormat:@"&%@=%@", kaddress, address]];

    [postString appendString:[NSString stringWithFormat:@"&%@=%@", kcity, city]];

    [postString appendString:[NSString stringWithFormat:@"&%@=%@", kcounty, county]];

    [postString appendString:[NSString stringWithFormat:@"&%@=%@", kcountry, country]];

    [postString appendString:[NSString stringWithFormat:@"&%@=%@", kpostcode, postcode]];

    [postString appendString:[NSString stringWithFormat:@"&%@=%@", kdate, date]];

    [postString appendString:[NSString stringWithFormat:@"&%@=%@", ktime, time]];

    [postString appendString:[NSString stringWithFormat:@"&%@=%@", klatitude, latitude]];

    [postString appendString:[NSString stringWithFormat:@"&%@=%@", klongitude, longitude]];

    [postString setString:[postString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];

    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:postString]];
    [request setHTTPMethod:@"POST"];

    postConnection = [[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:YES];

}

}


-(IBAction)post:(id)sender{

[self postMessage:(NSString*) postcodeText.text withName:nameText.text withAddress:addressText.text withCity:cityText.text withCounty:countyText.text withCountry:countryText.text withDate:dateText.text withTime:timeText.text withLatitude:latitudeText.text withLongitude:longitudeText.text];

[postcodeText resignFirstResponder];
nameText.text = nil;
addressText.text = nil;
cityText.text = nil;
countyText.text = nil;
countryText.text = nil;
postcodeText.text = nil;
dateText.text = nil;
timeText.text = nil;
latitudeText.text = nil;
longitudeText.text = nil;
[[NSNotificationCenter defaultCenter] postNotificationName:@"Test2" object:self];
}


-(IBAction)getLatLon:(id)sender{


NSString *eventName = [nameText text];
NSString *eventAddress = [addressText text];
NSString *eventCity = [cityText text];
NSString *eventCountry = [countryText text];
NSString *eventCounty = [countyText text];
NSString *eventDate = [dateText text];
NSString *eventPostCode = [postcodeText text];
NSString *eventTime = [timeText text];

// Use this to build the full event string
NSString *addressString = [NSString stringWithFormat:@"%@,%@,%@,%@,%@,%@,%@,%@", eventName, eventAddress, eventCity, eventCounty, eventCountry, eventPostCode, eventDate, eventTime];

// A search can't inlude the event and time and hour as it means nothing to MapKit
// The geocodeAddressString simply contains the address info
NSString *geocodeAddressString = [NSString stringWithFormat:@"%@,%@,%@,%@,%@", eventAddress, eventCity, eventCounty, eventCountry, eventPostCode];

//NSLog(@"Hello %@",addressString);
CLGeocoder *geocoder = [[CLGeocoder alloc] init]; 

[geocoder geocodeAddressString:geocodeAddressString completionHandler:^(NSArray* placemarks, NSError* error){
    // Check for returned placemarks
    if (placemarks && placemarks.count > 0) {
        CLPlacemark *topResult = [placemarks objectAtIndex:0];

        //NSLog([topResult locality]);
        //NSLog([topResult location]);
        //MKCoordinateRegion region;
        //double *value = topResult.region.center.latitude;

        CLLocationDegrees latitude = topResult.region.center.latitude;
        CLLocationDegrees longitude = topResult.location.coordinate.longitude;

        NSString *latitudeString = [NSString stringWithFormat:@"%f",latitude];
        NSString *longitudeString = [NSString stringWithFormat:@"%f",longitude];

        latitudeText.text = latitudeString;
        longitudeText.text = longitudeString;

        NSLog(@"Lat: %f", latitude);
        NSLog(@"Long: %f", longitude);
    }
}];
}
4

2 回答 2

1

当城市名称中有撇号时会发生什么?您可能想查看 mysql_real_escape_string。

无论如何,它们将被存储为浮点数。数据始终存储为列的数据类型(好吧,除非您将 null 计为数据类型:p)。

如果查询中的纬度和经度周围的引号是浮点数,则它们实际上是不必要的(尽管您希望在删除引号之前确保它们实际上是浮点数,否则您将打开 SQL 注入孔)。

现在会发生的是,当查询被执行时,MySQL 会默默地将字符串转换为浮点值。

于 2012-05-22T00:55:51.887 回答
0

它们将以 DB 字段的格式保存。因此,如果你有“int”,它们将被转换为整数,如果你有浮点数,它们将被四舍五入到最接近的浮点数,如果你有它作为 VarChar,它将被保存为一个字符串。

要保存为数字,请将表结构中的字段类型转换为数字 - 假设您已将条目验证为数字,那么相同的 SQL 查询就足够了(但在其他人指出之前可以改进:))

于 2012-05-22T00:57:15.197 回答