下面是在源碼數據庫提取數據的代碼:的SQLite數據庫崩潰
-(id) init {
if ((self = [super init])) {
NSString *sqLiteDb = [[NSBundle mainBundle]pathForResource:@"CourseFindr" ofType:@"sqlite"];
if (sqlite3_open([sqLiteDb UTF8String], &_db) != SQLITE_OK){
NSLog(@"Failed to open database!");
}
}
return self;
}
-(void)dealloc {
sqlite3_close(_db);
}
- (NSArray *)job {
NSMutableArray *retrieve = [[NSMutableArray alloc] init];
NSString *query = @"SELECT jID, jName, jDesc, jEarnings, jTags FROM jobs";
sqlite3_stmt *statement;
if (sqlite3_prepare_v2(_db, [query UTF8String], -1, &statement, nil) == SQLITE_OK) {
while (sqlite3_step(statement) == SQLITE_ROW) {
int _jID = sqlite3_column_int(statement, 0);
char *jNameChars = (char *) sqlite3_column_text(statement,1);
char *jDescChars = (char *) sqlite3_column_text(statement, 2);
char *jEarningsChars = (char *) sqlite3_column_text (statement, 3);
char *jTagsChars = (char *) sqlite3_column_text(statement, 4);
NSString *_jName =[[NSString alloc]initWithUTF8String:jNameChars];
NSString *_jDesc = [[NSString alloc]initWithUTF8String:jDescChars];
NSString *_jEarnings = [[NSString alloc]initWithUTF8String:jEarningsChars];
NSString *_jTags = [[NSString alloc]initWithUTF8String:jTagsChars];
Jobs *jobs = [[Jobs alloc]
initWithJID:_jID
jName:_jName
jDesc:_jDesc
jEarnings:_jEarnings
jTags:_jTags];
[retrieve addObject:jobs];
}
sqlite3_finalize(statement);
}
return retrieve;
}
但它一直得到這個崩潰:
2014-01-09 21:12:39.565 CourseFindr[4595:a0b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[NSPlaceholderString initWithUTF8String:]: NULL cString'
*** First throw call stack:
(
0 CoreFoundation 0x018255e4 __exceptionPreprocess + 180
1 libobjc.A.dylib 0x015a88b6 objc_exception_throw + 44
2 CoreFoundation 0x018253bb +[NSException raise:format:] + 139
3 Foundation 0x0120fb49 -[NSString initWithUTF8String:] + 89
4 CourseFindr 0x000036b5 -[CourseFindrDB jobs] + 645
5 libdyld.dylib 0x01e61725 start + 0
6 ??? 0x00000001 0x0 + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb)
我不知道這是否是因爲我libsqlite3的。 dylib還是與我的數據庫不兼容? 我的數據庫被命名爲「CourseFindr.sqlite」表是
JOBS Table
-jID PK
-jName
-jDesc
-jEarnings
-jTags
而且它不斷重定向時崩潰來的main.m。
我以前做過這個,但沒有錯誤。這一次,我嘗試沒有教程,但似乎我錯過了一些東西。 :/
好的,我會試試。但我可以在sqlite管理器中將它們設置爲不是NULL嗎? – hazelvan
請參閱我的編輯。 – sergio
@sergio OP正在訪問0-4列,而不是1-4。再看看問題中的代碼。 – rmaddy