2014-07-08 61 views
1

我想在iphone應用程序中的數據庫中顯示錶中的數據。我用下面的代碼來執行它。但它沒有顯示錶格中的數據。IOS數據庫沒有檢索到數據

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
CustomTableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:@"CustomCell"]; 
const char *dbpath = [_databasePath UTF8String]; 
sqlite3_stmt *statement; 

if (sqlite3_open(dbpath, &_beaconDB) == SQLITE_OK) 
{ 
    NSString *querySQL = [NSString stringWithFormat: 
          @"SELECT vendor_name, enter_message, enter_image FROM beacons WHERE major=%d AND minor=%d", major[indexPath.row], minor[indexPath.row]]; 

    const char *query_stmt = [querySQL UTF8String]; 
    if (sqlite3_prepare_v2(_beaconDB, 
         query_stmt, -1, &statement, NULL) == SQLITE_OK) 
    { 
     if (sqlite3_step(statement) == SQLITE_ROW) 
     { 
      NSString *title = [[NSString alloc] 
           initWithUTF8String: 
           (const char *) sqlite3_column_text(
                   statement, 0)]; 
      NSString *description = [[NSString alloc] 
          initWithUTF8String:(const char *) 
          sqlite3_column_text(statement, 1)]; 
      cell.title.text = title; 
      cell.description.text = description; 
     } 
    sqlite3_finalize(statement); 
    } 
    sqlite3_close(_beaconDB); 
} 

return cell; 
} 

在上面的「主要」是一個數組。我想要逐個獲取數組值。

在此先感謝。

+0

多少行你的函數'numberOfRowsInsection'回報?另外你不這麼認爲把這個數據檢索代碼放在'cellForRowAtIndexPath'函數之外。 –

+0

它返回主數值的數量。還有如何將此檢索代碼放在cellForRowAtIndexPath之外並在表格單元格中顯示值 –

+0

請參閱本教程http://blog.objectgraph.com/index.php/2010/04/08/how-to-use-sqlite-with -uitableview-in-iphone-sdk /它會很老,但很有幫助。 –

回答

1

我解決了這個問題。我在下面一行中犯了錯誤。

@"SELECT vendor_name, enter_message, enter_image FROM beacons WHERE major=%d AND minor=%d", major[indexPath.row], minor[indexPath.row]]; 

而不是使用major[indexPath.row]

我必須使用major[objectAtIndex: indexPath.row]和也爲minor