2011-03-29 14 views
0

sqliteに最初のレコードが挿入されました。別のレコードを追加しようとすると「データベースがロックされました」というエラーが表示されます。 コードはsqliteでのデータ挿入の問題

- (void) addRecord:(NSMutableDictionary *)recordDict 
{ 

    if(addStmt == nil) { 
     const char *sql = "insert into Product(ProID, BarCode , ProductName) Values(?,?,?)"; 
     if(sqlite3_prepare_v2(database, sql, -1, &addStmt, NULL) != SQLITE_OK) 
      NSAssert1(0, @"Error while creating add statement. '%s'", sqlite3_errmsg(database)); 
    } 
    NSInteger recordId = [[recordDict objectForKey:@"ProID"] intValue]; 
    NSLog(@"%d",recordId); 
    sqlite3_bind_int(addStmt, 1,recordId); 
    sqlite3_bind_text(addStmt, 2, [[recordDict objectForKey:@"BarCode"] UTF8String], -1, SQLITE_TRANSIENT); 
    sqlite3_bind_text(addStmt, 3, [[recordDict objectForKey:@"ProductName"] UTF8String], -1, SQLITE_TRANSIENT); 


    if(SQLITE_DONE != sqlite3_step(addStmt)) 
     NSAssert1(0, @"Error while inserting data. '%s'", sqlite3_errmsg(database)); 
    else 
    {//SQLite provides a method to get the last primary key inserted by using sqlite3_last_insert_rowid 
     rowID = sqlite3_last_insert_rowid(database); 
     NSLog(@"last inserted rowId = %d",rowID); 

      //Reset the add statement. 
      sqlite3_reset(addStmt); 
    } 
} 
+0

あなたはあなたのコードをフォーマットでしてください - ここで

-(void) checkAndCreateDatabase{ // Check if the SQL database has already been saved to the users phone, if not then copy it over BOOL success; // Create a FileManager object, we will use this to check the status // of the database and to copy it over if required NSFileManager *fileManager = [NSFileManager defaultManager]; // Check if the database has already been created in the users filesystem success = [fileManager fileExistsAtPath:databasePath]; // If the database already exists then return without doing anything if(success) return; // If not then proceed to copy the database from the application to the users filesystem // Get the path to the database in the application package NSString *databasePathFromApp = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:databaseName]; // Copy the database from the package to the users filesystem [fileManager copyItemAtPath:databasePathFromApp toPath:databasePath error:nil]; [fileManager release]; } 

は、データベースのパスを取得する方法ですか? –

+0

objective c xcodeを使用する – Anand

答えて

0

あなたのコードには何も表示されません。 「Database Locked」を取得している場合は、データベースをロックしている開いているトランザクションがあることを意味します。これはあなたのアプリ(おそらく別のスレッド)や同じデータベースにアクセスしている別のアプリにある可能性があります。あなたは以前にコンパイル文をfinlalizeするのを忘れたり、データベースがあなたの前のクエリでこれを試す閉じるのを忘れて)

1 - -

0

「データベースロックは」、データベースが書き込み可能ではありませんこれには2つの理由を意味

sqlite3_finalize(compiledStatement); 
sqlite3_close(database); 

2)使用する前に、ドキュメントディレクトリにデータベースをコピーしていますか?バンドル自体のデータベースは変更できません。ここでは、データベースをドキュメントディレクトリにコピーするコードを示します。

NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *documentsDir = [documentPaths objectAtIndex:0]; 
    databasePath = [[documentsDir stringByAppendingPathComponent:databaseName] retain]; 
    NSLog(@"%@",databasePath);