2013-02-24 2 views
1

리더 보드 시스템에 대해이 쿼리를 작성하고 있습니다. 대상 점수와 관련하여 그 사람에게 가장 가까운 다음 대상을 반환해야합니다. 몇 가지 이유로 내 SQL 쿼리가 작동하지 않습니다.어떤 이유로 든 SQL 쿼리가 작동하지 않습니다.

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

completecount =이 사람이 목표 stickercount을 완료 한 시간의 양 =이 사람이이 스티커의 양 :

이 내 쿼리입니다. 둘 다 int입니다 그래서 나는 그 사람보다 더 많은 사람을 완료했거나 같은 시간 동안 그리고 그들보다 더 많은 스티커를 가지고있는 사람들을 돌려 주길 원합니다.

데이터베이스 레이아웃 :

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 

확인이를, 그도 여기에 결과^자신을 보여주는. 스튜어트보다 더 많은 스티커가 아닌 에밀리를 보여 주기만하는 것이 아니라 에밀리가 여전히 검색된다는 것입니다. 여기서 뭐하는거야? 완전히 WHERE 절 분리

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 

:

NSString *sql = [NSString stringWithFormat:@"SELECT * FROM Children WHERE Completed > %d",completecount]; 

결과 EGE AKPINAR 의해

요청 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 

스틱 카운트가 잘못되었다고 생각합니다.

답변

1

숫자에 인용문을 사용하고 계신 것 같습니다. 매개 변수 주위에 따옴표를 제거하십시오.당신이 예상치 못한 결과가 이유를 설명 할 수

따라서 대신 Completed > '%d' 사용 Completed > %d 인용 부호를 사용하는 경우 (물론 다른 PARAMS도가)

, 그것은 아마도 숫자로 문자열을 변환합니다.

+0

의견을 보내 주셔서 감사합니다. 이 코드를 다음과 같이 변경했습니다 : SELECT * FROM Children Completed> % d OR (완료 % d AND '현재 스티커'> % d) - 불행히도 차이가 없습니다 :( – user1816481

+0

실감하지 못 했으므로 sqlite3_column_int 귀하의 int 매개 변수에 대해. sqlite3_column_text를 사용하여, 내가 생각하는 문자열로 그들을 강요합니다. –

+0

그래, 맞아. 지금은 그들을 바꿀거야,하지만 오류가 지속될거야. – user1816481

관련 문제