我正在为排行榜系统编写此查询。它需要返回与目标分数相关的下一个最接近的目标。出于某种原因,我的 sql 查询不起作用。
这是我的查询:
[NSString stringWithFormat:@"SELECT * FROM Children WHERE Completed > '%d' OR ( Completed = '%d' AND 'Current Stickers' > '%d')",completecount,completecount, stickercount];
completecount = 此人完成目标的次数stickercount = 此人拥有的贴纸数量。两者都是整数所以我希望查询返回完成它的人比这个人更多,或者它的次数是否相同,并且他们是否有比他们更多的贴纸。
数据库布局:
NSString *sql = [NSString stringWithFormat:
@"CREATE TABLE IF NOT EXISTS '%@' ('%@'"
"TEXT PRIMARY KEY, '%@' TEXT, '%@' TEXT, '%@' TEXT, '%@' INTEGER, '%@' INTEGER, '%@' TEXT, '%@' INTEGER, '%@' INTEGER, '%@' INTEGER, '%@' INTEGER);", tableName, field1, field2, field3, field4, field5, field6, field7, field8, field9, field10, field11];
字段名称:
[self createTable:@"Children" withField1:@"Name" withField2:@"Password" withField3:@"House" withField4:@"Sticker Collection" withField5:@"Tickets Gathered" withField6:@"Tickets Removed" withField7:@"Last Ticket Scanned" withField8:@"Current Tickets" withField9:@"Completed" withField10:@"Complete" withField11:@"Current Stickers"];
这是实际的查询:
sqlite3_stmt *statement;
if (sqlite3_prepare_v2(Childdb, [sql UTF8String], -1, &statement, nil)==SQLITE_OK) {
while (sqlite3_step(statement)==SQLITE_ROW) {
char *field1 = (char *) sqlite3_column_text(statement, 0);
NSString *field1Str = [[NSString alloc]initWithUTF8String:field1];
char *field2 = (char *) sqlite3_column_text(statement, 8);
NSString *field2Str = [[NSString alloc]initWithUTF8String:field2];
char *field3 = (char *) sqlite3_column_text(statement, 10);
NSString *field3Str = [[NSString alloc]initWithUTF8String:field3];
NSLog(@"Name:%@",field1Str);
NSLog(@"this is completecount: %@", field2Str);
NSLog(@"this is stickcount: %@",field3Str);
所以真正的问题是,尽管查询设置它仍然返回无效记录。看这个例子:
这个人的名字是斯图尔特:
2013-02-24 16:35:55.409 TableViewStory[15672:907] stickercount = 3
2013-02-24 16:35:55.410 TableViewStory[15672:907] completecount = 0
2013-02-24 16:35:55.412 TableViewStory[15672:907] Name:Oliver
2013-02-24 16:35:55.413 TableViewStory[15672:907] this is completecount: 1
2013-02-24 16:35:55.414 TableViewStory[15672:907] this is stickcount: 0
2013-02-24 16:35:55.415 TableViewStory[15672:907] Name:Rupert
2013-02-24 16:35:55.416 TableViewStory[15672:907] this is completecount: 2
2013-02-24 16:35:55.417 TableViewStory[15672:907] this is stickcount: 0
2013-02-24 16:35:55.418 TableViewStory[15672:907] Name:Emily
2013-02-24 16:35:55.419 TableViewStory[15672:907] this is completecount: 0
2013-02-24 16:35:55.420 TableViewStory[15672:907] this is stickcount: 0
2013-02-24 16:35:55.421 TableViewStory[15672:907] Name:Stuart
2013-02-24 16:35:55.423 TableViewStory[15672:907] this is completecount: 0
2013-02-24 16:35:55.424 TableViewStory[15672:907] this is stickcount: 3
看看这个,它甚至在结果中显示自己 ^ 在这里。不仅如此,它甚至显示了 Emily 的完整计数但不比 Stuart 更多的贴纸,而且 Emily 仍然被检索到。这里发生了什么?
Ege Akpinar 要求删除部分 where 子句:
NSString *sql = [NSString stringWithFormat:@"SELECT * FROM Children WHERE Completed > %d",completecount];
结果:
2013-02-24 17:37:22.626 TableViewStory[15814:907] This is the sticker count: 4
2013-02-24 17:37:22.627 TableViewStory[15814:907] This is the complete count: 0
2013-02-24 17:37:22.629 TableViewStory[15814:907] Name:Oliver
2013-02-24 17:37:22.629 TableViewStory[15814:907] this is completecount: 1
2013-02-24 17:37:22.630 TableViewStory[15814:907] this is stickcount: 0
2013-02-24 17:37:22.631 TableViewStory[15814:907] Name:Rupert
2013-02-24 17:37:22.632 TableViewStory[15814:907] this is completecount: 2
2013-02-24 17:37:22.633 TableViewStory[15814:907] this is stickcount: 0
完全删除 where 子句:
NSString *sql = [NSString stringWithFormat:@"SELECT * FROM Children"];
结果:
2013-02-24 17:38:42.698 TableViewStory[15829:907] This is the sticker count: 4
2013-02-24 17:38:42.699 TableViewStory[15829:907] This is the complete count: 0
2013-02-24 17:38:42.700 TableViewStory[15829:907] Name:Oliver
2013-02-24 17:38:42.701 TableViewStory[15829:907] this is completecount: 1
2013-02-24 17:38:42.702 TableViewStory[15829:907] this is stickcount: 0
2013-02-24 17:38:42.703 TableViewStory[15829:907] Name:Rupert
2013-02-24 17:38:42.704 TableViewStory[15829:907] this is completecount: 2
2013-02-24 17:38:42.705 TableViewStory[15829:907] this is stickcount: 0
2013-02-24 17:38:42.707 TableViewStory[15829:907] Name:Emily
2013-02-24 17:38:42.708 TableViewStory[15829:907] this is completecount: 0
2013-02-24 17:38:42.709 TableViewStory[15829:907] this is stickcount: 0
2013-02-24 17:38:42.710 TableViewStory[15829:907] Name:Stuart
2013-02-24 17:38:42.711 TableViewStory[15829:907] this is completecount: 0
2013-02-24 17:38:42.712 TableViewStory[15829:907] this is stickcount: 4
将括号放在语句上:
NSString *sql = [NSString stringWithFormat:@"SELECT * FROM Children WHERE (Completed > %d) OR (Completed = %d AND 'Current Stickers' > %d)",completecount,completecount, stickercount];
这是结果:
2013-02-24 17:59:33.987 TableViewStory[15898:907] This is the sticker count: 4
2013-02-24 17:59:33.988 TableViewStory[15898:907] This is the complete count: 0
2013-02-24 17:59:33.990 TableViewStory[15898:907] Name:Oliver
2013-02-24 17:59:33.991 TableViewStory[15898:907] this is completecount: 1
2013-02-24 17:59:33.992 TableViewStory[15898:907] this is stickcount: 0
2013-02-24 17:59:33.993 TableViewStory[15898:907] Name:Rupert
2013-02-24 17:59:33.994 TableViewStory[15898:907] this is completecount: 2
2013-02-24 17:59:33.995 TableViewStory[15898:907] this is stickcount: 0
2013-02-24 17:59:33.996 TableViewStory[15898:907] Name:Emily
2013-02-24 17:59:33.997 TableViewStory[15898:907] this is completecount: 0
2013-02-24 17:59:33.998 TableViewStory[15898:907] this is stickcount: 0
2013-02-24 17:59:33.999 TableViewStory[15898:907] Name:Stuart
2013-02-24 17:59:34.000 TableViewStory[15898:907] this is completecount: 0
2013-02-24 17:59:34.002 TableViewStory[15898:907] this is stickcount: 4
现在尝试当前贴纸>:
NSString *sql = [NSString stringWithFormat:@"SELECT * FROM Children WHERE 'Current Stickers' > %d",stickercount];
结果与斯图尔特:
2013-02-24 18:05:27.855 TableViewStory[15942:907] This is the sticker count: 4
2013-02-24 18:05:27.856 TableViewStory[15942:907] This is the complete count: 0
2013-02-24 18:05:27.857 TableViewStory[15942:907] Name:Oliver
2013-02-24 18:05:27.858 TableViewStory[15942:907] this is completecount: 1
2013-02-24 18:05:27.860 TableViewStory[15942:907] this is stickcount: 0
2013-02-24 18:05:27.861 TableViewStory[15942:907] Name:Rupert
2013-02-24 18:05:27.862 TableViewStory[15942:907] this is completecount: 2
2013-02-24 18:05:27.863 TableViewStory[15942:907] this is stickcount: 0
2013-02-24 18:05:27.864 TableViewStory[15942:907] Name:Emily
2013-02-24 18:05:27.865 TableViewStory[15942:907] this is completecount: 0
2013-02-24 18:05:27.866 TableViewStory[15942:907] this is stickcount: 0
2013-02-24 18:05:27.867 TableViewStory[15942:907] Name:Stuart
2013-02-24 18:05:27.868 TableViewStory[15942:907] this is completecount: 0
2013-02-24 18:05:27.870 TableViewStory[15942:907] this is stickcount: 4
这让我觉得stickcount有问题