Search code examples
iphoneobjective-cfmdb

FMDB EXC_BAD_ACCESS when inserts to database


Im getting this error when im trying to insert something in my database:

fmdb error The strange thing is, that the error only occurs when there is about 12-14 previus records in the table that im inserting to. Anyone that knows how to solve this?

Here is my code:

 - (void)addToBasket:(id)sender {
   NSArray *sysPaths = NSSearchPathForDirectoriesInDomains( NSDocumentDirectory, NSUserDomainMask, YES );
   NSString *docDirectory = [sysPaths objectAtIndex:0];
   NSString *filePath = [NSString stringWithFormat:@"%@/MainDatabase.db", docDirectory];

   databasePath = filePath;

   db = [FMDatabase databaseWithPath:databasePath];
   if (![db open]) { [db release]; return; }

   NSInteger prodID = ((UIControl*)sender).tag;

   [db executeUpdate:@"INSERT INTO order_items VALUES (?, ?, ?, ?)", @"", [NSNumber numberWithInt:prodID], orderID, [NSNumber numberWithInt:1], nil];

   [basket insertObject:[NSNumber numberWithInt:prodID] atIndex:0];
   [basketQA insertObject:[NSNumber numberWithInt:1] atIndex:0];
   [basketItemName insertObject:@"test" atIndex:0];
   [basketPrice insertObject:@"test" atIndex:0];

   NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
   [self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];

   [db close];
}

Solution

  • What type is orderID? Where is it coming from? Are you sure it's not a dangling pointer? You are only supposed to pass objects into executeUpdate:.

    Also, you are over-releasing db. Unless a method has new, alloc, retain, or copy in the name, any object returned is autoreleased. You don't have to release it yourself.