0
我在sqlite3_prepare之後立即得到EXC_BAD_ACCESS錯誤。任何想法將不勝感激?請注意,我正在使用4個sql語句循環訪問數據庫。第一次迭代總是正常工作,第二次迭代拋出錯誤。SQLite EXC_BAD_ACCESS
更新:第一條SQL語句的工作原理是因爲它沒有返回任何值。
-(NSMutableArray *) categoryList{
categories = [NSMutableArray array];
const char *sql;
for (int i = 0; i < 4; i++){
@try {
NSFileManager *fileMgr = [NSFileManager defaultManager];
NSString *dbPath = [[[NSBundle mainBundle] resourcePath ]stringByAppendingPathComponent:@"webdemo.db"];
BOOL success = [fileMgr fileExistsAtPath:dbPath];
if(!success)
{
NSLog(@"Cannot locate database file '%@'.", dbPath);
}
if(!(sqlite3_open([dbPath UTF8String], &db) == SQLITE_OK))
{
NSLog(@"An error has occured: %@", sqlite3_errmsg(db));
}
//queries for category captions and category images
//get images from category attach first
//take high res images first
switch (i){
case 0: //high res image in category attach
sql = "SELECT DISTINCT a.CategoryCaption1, b.LocationHigh FROM CategoryAdvance a JOIN CategoryAttach b ON a.CategoryText1 = b.CategoryText1 WHERE b.LocationHigh IS NOT NULL";
break;
case 1: //low res image in category attach
sql = "SELECT DISTINCT a.CategoryCaption1, b.Location FROM CategoryAdvance a JOIN CategoryAttach b ON a.CategoryText1 = b.CategoryText1 WHERE b.LocationHigh IS NULL AND b.Location IS NOT NULL";
break;
case 2: //high res image in category advance
sql = "SELECT DISTINCT CategoryCaption1, CategoryHIRESPicPath FROM CategoryAdvance where CategoryText1 NOT IN (SELECT CategoryText1 from CategoryAttach where LocationHigh IS NOT NULL or Location IS NOT NULL) AND CategoryHIRESPicPath IS NOT NULL";
break;
case 3: //low res image in category advance
sql = "SELECT DISTINCT CategoryCaption1, CategoryPicPath FROM CategoryAdvance where CategoryText1 NOT IN (SELECT CategoryText1 from CategoryAttach where LocationHigh IS NOT NULL or Location IS NOT NULL) AND CategoryHIRESPicPath IS NOT NULL";
break;
}
sqlite3_stmt *sqlStatement;
if(sqlite3_prepare(db, sql, -1, &sqlStatement, NULL) != SQLITE_OK)
{
NSLog(@"Problem with prepare statement: %@", sqlite3_errmsg(db));
}else{
while (sqlite3_step(sqlStatement)==SQLITE_ROW) {
Category *category = [[Category alloc] init];
category.caption = [NSString stringWithUTF8String:(char *) sqlite3_column_text(sqlStatement,0)];
category.imageName = @"AppIcon-retina.png"; //[NSString stringWithUTF8String:(char *) sqlite3_column_text(sqlStatement,1)];
[category parseImageName];
[categories addObject:category];
}
}
sqlite3_finalize(sqlStatement);
}
@catch (NSException *exception) {
NSLog(@"Problem with prepare statement: %@", sqlite3_errmsg(db));
}
@finally {
//sqlite3_finalize(sqlStatement);
sqlite3_close(db);
}
}
//delete duplicate categories
//order categories alphabetically
return categories;
}
一方面sqlite3_errmsg不*不*返回一個Objective-C的對象,所以你不能用'%@把它傳遞給NSLog的',它應該是'%s' – borrrden 2012-07-26 01:19:06