SQL查询由于某种原因无法正常工作

时间:2013-02-24 16:39:33

标签: ios objective-c xcode sqlite

我正在为排行榜系统编写此查询。它需要返回与目标分数相关的人的下一个最接近的目标。由于某种原因,我的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错误了

1 个答案:

答案 0 :(得分:1)

我认为您的错误可能是您正在使用数字引用。删除参数周围的引号。

因此,而不是Completed > '%d'使用Completed > %d(当然还有其他参数)

当你使用引号时,它很可能会将你的字符串转换为一个数字,这个数字可以解释为什么你会有意想不到的结果。