2010-07-20 152 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