2011-01-03 5 views
0

私のココアアプリケーションでは、リソースフォルダ内にSQLiteデータベースを保持しています。選択操作をしようとしましたが、しばらくしてからデータベースがロックされています。次のように私が選択削除操作のために使用していますSqlite3: "データベースがロックされています"エラー

方法は次のとおりです://

if (sqlite3_open([databasePath UTF8String], &database) != SQLITE_OK) 
     { 
      sqlite3_close(database); 
      NSAssert(0, @"Failed to open database"); 
     } 

     NSLog(@"mailBodyFor:%d andFlag:%d andFlag:%@",UId,x,Ffolder); 
     NSMutableArray *recordsToReturn = [[NSMutableArray alloc] initWithCapacity:2]; 
     NSString *tempMsg; 


     const char *sqlStatementNew; 
     NSLog(@"before switch"); 
     switch (x) { 
      case 9: 
      // tempMsg=[NSString stringWithFormat:@"SELECT * FROM users_messages"]; 
       tempMsg=[NSString stringWithFormat:@"SELECT message,AttachFileOriName as oriFileName,AttachmentFileName as fileName FROM users_messages WHERE id = (select message_id from users_messages_status where id= '%d')",UId]; 
       NSLog(@"mail body query - %@",tempMsg); 
       break; 
      default: 
       break; 
     } 
     sqlStatementNew = [tempMsg cStringUsingEncoding:NSUTF8StringEncoding]; 
     sqlite3_stmt *compiledStatementNew; 

     NSLog(@"before if statement"); 
     if(sqlite3_prepare_v2(database, sqlStatementNew, -1, &compiledStatementNew, NULL) == SQLITE_OK) { 
      NSLog(@"the sql is finalized"); 
      while(sqlite3_step(compiledStatementNew) == SQLITE_ROW) { 
       NSMutableDictionary *recordDict = [[NSMutableDictionary alloc] initWithCapacity:3]; 

       NSString *message; 
       if((char *)sqlite3_column_text(compiledStatementNew, 0)){ 
        message = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatementNew, 0)]; 
       } 
       else{ 
        message = @""; 
       } 
       NSLog(@"message - %@",message); 

       NSString *oriFileName; 
       if((char *)sqlite3_column_text(compiledStatementNew, 1)){ 
        oriFileName = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatementNew, 1)]; 
       } 
       else{ 
        oriFileName = @""; 
       } 
       NSLog(@"oriFileName - %@",oriFileName); 

       NSString *fileName; 
       if((char *)sqlite3_column_text(compiledStatementNew, 2)){ 
        fileName = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatementNew, 2)]; 
       } 
       else{ 
        fileName = @""; 
       } 
       NSLog(@"fileName - %@",fileName); 

       [recordDict setObject:message forKey:@"message"]; 
       [recordDict setObject:oriFileName forKey:@"oriFileName"]; 
       [recordDict setObject:fileName forKey:@"fileName"]; 

       [recordsToReturn addObject:recordDict]; 
       [recordDict release]; 
      } 

      sqlite3_finalize(compiledStatementNew); 
      sqlite3_close(database); 
      NSLog(@"user messages return -%@",recordsToReturn); 

      return recordsToReturn; 
     } 
     else{ 
      NSLog(@"Error while creating retrieving mailBodyFor in messaging '%s'", sqlite3_errmsg(database)); 
      sqlite3_close(database); 

     } 

if (sqlite3_open([databasePath UTF8String], &database) != SQLITE_OK) 
    { 
     sqlite3_close(database); 
     NSAssert(0, @"Failed to open database"); 
    } 

    NSString *deleteQuery = [[NSString alloc] initWithFormat:@"delete from users_messages_status where id IN(%@)",ids]; 
    NSLog(@"users_messages_status msg deleteQuery - %@",deleteQuery); 

    sqlite3_stmt *deleteStmnt; 
    const char *sql = [deleteQuery cStringUsingEncoding:NSUTF8StringEncoding]; 

    if(sqlite3_prepare_v2(database, sql, -1, &deleteStmnt, NULL) != SQLITE_OK){ 
     NSLog(@"Error while creating delete statement. '%s'", sqlite3_errmsg(database)); 
    } 
    else{ 
     NSLog(@"successful deletion from users_messages"); 
    } 

    if(SQLITE_DONE != sqlite3_step(deleteStmnt)){ 
     NSLog(@"Error while deleting. '%s'", sqlite3_errmsg(database)); 
    } 

    sqlite3_close(database); 

物事があるデータを削除する方法を、データを取得するための

//メソッドこのシーケンスで間違っている

  1. データが取得されます
  2. 削除操作の実行時に「データベースがロックされています」というエラーが発生します。
  3. 最初の手順を実行しようとすると、同じエラーが表示されます。

誰も私を提案することができます:

  1. 私は何も悪いことをやったり、いくつかのチェックをしないのですか?
  2. ロックされたエラーが発生したときにロックを解除する方法はありますか?

おかげで、

Miraaj

+0

[FMDB](http://github.com/ccgus/fmdb)を使用してください。 SQLiteデータベースを無限に扱うことができます。 –

答えて

1

オープン/データベースあなたがアクセスするたびに閉じていることは本当に珍しいです。

ご希望のシナリオであれば、通常のファイルを使用する方がはるかに良いでしょう。

起動時にデータベースを開き、終了時に閉じます。これはすべての問題を解決することができます。

+0

こんにちは9dan ...あなたのためにお返事:)私はあなたが提案したことをしました - アプリケーションの起動時にアプリケーションを起動し、アプリケーションを終了するときにdbを開きますが、今はいくつかのデータを取得しようとしたときに、ライブラリルーチンが順番に呼ばれる '..あなたはそれのためのいくつかのソリューションを提案できますか? – Devarshi

+0

@Miraajまあ、スレッディングの問題は?スレッドの安全性に関するSQLite FAQをチェックしてください:http://www.sqlite.org/faq.html#q6 – 9dan

関連する問題