开发者

sqlite records are not displaying for iphone

开发者 https://www.devze.com 2023-01-06 03:10 出处:网络
i can only show last elemnts out of 10 records qlite3 *database; scores = [[NSMutableArray alloc] init];

i can only show last elemnts out of 10 records

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(databa开发者_Go百科se);

now if i call this in table view like cell.text=aBook.name so it only display last element


In each iteration of the loop you are creating a new array for appDelegate.books then inserting a book. This is why after the loop appDelegate.books only contains the last element.

Create the appDelegate and appDelegate.books only once before the loop instead.

In Response Most likely you are setting each cell's text to the SAME book.

You said you used 'cell.text=aBook.name'.

How are you getting 'aBook.name'. You will need to use the indexPath like

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

精彩评论

暂无评论...
验证码 换一张
取 消