1

リーダーボード システム用にこのクエリを書いています。ターゲット スコアに関連して、その人に次に近いターゲットを返す必要があります。何らかの理由で、SQL クエリが機能しません。

これは私のクエリです:

[NSString stringWithFormat:@"SELECT * FROM Children WHERE Completed > '%d' OR ( Completed = '%d' AND 'Current Stickers' > '%d')",completecount,completecount, stickercount]; 

completecount = この人が目標を達成した回数 stickercount = この人が持っているステッカーの数。両方とも ints ですので、その人よりも多く完了した人、または同じ回数で、それらよりも多くのステッカーを持っている人をクエリで返すようにします。

データベースのレイアウト:

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

これをチェックしてください。結果にも自分自身が表示されています^ here. それだけでなく、完全な数は同じであるがスチュアートよりも多くのステッカーを持っていないエミリーも表示されますが、エミリーはまだ取得されています。何が起きてる?

where 句の一部を削除する Ege Akpinar による要求:

  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

これは、スティックカウントに何か問題があると私に思わせます

4

1 に答える 1

1

あなたのエラーは、数字に引用符を使用している可能性があると思います。パラメータを囲む引用符を削除します。

Completed > '%d'したがって、使用する代わりにCompleted > %d(そしてもちろん他のパラメーターも)

引用符を使用すると、おそらく文字列が数値に変換され、予期しない結果が得られる理由が説明されます。

于 2013-02-24T17:08:40.923 に答える