2012-03-19 12 views
1

機能していません:ログで私のUPDATE文は、私はこのように私のテーブルのエントリ更新しようとした

NSFileManager *fileMgr=[NSFileManager defaultManager]; 
     NSString *dbPath=[[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"quizDB.sqlite"]; 

     if ([fileMgr fileExistsAtPath:dbPath]) { 
      NSLog(@"DB does exist!");//displayed 
     } 
     const char *dbpath = [dbPath UTF8String]; 
     sqlite3_stmt *updateStmt; 
if (sqlite3_open(dbpath, &contactDB) == SQLITE_OK) 
     { 
      NSLog(@"%i",id_quiz);//displayed, in a test case it display 1 which is a quiz ID in the table 
      NSString *querySql=[NSString stringWithFormat:@"Update quiz set etat=1 where id=\"%i\"",id_quiz]; 
      const char*sql=[querySql UTF8String]; 

      sqlite3_prepare_v2(contactDB, sql, -1, &updateStmt, NULL); 

       if(SQLITE_DONE != sqlite3_step(updateStmt)) 
       { 
        NSAssert1(0, @"Error while updating. '%s'", sqlite3_errmsg(contactDB)); 
       } 
       else{ 

        sqlite3_reset(updateStmt); 
        NSLog(@"Update done successfully!");//this is also displayed, i guess everything is ok then and the entry is updated 
       } 

      sqlite3_finalize(updateStmt);   
      sqlite3_close(contactDB); 
     } 

を、私はメッセージを得た:Update done successfully!はので、私はすべてがあると仮定し、私は確認してくださいしかし、ときMozilla FirefoxのSQLite Manager内のデータベースでは、エントリは更新されません。何か不足していますか?

- (void) checkAndCreateDatabase 
{ 
NSString *database_Name [email protected]"quizDB.sqlite" 
NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
NSString *documentsDir = [documentPaths objectAtIndex:0]; 
NSString *database_Path = [documentsDir stringByAppendingPathComponent:database_Name]; 

NSFileManager *fileManager = [NSFileManager defaultManager]; 

if(![fileManager fileExistsAtPath:database_Path]) 
{ 
    NSString *databasePathFromApp = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:database_Name]; 

    [fileManager removeItemAtPath:database_Path error:nil]; 
    [fileManager copyItemAtPath:databasePathFromApp toPath:database_Path error:nil]; 
} 
else 
{ 
    NSString *databasePathFromApp = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:database_Name]; 
    [fileManager copyItemAtPath:databasePathFromApp toPath:database_Path error:nil]; 
} 

} 

、あなたのデータベースを更新した後:DIR文書化するリソースからの助け

答えて

2

最初のコピー、データベースのありがとう

- (void) UpdateDatabase 
{  
sqlite3 *contactDB; 
sqlite3_stmt *updateStmt; 
NSString *database_Name [email protected]"quizDB.sqlite" 

NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
NSString *documentsDir = [documentPaths objectAtIndex:0]; 
NSString *dbpath = [documentsDir stringByAppendingPathComponent:database_Name]; 

if(sqlite3_open([dbpath UTF8String], &contactDB) == SQLITE_OK) 
    { 
    NSString *querySql=[NSString stringWithFormat:@"Update quiz set etat=1 where id=\"%i\"",id_quiz]; 
    const char*sql=[querySql UTF8String]; 
    if(sqlite3_prepare_v2(contactDB,sql, -1, &updateStmt, NULL) == SQLITE_OK) 
    { 
     if(SQLITE_DONE != sqlite3_step(updateStmt)) 
      { 
        NSAssert1(0, @"Error while updating. '%s'", sqlite3_errmsg(contactDB)); 
      } 
      else{ 
       sqlite3_reset(updateStmt); 
       NSLog(@"Update done successfully!"); 
       } 
     } 



    sqlite3_finalize(updateStmt); 
} 
sqlite3_close(contactDB); 

} 

をし、リソースにdocumentdirではない保存データベースをチェックし

+0

こんにちはPiyush Patel:いつどこで 'checkAndCreateDatabase'が呼び出されるべきですか? – Luca

+0

これまで行ってきたことは、 'checkAndCreateDatabase'メソッドを' AppDelegate'ファイルに置き、 'didFinishLaunchingWithOptions'メソッドで呼びましたが、うまくいかなかったということです。私のSQLite DBファイルは、 'Supporting Files'ディレクトリにあります。'あなたのデータベースをチェックして、どのドキュメントディレクトリに保存されていないのかを確認するにはどうすればいいでしょうか? – Luca

+0

それは動作しません。 'あなたのデータベースをチェックして、リソースにないdocumentdirに保存してください'と言ったときに理解できませんでした---私のSQLite DBファイルは現在Xcodeの 'Supporting Files'ディレクトリにありますか? – Luca

関連する問題