没有为iphone显示sqlite记录

时间:2010-07-07 01:00:08

标签: iphone sqlite

我只能在10条记录中显示最后的元素

qlite3 *database;


scores = [[NSMutableArray alloc] init];


if(sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK) {

        const char *sqlStatement = "select name,score from game";
        sqlite3_stmt *compiledStatement;
        if(sqlite3_prepare_v2(database, sqlStatement, -1, &compiledStatement, NULL) == SQLITE_OK) {



                // Loop through the results and add them to the feeds array
                while(sqlite3_step(compiledStatement) == SQLITE_ROW) {
                //if(sqlite3_step(compiledStatement) == SQLITE_ROW) {
                        // Read the data from the result row


                 Book *  aBook = [[Book alloc] init];

                // NSString *id = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 0)];
                aBook.id = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 0)];
aBook.name = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 1)];


                       SQLiteTutorialAppDelegate *appDelegate = (SQLiteTutorialAppDelegate *)[[UIApplication sharedApplication] delegate];
                appDelegate.books = [[NSMutableArray alloc] init];






                [books addObject:aBook];
       //        aBook.name  shows all 10 names

                } 
        }

        sqlite3_finalize(compiledStatement);

}
sqlite3_close(database);

现在如果我在cell.text=aBook.name这样的表视图中调用它,那么它只显示最后一个元素

1 个答案:

答案 0 :(得分:2)

在循环的每次迭代中,您将为appDelegate.books创建一个新数组,然后插入一本书。这就是为什么循环appDelegate.books只包含最后一个元素。

在循环之前只创建一次appDelegate和appDelegate.books。

回复 您很可能将每个单元格的文本设置为相同的书籍。

你说过用过'cell.text = aBook.name'。

你是怎么得到'aBook.name'的。您需要使用indexPath,如

Book* aBook = [appDelegate.books objectAtIndex:indexPath.row];