【问题标题】:FMDB EXC_BAD_ACCESS when inserts to database插入数据库时​​的 FMDB EXC_BAD_ACCESS
【发布时间】:2012-05-18 03:10:30
【问题描述】:

当我尝试在我的数据库中插入某些内容时出现此错误:

奇怪的是,只有当我插入的表中有大约 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];
}

【问题讨论】:

    标签: iphone objective-c fmdb


    【解决方案1】:

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

    另外,你过度释放db。除非方法名称中包含 newallocretaincopy,否则返回的任何对象都会自动释放。你不必自己释放它。

    【讨论】:

    • orderID 的类型是 NSNumber,但正如您所说,我过度发布了我认为导致错误的 db。删除if (![db open]) { [db release]; return; } 后,它终于可以工作了。谢谢!
    • 我遇到了同样的错误消息,但我无法捕捉到任何与zombie 相关的错误。那是因为我不知道我应该只传递对象!非常感谢。
    【解决方案2】:

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

    [NSNumber numberWithInt:orderID]
    

    【讨论】:

      【解决方案3】:

      打开zombies,看看会发生什么。

      【讨论】:

        猜你喜欢
        • 2012-04-23
        • 1970-01-01
        • 1970-01-01
        • 2018-04-06
        • 2013-04-15
        • 1970-01-01
        • 1970-01-01
        • 2014-01-19
        • 2015-06-29
        相关资源
        最近更新 更多