【发布时间】:2012-03-23 17:24:27
【问题描述】:
我试图在最初没有设置自动增量并且已经有数百条记录的 SQLite 数据库中获得下一个最高 PK。所以我做了下面的方法,它看起来应该可以工作,但我无法从中得到任何结果。我还有其他几种方法可以从数据库中获取有效的数据,但无法弄清楚我做错了什么,而且我会睁一只眼闭一只眼,所以我想我会寻求帮助。
- (NSInteger)getNextSubjectId {
DrillDownAppAppDelegate *appDelegate = (DrillDownAppAppDelegate *)[[UIApplication sharedApplication] delegate];
FMDatabase *database = [FMDatabase databaseWithPath:appDelegate.getDBPath];
[database open];
FMResultSet *rst = [database executeQuery:@"select max(subject_id) + 1 AS SUBJECT_ID from SUBJECT"];
NSLog(@"PRINT FIRST SUBJECT_ID = %@", [rst stringForColumnIndex:0]);
NSString *nextSubId = [rst stringForColumnIndex:0];
[database close];
NSLog(@"NEW SUBJECT_ID = %@", nextSubId);
NSInteger myInt = [nextSubId intValue];
return myInt;
// [maxSubjectId release];
}
日志显示:
2012-01-25 22:56:29.398 报价[6992:f803] 打印第一个主题 ID = (null) (gdb)
标题如下所示:
// Subject.h
// DrillDownApp
#import <UIKit/UIKit.h>
@interface Subject : NSObject {
NSInteger subject_id;
NSString *category_title;
NSString *title;
BOOL isDirty;
}
- (id) initWithPrimaryKey:(NSInteger)pk;
- (void) addSubject;
- (NSInteger)getNextSubjectId;
@property (nonatomic, readwrite) BOOL isDirty;
@property (nonatomic, readonly) NSInteger subject_id;
@property (nonatomic, retain) NSString * title;
@property (nonatomic, retain) NSString * category_title;
@end
【问题讨论】: