0

我在 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;
}
4

1 回答 1

0

我认为问题在于 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;
}
于 2012-07-26T17:53:24.400 回答