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

jro*_*yce 3 sqlite xcode objective-c fmdb

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

- (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;

}
Run Code Online (Sandbox Code Playgroud)

问题与我在FMDB插入问题的另一篇文章中遇到的问题是一样的,这归结为我的subject_id成员有问题.我相信我在标题中使用了错误的声明.这里是:

//
//  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
Run Code Online (Sandbox Code Playgroud)

(注意:我主要编辑了这个,因为我弄清楚了其余部分.)

jro*_*yce 8

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

所以在我上面的示例中,我修复此问题的方法如下:

[database executeUpdate:@"update Subject Set subject = ?, category = ? where subject_id = ?", self.title, self.category_title, [NSNumber numberWithInt:self.subject_id]];
Run Code Online (Sandbox Code Playgroud)

我希望这可以更好地记录下来,但是哦.