3

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];
}
Paul R
  • 208,748
  • 37
  • 389
  • 560
ebsp
  • 183
  • 1
  • 2
  • 8

3 Answers3

3

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.

Jim
  • 72,985
  • 14
  • 101
  • 108
  • The type of the orderID is a NSNumber, but as you said, i over-released the `db` which i think caused the errors. It is finally working now after removing `if (![db open]) { [db release]; return; }`. Thank you! – ebsp May 09 '12 at 15:41
  • 1
    I ran into the same error message and I couldn't catch any error with zombie and all that. And it was because I didn't know I am suppose to pass only objects! Thank you so much. – huggie Jul 17 '12 at 01:13
2

You don't show where orderID is defined, but if it's not an object (derived from NSObject) then that will break in the code you have shown. If it's a primitive integer type then you need to wrap it in an NSNumber object as you have done with the other values:

[NSNumber numberWithInt:orderID]
trojanfoe
  • 120,358
  • 21
  • 212
  • 242
0

Turn on zombies, see what happens.

Community
  • 1
  • 1
ccgus
  • 2,906
  • 23
  • 18