2010-07-20 79 views
0

我有以下代码:问题的SQL语句

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语句我怎么包括可变的azazaz作为SQL语句
  2. 的一部分
  3. 该行

    CLLocationCoordinate2D coordinate = {xCoordinate,yCoordinate};

给了我下面的警告 “不兼容类型的初始化”

感谢

+0

您可以通过移动到核心数据解决您的SQL语句的问题;) – jer 2010-07-20 13:31:45

+0

耶,有趣的是我见过几个人建议这个时候关于SQL的问题张贴。我现在承诺虽然....我肯定会审查核心数据后,我有这个程序完成。感谢您的建议。 – Stephen 2010-07-20 13:53:47

+0

这不是SQL语句,它是程序代码。 – onedaywhen 2010-07-20 14:40:25

回答

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

0

要包括的azazaz,

更改此:

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; 
+0

感谢JonH,只需回读了我原来的职位我没有解释自己很好。 mapID被定义为一个全局变量,从另一个表中填充。在我上面的代码中,我有一个警告'未使用的变量mapID'。很明显,每次运行这个代码时,mapID都可以包含变量。我希望这有点清楚。 – Stephen 2010-07-20 13:48:59