當下面的函數被調用,我得到EXC_BAD_ACCESS崩潰。看起來FMDB在解析subject_id NSInteger時遇到了問題,因爲當它擊中WHERE子句中的這個subject_id列時,它會通過兩個NString和炸彈。保存到數據庫使用FMDB崩潰解釋NSInteger
- (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成員有些不妥。我相信我在標題中使用了錯誤的聲明。那就是:
//
// 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
(注:我編輯這majorly,我想通了,它的其餘部分。)
您正在將標題值和類別標題值設置爲主題對象。但是,您將對象保存到數據庫的位置? – Ilanchezhian 2012-01-27 07:40:19
我將其更新爲包含將對象保存到數據庫的saveAllData函數。但問題是,它甚至沒有使數據保留在對象中。我認爲我的問題是我沒有正確調用setTitle和setCategory_Title函數,因爲在調試器中運行時,這些方法永遠不會被觸及... – jroyce 2012-01-27 18:44:36