views:

123

answers:

1

I have a high scores table in my game. When the game is over, the score is shown on the screen and it gets inserted into the high scores table. I want to know how to compare the new score to the highest score in the table so I can let the user know if they achieved a high score. Below I included the code I use to update and insert the score into the table. The score being kept is an integer, globalScore.

-(void)updateScores:(NSInteger)Primary_key
{
    sqlite3_stmt *statement=nil;
    NSString  *sql=nil; 
    @try
    {
        statement=nil;
        sql=nil;    


        sql=[NSString stringWithFormat:@"update tblHard set Score=? where id=%d",Primary_key];


        if(sqlite3_prepare_v2(database, [sql UTF8String], -1, &statement, NULL)!=SQLITE_OK)
        {
            NSAssert1(0, @"Error: failed to prepare statement with message '%s'.", sqlite3_errmsg(database));
        }

        sqlite3_bind_int(statement, 1, globalScore);

        int success=sqlite3_step(statement);

        if (success == SQLITE_ERROR) {

            NSAssert1(0, @"Error: failed to insert into the database with message '%s'.", sqlite3_errmsg(database));
        }
        sqlite3_finalize(statement);
        statement=nil;
        sql=nil;
    }
    @catch (NSException *e) 
    {
        NSLog(@"asd");
    }
}

-(int)InsertGame
{
    int i=0;
    sqlite3_stmt *statement=nil;
    NSString  *sql=nil; 
    @try
    {
        statement=nil;
        sql=nil;    


        sql=[NSString stringWithFormat:@"insert into tblHard(Score) values (?)"];


        if(sqlite3_prepare_v2(database, [sql UTF8String], -1, &statement, NULL)!=SQLITE_OK)
        {
            NSAssert1(0, @"Error: failed to prepare statement with message '%s'.", sqlite3_errmsg(database));
        }

        sqlite3_bind_int(statement, 1, globalScore);

        int success=sqlite3_step(statement);

        if (success == SQLITE_ERROR) {

            NSAssert1(0, @"Error: failed to insert into the database with message '%s'.", sqlite3_errmsg(database));
        }
        i= sqlite3_last_insert_rowid(database);
        sqlite3_finalize(statement);
        statement=nil;
        sql=nil;
    }
    @catch (NSException *e) 
    {
        NSLog(@"asd");
    }
    return i;
}

So what I'm looking for is something like this...

if(globalScore > "the highest score in the table"){
highscorelabel.hidden=NO;
}
A: 

You can retrieve the highest score in the table with

select max(Score) from tblHard

You can retrieve the result of this SQL query with sqlite3_column_int. Here is the code (error checking removed for brevity):

int highestScore;
/* ... */
sqlite3_prepare_v2(database, "select max(Score) from tblHard", -1,
                   &statement, NULL);
sqlite3_step(statement);
highestScore = sqlite3_column_int(statement, 0);

And then you can compare the current maximum score with globalScore:

if (globalScore > highestScore) {
    /* ... */
}
Danilo Piazzalunga
I need to be able to compare it to the new score in my GameViewController
NextRev
Edited. I added the code to execute the query and retrieve the result.
Danilo Piazzalunga