2012-04-18 13 views
2

私はsqlite3_finalizeについて完全に理解していません。私の初期コード。私はsqlite3_finalizeを完全に理解していません

while (j < Autors.count) 
{ 
if(sqlite3_prepare_v2(database, sqlStatement, -1, &compiledStatement, NULL) == SQLITE_OK) 
{ 
    sqlite3_bind_int(compiledStatement, 1, ((AuthorSettings *)[Autors objectAtIndex:j]).authorId); 
    if(sqlite3_step(compiledStatement) != SQLITE_DONE) 
    {  
     NSLog(@"sqlite3_step error %s", sqlite3_errmsg(database)); 
     sqlite3_busy_timeout(database, 5); 
    } 
    else 
    {  
     if(sqlite3_prepare_v2(database, sqlStatement_1, -1, &compiledStatement, NULL) == SQLITE_OK) 
     { 
      sqlite3_bind_int(compiledStatement, 1, bookId); 
      if(sqlite3_step(compiledStatement) != SQLITE_DONE) 
      {  
       NSLog(@"sqlite3_step error %s", sqlite3_errmsg(database)); 
       sqlite3_busy_timeout(database, 5); 
      }  
      else j++;  
     } 
     else NSLog(@"sqlite3_prepare_v2 error %s", sqlite3_errmsg(database)); 
    }  
} 
} 
sqlite3_finalize(compiledStatement); 

sqlite3_finalize関数を追加しました。私を確認してください。

while (j < Autors.count) 
{ 
sqlite3_finalize(compiledStatement); //**added 
if(sqlite3_prepare_v2(database, sqlStatement, -1, &compiledStatement, NULL) == SQLITE_OK) 
{ 
    sqlite3_bind_int(compiledStatement, 1, ((AuthorSettings *)[Autors objectAtIndex:j]).authorId); 
    if(sqlite3_step(compiledStatement) != SQLITE_DONE) 
    {  
     NSLog(@"sqlite3_step error %s", sqlite3_errmsg(database)); 
     sqlite3_busy_timeout(database, 5); 
    } 
    else 
    { 
     sqlite3_finalize(compiledStatement); //**added 
     if(sqlite3_prepare_v2(database, sqlStatement_1, -1, &compiledStatement, NULL) == SQLITE_OK) 
     { 
      sqlite3_bind_int(compiledStatement, 1, bookId); 
      if(sqlite3_step(compiledStatement) != SQLITE_DONE) 
      {  
       NSLog(@"sqlite3_step error %s", sqlite3_errmsg(database)); 
       sqlite3_busy_timeout(database, 5); 
      }  
      else j++;  
     } 
     else NSLog(@"sqlite3_prepare_v2 error %s", sqlite3_errmsg(database)); 
    }  
} 
} 
sqlite3_finalize(compiledStatement); 

答えて

6

sqlite3_finalizeは、sqlite3_prepare_v2の逆です。したがって、コードは次のようになります。

sqlite3_prepare_v2(...); 
while() { 
    sqlite3_reset(...); 
    sqlite3_bind_int(...); 
    sqlite3_step(...); 
} 
sqlite3_finalize(...); 

ステートメントを必要以上に準備する必要はありません。

+0

sqlite3_execを使用している場合は、sqlite3_finalizeも使用する必要がありますか? – ademar111190

+1

@ ademar111190ドキュメントに「sqlite3_finalize()関数がプリペアドステートメントを削除するために呼び出されました。したがって、文章を準備しないと、文章を完成させる必要はありません。私はsqlite3_finalizeとsqlite3_execを呼び出すと、時々私はランタイムエラーが発生します。 –

+0

ありがとう、ちょうどそうです。 – ademar111190

関連する問題