SQL语句问题

时间:2010-07-20 13:29:44

标签: iphone sql sqlite

我有以下代码:

if(sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK) 
    {
        // Setup the SQL Statement and compile it for faster access
        const char *sqlStatement = "select ZWAYPOINT_X, ZWAYPOINT_Y from ZWAYPOINT where ZMAP_ID %@", mapID;
        sqlite3_stmt *compiledStatement;
        if(sqlite3_prepare_v2(database, sqlStatement, -1, &compiledStatement, NULL) == SQLITE_OK) 
        {
            // Loop through the results and add them to the Route Name array
            while(sqlite3_step(compiledStatement) == SQLITE_ROW) 
            {
                // Read the data from the result row
                if((char*)sqlite3_column_text(compiledStatement, 0) != NULL)
                {                                       
                    NSString *xCoordinate = [NSString stringWithUTF8String:(char*)sqlite3_column_text(compiledStatement, 0)];
                    NSString *yCoordinate = [NSString stringWithUTF8String:(char*)sqlite3_column_text(compiledStatement, 1)];

                    NSLog(@"xCoordinate: %@", xCoordinate);
                    NSLog(@"yCoordinate: %@", yCoordinate);

                    CLLocationCoordinate2D coordinate = {xCoordinate, yCoordinate};
                    MapPin *pin = [[MapPin alloc]initWithCoordinates:coordinate
                                                           placeName:@"Keenan Stadium"
                                                         description:@"Tar Heel Football"];
                    [self.mapView addAnnotation:pin];
                    [pin release];
                }
            }
        }
        else 
        {
            NSLog(@"Error: failed to select details from database with message '%s'.", sqlite3_errmsg(database));
        }

我有几个问题:

  1. 在我的SQL语句中如何将变量mapID作为SQL语句的一部分包含在内
  2. 这一行

    CLLocationCoordinate2D coordinate = {xCoordinate,yCoordinate};

  3. 给我以下警告“初始化中的不兼容类型”

    由于

3 个答案:

答案 0 :(得分:0)

包含MapID,

改变这个:

const char *sqlStatement = "select ZWAYPOINT_X, ZWAYPOINT_Y from ZWAYPOINT where ZMAP_ID %@", mapID; 

对此:

const char *sqlStatement = "select ZWAYPOINT_X, ZWAYPOINT_Y, MapID from ZWAYPOINT where ZMAP_ID %@", mapID; 

答案 1 :(得分:0)

由于值不是NSString *而是double,因此您会收到一条警告来创建该结构。 http://developer.apple.com/iphone/library/documentation/CoreLocation/Reference/CLLocation_Class/CLLocation/CLLocation.html#jumpTo_17

答案 2 :(得分:0)

您应该使用参数化语句并绑定值,如下所示:

const char *sqlStatement = "select ZWAYPOINT_X, ZWAYPOINT_Y from ZWAYPOINT where ZMAP_ID = ?";
sqlite3_stmt *compiledStatement;
if(sqlite3_prepare_v2(database, sqlStatement, -1, &compiledStatement, NULL) == SQLITE_OK) 
{
    sqlite3_bind_text( compiledStatement, 1, [mapID UTF8String], -1, SQLITE_TRANSIENT );

(请注意,绑定参数时,从1开始,但从结果行读取列时,从0开始...)。我假设你的mapID是一个NSString,因为你试图使用%@将它粘贴在你的查询中。如果是其他内容,则必须使用其他bind function并明显跳过UTF8String