gpt4 book ai didi

ios - 随机选择、不重复的Sql语句

转载 作者:行者123 更新时间:2023-11-29 03:38:48 25 4
gpt4 key购买 nike

我在 iOS 上使用 sqlite 数据库创建了一个问答游戏。我可以随机检索我的问题及其正确答案,但我有两个问题。首先我想知道是否可以不重复同样的问题?其次,我希望玩家设置他想要玩的问题数量。对于第二个,我成功创建了一个带有文本字段的警报 View ,但是是否可以将值传递到 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;
}
}

最佳答案

  1. 您可以做的是添加一个列,让它对已使用的表示 YES,对未使用的表示 NO,然后修改您的 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];

关于ios - 随机选择、不重复的Sql语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18767094/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com