3

当我尝试在我的数据库中插入一些东西时,我收到了这个错误:

fmdb 错误 奇怪的是,只有当我插入的表中有大约 12-14 条以前的记录时才会发生错误。有谁知道如何解决这个问题?

这是我的代码:

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

3 回答 3

3

什么类型orderID?它来自哪里?你确定它不是一个悬空指针?您只应该将对象传递到executeUpdate:.

此外,您过度释放db. 除非方法名称中有newallocretaincopy,否则返回的任何对象都是自动释放的。你不必自己释放它。

于 2012-05-09T15:23:33.460 回答
2

您没有显示orderID定义的位置,但如果它不是一个对象(派生自NSObject),那么这将破坏您显示的代码。如果它是原始整数类型,那么您需要将它包装在一个NSNumber对象中,就像您对其他值所做的那样:

[NSNumber numberWithInt:orderID]
于 2012-05-09T15:25:26.387 回答
0

打开僵尸,看看会发生什么。

于 2012-05-09T23:37:10.980 回答