tags:

views:

27

answers:

2

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(database);

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

+1  A: 

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];
Akusete
hey i did as you said but now i can see all 10 records of same name plz helpi did this before while loop Book * aBook = [[Book alloc] init]; SQLiteTutorialAppDelegate *appDelegate = (SQLiteTutorialAppDelegate *)[[UIApplication sharedApplication] delegate]; appDelegate.books = [[NSMutableArray alloc] init];
ram
perfect u saved my life hats of to ur knowledge :-)
ram
A: 

hey now while searching item in table i can trace those value but i cant update my display can you tell me my mistake

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

         //NSLog(@"TESTtttttttttttt");
         // Setup the SQL Statement and compile it for faster access
         const char *sqlStatement = "SELECT * FROM wateat_tbl where name like '%love%' or desc like '%love%'";
     // NSLog(@"query %c", sqlStatement);
        // const char *sql = [[NSString stringWithFormat:@"SELECT * FROM destinationsData WHERE DisplayName = '%@'", placeName] UTF8String];
        // NSString *file = [[NSBundle mainBundle] pathForResource:@"wat2eatTest" ofType:@"sqlite"];
        // sqlite3 *database = NULL;

         sqlite3_stmt *compiledStatement;


    //   XMLAppDelegate *appDelegate=(XMLAppDelegate*)[UIApplication sharedApplication].delegate;
    //   appDelegate.books = [[NSMutableArray alloc] init];
         if(sqlite3_prepare_v2(database, sqlStatement, -1, &compiledStatement, NULL) == SQLITE_OK) {
             NSLog(@" DATA IN ")working
             while(sqlite3_step(compiledStatement) == SQLITE_ROW) {

                  NSLog(@" DATA base Process  ");
                 int myInt = sqlite3_column_int(compiledStatement, 0);
                 NSString *myStringResult = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 1)];

                 NSLog(@" data base vlaue%@",myStringResult);
                //  

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




             }


         }
         sqlite3_finalize(compiledStatement);

sqlite3_close(database); //[super viewDidLoad]; [self.tableView reloadData]; [self searchTableView];

ram