我在 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;
}
}