2011-11-26 120 views
0

我想從SQLite數據庫(NSArray的字符串)中獲取數據並填充表視圖單元格中的內容。我試圖在命令提示符下執行查詢,它工作正常。但是在代碼中,它返回一個空數組。SQLite select語句返回空數組

NSString *temp = [[NSString alloc]initWithFormat:@"select img.image from images img,illness i,illness_images ii where img.img_id=ii.img_id and i.i_id=ii.i_id and i.i_id = '%d'",illid]; 
     const char *sqlStatement = [temp UTF8String]; 
     sqlite3_stmt *compiledStatement; 
     if(sqlite3_prepare_v2(database, sqlStatement, -1, &compiledStatement, NULL) == SQLITE_OK) 
     { 
      // Loop through the results and add them to the array 
      while(sqlite3_step(compiledStatement) == SQLITE_ROW) 
      { 
       // Read the data from the result row 
       NSString *imgname = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 0)]; 

       [imglist addObject:imgname]; 
       sqlite3_finalize(compiledStatement); 

      } 
     } 
     else{ 
      NSAssert1(0,@"Error retrieving image names '%s'",sqlite3_errmsg(database)); 
     } 

我試圖調試,當它到達時(sqlite3_step(compiledStatement)== SQLITE_ROW),它不進入循環。 不知道我在做什麼錯誤。

謝謝!

回答

1

將字符串查詢分配給temp變量後,嘗試NSLog。 並檢查變量illid是否爲您提供了正確的值,並直接在數據庫上運行該查詢。

NSString *temp = [NSString stringWithFormat:@"select img.image from images img,illness i,illness_images ii where img.img_id=ii.img_id and i.i_id=ii.i_id and i.i_id = %d",illid]; 

你最好releasealloc ated對象或使用類方法的設定值,進行內存管理。

HTH。

+0

太棒了!你是對的。我沒有傳遞正確的價值觀。所以愚蠢的我意識到這麼久了!可能我需要睡一會兒。謝謝Himanshu!它確實有幫助。 – user1065969

+0

順便說一句,我正在做下面的發佈。這裏不包括代碼:) – user1065969