2011-11-07 17 views
1

我試圖從我的表中檢索數據。但它總是返回(null)。這裏是我的代碼SQLite總是返回(空)的iphone中的文本值

-(NSString *)filePath 
{ 

    NSArray *paths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *documentDir=[paths objectAtIndex:0]; 
    return [documentDir stringByAppendingPathComponent:@"database.sql"]; 

} 

-(void)openDB 
{ 
    //sqlite#_open 
    if(sqlite3_open([[self filePath]UTF8String], &newDB)!=SQLITE_OK) 
    { 
     sqlite3_close(newDB); 
     NSAssert(0,@"Database fialed to open"); 
    } 
} 

-(void)readAttendeesFormeetingId:(NSInteger)mId 
{ 

    NSLog(@"readAttendeesFormeetingId==> %d",mId); 

    sqlite3_stmt *stmtAgenda=nil; 

    attendeesArray = [[NSMutableArray alloc]init]; 
    SQLiteConnClass *objsqlite1=[[SQLiteConnClass alloc]init]; 
    newdb=[objsqlite1 openDB]; 

    if (stmtAgenda==nil) { 

     const char *sql1 = "SELECT DISTINCT name FROM MeetingAttendees where meetingId= ? AND isPresent = 1"; 
     if (sqlite3_prepare_v2(newdb, sql1, -1, &stmtAgenda, NULL) != SQLITE_OK) { 

      NSAssert1(0, @"Error: failed to prepare statement with message '%s'.", sqlite3_errmsg(newdb)); 
     } 

    } 

    sqlite3_bind_int(stmtAgenda, 1, mId); 

    while(sqlite3_step(stmtAgenda) == SQLITE_ROW) { 

     const char *agendaTitle=(const char *)sqlite3_column_text(stmtAgenda,4); 

     NSString *stragendaTitle=agendaTitle == NULL ? nil:[[NSString alloc] initWithUTF8String:agendaTitle]; 


     //NSString *stragendaTitle = [[NSString alloc] initWithUTF8String:(const char *) sqlite3_column_text(stmtAgenda, 4)]; 

     NSLog(@"agendaTitle %@",stragendaTitle); 

     [attendeesArray addObject:stragendaTitle]; 

     [stragendaTitle release];      
    } 

    // Reset the statement for future reuse. 
    sqlite3_reset(stmtAgenda); 
    sqlite3_close(newDB); 
} 

這裏是我的表結構

enter image description here

我有我的表名值。任何行都不包含空值。仍然我得到空結果。

回答

4

您的SELECT語句正在返回一個只有一列(來自MeetingAttendees的不同名稱列)的表,但您的sqlite3_column_text調用嘗試從列索引4讀取。嘗試將4更改爲0並查看它是否返回你需要的數據。

+0

謝謝BP。它完美的作品。我認爲我們應該將表的列號傳遞給sqlite3_column_text。 – iOSAppDev

+0

沒問題,樂意幫忙。我建議在iPhone上使用FMDB進行SQLite數據庫訪問,這對於SQLite來說是一個易於使用和堅實的開源Objective-C包裝器。這裏是一個鏈接:https://github.com/ccgus/fmdb –