Hasan
Hasan

Reputation:

iphone - SQLite records on table not coming?

I have a table and 3 records.

and I have the code;

-(void) readScoreFromDatabase {

sqlite3 *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

            NSString *aName =[NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 0)];
            NSString *aScore =[NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 1)];


            DatabaseClass *dbOBJ = [[DatabaseClass alloc] initWithName:aName score:aScore];


            [scores addObject:dbOBJ];

            [dbOBJ release];
        } 
    }

    sqlite3_finalize(compiledStatement);

} else {
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" message:@"No Connection" 
                                                   delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil];
    [alert show];
}
sqlite3_close(database);

}

and I am using that code to show records;

-(IBAction)refreshClick:(id)sender {

// Navigation logic -- create and push a new view controller
IDRGameAppDelegate *appDelegate = (IDRGameAppDelegate *)[[UIApplication sharedApplication] delegate];
DatabaseClass *dbOBJ = (DatabaseClass *)[appDelegate.scores objectAtIndex:1];

game1NameLabel.text = dbOBJ.name;
score1Label.text = dbOBJ.score;
}

I have 3 records, but I can take only one record. I mean changed that line "DatabaseClass *dbOBJ = (DatabaseClass *)[appDelegate.scores objectAtIndex:1];"
objectAtIndex:1 when I change this value 1 or 2 or 3 etc. the result doesn't change. It is always showing one record from 3 records. I don't understand the reason.

Thank you.

Upvotes: 0

Views: 261

Answers (3)

user292049
user292049

Reputation: 1138

Are you able to see the table rows, when you go to the SQLite manager (from Firefox) and run the below query:

select name,score from game;

If you are able to see 3 records, then:

  • Go to the iPhone simulator and reset settings (which will clear all the temp caches, database, and build files).

  • Go to Xcode and do a Build and GO.

This will get you latest database; put it in the Build folder.

Upvotes: 1

oxigen
oxigen

Reputation: 6263

Maybe you forgot to set score to your delegate?

scores = [[NSMutableArray alloc] init];
....

//I don't see this in your code
((IDRGameAppDelegate *)[[UIApplication sharedApplication] delegate]).score = score;    

Upvotes: 0

Dan Lorenc
Dan Lorenc

Reputation: 5394

This may not be the answer you want, but you should really consider using Core Data now that it's in iPhone OS 3.0. It handles most of this for you and is very easy to use.

Upvotes: 0

Related Questions