【问题标题】:Comparing integers in sqlite table?比较sqlite表中的整数?
【发布时间】:2010-02-27 13:33:59
【问题描述】:

我的游戏中有一张高分表。游戏结束后,分数会显示在屏幕上,并会被插入到高分表中。我想知道如何将新分数与表中的最高分数进行比较,以便让用户知道他们是否获得了高分。下面我包含了用于更新分数并将其插入表中的代码。保留的分数是一个整数 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;
}

所以我要找的是这样的东西......

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

【问题讨论】:

    标签: ios iphone sqlite sdk compare


    【解决方案1】:

    您可以使用

    检索表中的最高分
    select max(Score) from tblHard
    

    您可以使用sqlite3_column_int 检索此 SQL 查询的结果。这是代码(为简洁起见,删除了错误检查):

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

    然后你可以将当前的最高分数与globalScore进行比较:

    if (globalScore > highestScore) {
        /* ... */
    }
    

    【讨论】:

    • 我需要能够将它与我的 GameViewController 中的新分数进行比较
    • 已编辑。我添加了代码来执行查询并检索结果。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-03-01
    • 2019-03-25
    • 2017-02-11
    • 2015-11-11
    • 2013-07-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多