ebsp
ebsp

Reputation: 183

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];
}

Upvotes: 3

Views: 2056

Answers (3)

ccgus
ccgus

Reputation: 2916

Turn on zombies, see what happens.

Upvotes: 0

trojanfoe
trojanfoe

Reputation: 122391

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]

Upvotes: 2

Jim
Jim

Reputation: 73936

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.

Upvotes: 3

Related Questions