2013-04-09 1 views
3

다음 코드는 오류로 호출되는 라이브러리 루틴을 제공하지만 오류는 어디에서 설명 할 수 있습니까? 어떤 아이디어?SQLite3 라이브러리 루틴이 시퀀스 외 호출되었습니다

- (BOOL)insertProduct:(Product *)product inOrder:(Order *)order withAmount:(int)amount 
    { 
     BOOL ok = NO; 
     sqlite3_stmt *statement; 

     const char *dbpath = [_databasePath UTF8String]; 

     if (sqlite3_open(dbpath, &_database) == SQLITE_OK) 
     { 
      NSString * insertSQL; 
      int amount = [self getAmountForProduct:product inOrder:order]; 
      NSLog(@"%i", amount); 

      if (amount != -1) 
      { 
       insertSQL = [NSString stringWithFormat: @"UPDATE ARTICOLIPERORDINE SET quantita = %i WHERE ordine = %i AND articolo = '%@'", amount, order.idOrdine, product.codice]; 
      } 
      else 
      { 
       insertSQL = [NSString stringWithFormat: @"INSERT INTO ARTICOLIPERORDINE (ordine, articolo, quantita) VALUES(%i, '%@', %i)",order.idOrdine, product.codice, 1]; 
      } 

      NSLog(@"%@", insertSQL); 
      const char *insert_stmt = [insertSQL UTF8String]; 

      if (sqlite3_prepare_v2(_database, insert_stmt, -1, &statement, NULL) == SQLITE_OK) 
      { 
       if (sqlite3_step(statement) == SQLITE_DONE) 
       { 
        ok = YES; 
       } 
       else 
       { 
        ok = NO; 
        NSLog(@"sqlite3 ERROR %s: %@",sqlite3_errmsg(_database), insertSQL); 
       } 

       sqlite3_finalize(statement); 
       sqlite3_close(_database); 
      } 
      else 
      { 
       NSLog(@"Error prepare = %s", sqlite3_errmsg(_database)); 
      } 

     } 

     return ok; 
    } 

로그 인쇄 오류가 나는 한 가지 방법을 순서

+0

'sqlite3_prepare_v2'를 (를) 호출하여 얻은 반환 값은 무엇입니까? – gaige

+0

a BOOL ... question updated updated – Fry

+2

실제로'sqlite3_prepare_v2'는 문서에 따라 성공하면'SQLITE_OK'를 반환하므로 위의 코드를 실행하면 실패하게됩니다. 먼저 step on을 실패라고 선언하고 ('SQLITE_OK'가 0), 두 번째로, 준비된 명령문에서'sqlite3_finalize'를 호출합니다. – gaige

답변

1

에서 호출 = 라이브러리 루틴을 준비합니다. , 코드에 실제 쿼리를 실행하는 데이터베이스 브라우저에 해당 쿼리를 실행하기 전에 이 전에 가까운, 열린 연결 등의 모든 SQL 쿼리를 완료했는지 확인하십시오, 당신은 무엇을 볼 수 등

을 마무리 귀하의 질문에 누락되었습니다.

필자의 경우 필자는 잘못하여 열 이름을 쿼리에 넣고 코드를 실행하면 오류가 여러 번 발생합니다. 나는 모든 코드를 신중하게 검토하고 쿼리를 데이터베이스 브라우저로 실행하고 실수를 발견했다.

관련 문제