title: SQLite EXC_BAD_ACCESS
body: <p>I am getting the EXC_BAD_ACCESS error right after sqlite3_prepare. Any ideas would be greatly appreciated? Note that I am looping through the database with 4 sql statements.The first iteration always works fine and the second iteration throws the error.</p>
  
  <p>Update: The first SQL statement works because it is returning no values.</p>
  
  <pre><code>-(NSMutableArray *) categoryList{
      categories = [NSMutableArray array];
      const char *sql;
  
      for (int i = 0; i &lt; 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], &amp;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, &amp;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;
  }
  </code></pre>