2014-10-20 56 views
0

我正在使用FMDB。我創建並插入到表中。它工作正常。使用FMDB無法讀取數據

IDX INTEGER NOT NULL DEFAULT 0 PRIMARY KEY AUTOINCREMENT

topicId VARCHAR NOT NULL

listChat BLOB NOT NULL

enter image description here

但我不能得到所有數據從listChat列表。這裏是我的代碼:

- (NSMutableArray*)readChatHistoryFromDatabaseWithTopicId:(NSString *)topicId { 
    NSMutableArray *listChat = [[NSMutableArray alloc] init]; 
    NSFileManager *fileManager = [NSFileManager defaultManager]; 
    NSString *path = [self databasePath]; 
    if ([fileManager fileExistsAtPath:path] == YES) { 
     FMDatabase *database = [FMDatabase databaseWithPath:path]; 
     if (database) { 
      [database open]; 
      NSString *query = [NSString stringWithFormat:@"SELECT * FROM %@ WHERE topicId=\"%@\"", OCSDK_CHAT_HISTORY_TABLE_NAME, topicId]; 
      FMResultSet *results = [database executeQuery:query]; 
      [results next]; 
      NSData *notesData = [results dataForColumn:@"listChat"]; 
      [listChat addObject:notesData]; 
      NSLog(@"notes: %@", listChat); 
     } 
     [database close]; 
    } 
    return listChat; 
} 

它打印:

enter image description here

什麼是錯我的代碼?

回答

0

更改代碼:

- (NSMutableArray*)readChatHistoryFromDatabaseWithTopicId:(NSString *)topicId { 
    NSMutableArray *listChat = [[NSMutableArray alloc] init]; 
    NSFileManager *fileManager = [NSFileManager defaultManager]; 
    NSString *path = [self databasePath]; 
    if ([fileManager fileExistsAtPath:path] == YES) { 
     FMDatabase *database = [FMDatabase databaseWithPath:path]; 
     if (database) { 
      [database open]; 
      NSString *query = [NSString stringWithFormat:@"SELECT * FROM %@ WHERE topicId=\"%@\"", OCSDK_CHAT_HISTORY_TABLE_NAME, topicId]; 
      FMResultSet *results = [database executeQuery:query]; 
      while([results next]) { 

       NSString *noteString = [results stringForColumn:@"topicId"] 
       [listChat addObject: noteString]; 
       NSLog(@"notes: %@", listChat); 
      } 
     } 
     [database close]; 
    } 
    return listChat; 
} 

您的關鍵topicId取出由數據庫NSData值。您需要改爲獲取NSString值。檢查上面的代碼。

0

看起來筆記數組有一個記錄的NSData對象,所以你的代碼沒有問題。但我猜你想要topicId字符串,用[results [email protected]"topicId"]來代替。