为什么我的UITableViewController不显示数据库中的数据?

时间:2014-03-03 11:43:06

标签: ios database sqlite uitableview

我在UITableViewController中显示从数据库中获取的数据时遇到问题。我的方法是:

-(void)createDataFromDB
{
   self.characterData = [[NSMutableArray alloc]initWithCapacity:20];

@try
{
    // give the database path
    NSString * path = [[NSBundle mainBundle] resourcePath];

    NSString * pathDB = [path stringByAppendingPathComponent:@"Characters.rdb"];
    const char * pathStr = [pathDB UTF8String];

    // open the db

    BOOL success = sqlite3_open(pathStr, &db);
    if(!success)
    {
        NSLog(@"Database cannot open \n");
    }

    // make a sql query

    const char * queryStr = "SELECT * FROM characters";

    NSLog(@"Query prepared \n");
    // declare a statement

    sqlite3_stmt * statement;

    sqlite3_prepare(db, queryStr, -1, &statement, NULL);

    if(!success)
    {
        NSLog(@"Query statement cannot prepare \n");
    }
    //run a query and iterate through the results

    while (sqlite3_step(statement) == SQLITE_ROW)
    {



        NSString * nameStr = [[NSString alloc ]initWithUTF8String:(char *)sqlite3_column_text(statement, 1)];
        NSString * typeStr = [NSString stringWithUTF8String:(char *)sqlite3_column_text(statement, 2)];
        NSString * availabilityStr = [NSString stringWithUTF8String:(char *)sqlite3_column_text(statement, 3)];
        NSString * imageStr = [NSString stringWithUTF8String:(char *)sqlite3_column_text(statement, 4)];
        NSString * iconStr = [NSString stringWithUTF8String:(char *)sqlite3_column_text(statement, 5)];

        Character * characters =[[Character alloc] initWithName: nameStr
                                                        andType: typeStr
                                                andAvailability:availabilityStr
                                                       andImage: imageStr
                                                        andIcon:iconStr];
        [self.characterData addObject: characters];

       NSLog(@" %@", nameStr);

         //NSLog(@"%@", characters.characterType);
         //NSLog(@"%@", characters.availability);
        // NSLog(@"%@", characters.image);

    }

}
@catch (NSException *exception)
{
    NSLog(@"Database Error %s", sqlite3_errmsg(db));
}

@finally
{
    // close the db
    sqlite3_close(db);
}
}

我实现原型单元的方法是:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
    NSLog(@" Cell created");


if(cell == nil)
{
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle
                                  reuseIdentifier:@"Cell"];
}


Character * character = self.characterData [indexPath.row];
NSLog(@"%@", character.name);
cell.textLabel.text = character.name;
cell.imageView.image = character.icon;

NSLog(@"%@", character.name);

return cell;
}

我已经尝试了很多次,但是在我的代码中找不到错误。查询很好构建,因为它适用于我的MesaSQLite数据库,但我试图在控制台上打印一些内容但它没有显示任何东西。请帮我!感谢

0 个答案:

没有答案