0

我在 iOS 上创建了一个带有 sqlite 数据库的问答游戏。我可以用正确的答案随机检索我的问题,但我有两个问题。首先我想知道是否可以不重复同样的问题?其次,我希望玩家设置他想玩的问题的数量。对于第二个,我成功地创建了一个带有文本字段的警报视图,但是是否可以将值传递到 sql 语句中?先感谢您。

这是我用于随机问题的 sql 语句的代码:

const char *sql = "SELECT * FROM Questions WHERE Category IS 2 ORDER BY RANDOM() LIMIT 1";

所有的代码是:

-(NSMutableArray *) categoriesList{
categories = [[NSMutableArray alloc] initWithCapacity:10];
@try {
    NSFileManager *fileMgr = [NSFileManager defaultManager];
    NSString *dbPath = [[[NSBundle mainBundle] resourcePath ]stringByAppendingPathComponent:@"Categories.sqlite"];
    BOOL success = [fileMgr fileExistsAtPath:dbPath];
    if(!success)
    {
        NSLog(@"Cannot locate database file '%@'.", dbPath);
    }
    if(!(sqlite3_open([dbPath UTF8String], &db) == SQLITE_OK))
    {
        NSLog(@"An error has occured: %s", sqlite3_errmsg(db));
    }


    const char *sql = "SELECT * FROM Questions WHERE Category IS 2 ORDER BY RANDOM() LIMIT 1";
    sqlite3_stmt *sqlStatement;
    NSString * insertSQL = [NSString stringWithFormat:
                            @"UPDATE Questions SET Used='NO'"];
    const char * insert_stmt = [insertSQL UTF8String];
    sqlite3_prepare_v2(db, insert_stmt,-1, &sqlStatement, NULL);

    if(sqlite3_prepare(db, sql, -1, &sqlStatement, NULL) != SQLITE_OK)
    {
        NSLog(@"Problem with prepare statement:  %s", sqlite3_errmsg(db));
    }else{

        while (sqlite3_step(sqlStatement)==SQLITE_ROW) {

            Categories * choise = [[Categories alloc] init];

            question.text = [NSString stringWithUTF8String:(char *) sqlite3_column_text(sqlStatement, 1)];

            NSMutableArray  *arary = [[NSMutableArray alloc]init];
            while (arary.count < 4) {
                int value = arc4random()%4+2;
                BOOL isFound = [[arary filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:[NSString stringWithFormat:@"intValue == %d",value]]] count];
                if(!isFound)
                    [arary addObject:[NSNumber numberWithInteger:value]];
            }
            answer1.text = [NSString stringWithUTF8String:(char *) sqlite3_column_text(sqlStatement, [[arary objectAtIndex:0] intValue])];
            answer2.text = [NSString stringWithUTF8String:(char *) sqlite3_column_text(sqlStatement, [[arary objectAtIndex:1] intValue])];
            answer3.text = [NSString stringWithUTF8String:(char *) sqlite3_column_text(sqlStatement, [[arary objectAtIndex:2] intValue])];
            answer4.text = [NSString stringWithUTF8String:(char *) sqlite3_column_text(sqlStatement, [[arary objectAtIndex:3] intValue])];

            NSString * columnName  = [NSString stringWithUTF8String:(char *) sqlite3_column_name(sqlStatement, [[arary objectAtIndex:0] intValue])];

            answer1.tag = ([columnName isEqualToString:@"Answer1"])?999:0;

            columnName = [NSString stringWithUTF8String:(char *) sqlite3_column_name(sqlStatement, [[arary objectAtIndex:1] intValue])];

            answer2.tag = ([columnName isEqualToString:@"Answer1"])?999:0;


            columnName = [NSString stringWithUTF8String:(char *) sqlite3_column_name(sqlStatement, [[arary objectAtIndex:2] intValue])];

            answer3.tag = ([columnName isEqualToString:@"Answer1"])?999:0;


            columnName = [NSString stringWithUTF8String:(char *) sqlite3_column_name(sqlStatement, [[arary objectAtIndex:3] intValue])];

            answer4.tag = ([columnName isEqualToString:@"Answer1"])?999:0;

            answer1btn.tag = answer1.tag;
            answer2btn.tag = answer2.tag;
            answer3btn.tag = answer3.tag;
            answer4btn.tag = answer4.tag;
            [categories addObject:choise];
            [self prepareForIntroAnimation];
            [self performIntroAnimation];
        }
    }
    sqlite3_finalize(sqlStatement);
}
@catch (NSException *exception) {
    NSLog(@"Problem with prepare statement:  %s", sqlite3_errmsg(db));
}
@finally {

    sqlite3_close(db);

    return categories;
}
}
4

1 回答 1

1
  1. 您可以做的是添加一列,并让它对已使用说“是”,对未使用说“否”,然后修改您的 SQL 语句以说出where used=NO或类似的内容。

  2. 您可以使用字符串并将值传递给字符串,然后将其转换为字符:

    NSString *sqlStr = [NSString stringWithFormat:@"SELECT * FROM Questions WHERE Category IS 2 ORDER BY RANDOM() LIMIT %i", [limitTextfield.text intValue]];
    
    const char *sqlChar = [sqlStr UTF8String];
    

编辑

回答您的评论。在 viewDidLoad 中,您可以通过使用将整个“已使用”列设置为“否”

UPDATE Questions
SET used = 'NO'
WHERE used = 'YES';

所以这会将列中的所有元素重置为“YES”部分的默认值“NO”

NSString *sqlStr = [NSString stringWithFormat:@"UPDATE Questions SELECT * FROM Questions WHERE questionColumn = '%@' SET used = 'YES'", questionLabel.text intValue];
const char *sqlChar = [sqlStr UTF8String]; 
于 2013-09-12T14:38:25.940 回答