sqlite データベースを使用して、iOS でクイズ ゲームを作成しました。質問をランダムに取得して、その回答を正しく返すことはできますが、2 つの質問があります。まず、同じ質問を繰り返さないようにすることは可能ですか? 次に、プレイヤーがプレイしたい質問の数を設定してもらいたいと思います。2番目のものについては、テキストフィールドを使用してアラートビューを正常に作成しましたが、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;
}
}