[英]SQLite EXC_BAD_ACCESS
我在sqlite3_prepare之后收到EXC_BAD_ACCESS錯誤。 任何想法將不勝感激? 請注意,我正在使用4條sql語句遍歷數據庫。第一次迭代始終可以正常工作,第二次迭代會引發錯誤。
更新:第一條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;
}
我認為問題在於finalize語句,在您的代碼中,如果未准備好語句,那么它也會調用finalize方法。
檢查重寫的代碼:
-(NSMutableArray *) categoryList
{
categories = [NSMutableArray array];
const char *sql;
@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 occurred");
}
for (int i = 0; i < 4; i++)
{
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");
}
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");
}
@finally
{
sqlite3_close(db);
}
return categories;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.