objective-c - 使用 FMDB 保存到数据库在解释 NSInteger 时崩溃

标签 objective-c xcode sqlite fmdb

调用以下函数时,我遇到了 EXC_BAD_ACCESS 崩溃。看起来 FMDB 在解释 subject_id NSInteger 时遇到问题,因为它在 WHERE 子句中命中此 subject_id 列时通过两个 NString 和炸弹。

- (void) saveAllData {

if(isDirty) {

    DrillDownAppAppDelegate *appDelegate = (DrillDownAppAppDelegate *)[[UIApplication sharedApplication] delegate];
    FMDatabase *database = [FMDatabase databaseWithPath:appDelegate.getDBPath];    

    if ([database open]) {

        [database executeUpdate:@"update Subject Set subject = ?, category = ? where subject_id = ?", 
        self.title, self.category_title, self.subject_id];

        [database close];
    }

    isDirty = NO;
}

//Reclaim all memory here.
[title release];
title = nil;
[category_title release];
category_title = nil;

}

问题与我在另一篇关于 FMDB 插入问题的帖子中遇到的问题相同,归结为我的 subject_id 成员有问题。我相信我在 header 中使用了错误的声明。在这里:

//
//  Subject.h
//  DrillDownApp

    #import <UIKit/UIKit.h>

    @interface Subject : NSObject {
        NSInteger subject_id;
        NSString *category_title;
        NSString *title;
    //    NSMutableArray *quotes;
        BOOL isDirty;
        //  BOOL isDetailViewHydrated;

    }

- (id) initWithPrimaryKey:(NSInteger)pk;
    @property (nonatomic, readwrite) BOOL isDirty;
    //@property (nonatomic, readwrite) BOOL isDetailViewHydrated;
- (void) addSubject;
- (NSInteger)getNextSubjectId;

    @property (nonatomic, assign) NSInteger subject_id;
    @property (nonatomic, copy) NSString * title;
    @property (nonatomic, copy) NSString * category_title;
    //@property (nonatomic, retain) NSMutableArray *quotes;
    //- (void) saveAllData;


    @end

(注意:我主要编辑了这个,因为我想出了它的其余部分。)

最佳答案

好的,我解决了。 FMDB 将无法使用整数。您必须将它们转换为数字。我在 FMDB doc 上的示例中发现了这一点并且永远不会通过 executeUpdate 语句传递 int。

所以在我上面的例子中,我修复这个问题的方法如下:

[database executeUpdate:@"update Subject Set subject = ?, category = ? where subject_id = ?", self.title, self.category_title, [NSNumber numberWithInt:self.subject_id]];

我希望这有更好的记录,但是哦,好吧。

关于objective-c - 使用 FMDB 保存到数据库在解释 NSInteger 时崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9029535/

相关文章:

xcode - Xcode "open quickly"和 Textmate "Go to file"使用什么样的搜索算法?

mysql文件导入多行字符串

ios - Arc 语义问题 : No known class method for selector (method name here)

iphone - 这段代码中出现 "Missed Method"的原因是什么?

iphone - 更改 IOS 应用程序上 TabBar 应用程序中的当前屏幕

iphone - 将照片发布到 Facebook

ios - 使用 http ://links in ios 打开你的应用程序

ios - Phonegap SQLite 插件预填充数据库 iOS

c++ - 使用 sqlite 和 c++ 对数据库表进行实际排序的最佳方法?

objective-c - 将数组从一个 View 添加到另一个 View 并保留值