title: Memory Issue in sqlite database iOS?
body: <p>I have used sqlite database in my app.I am doing CURD operation.I get data from json then i convert that data into arrays after that from arrays i insert the data into sqlite database.In my app when data can be upto more than 1000 records.When database is done then i get <strong>memory warning</strong>.</p>
  
  <p>I have one function for Inserting data.It firstly check if data exists then update data else insert new record</p>
  
  <p><code>[[DBManager getSharedInstance]insertData:object</code>];</p>
  
  <p>Function definition -</p>
  
  <pre><code> (BOOL) insertData:(SubComponent *)subcomponent
  {
  
  
      const char *dbpath = [databasePath UTF8String];
      if([self getSubComponentDataBySubclientClientInspectionid:subcomponent.componentid : subcomponent.componentsubclientid:subcomponent.componentClientid : subcomponent.componentinspectionid] != nil)
      {
  
          [self updateCompSubComponentDataBySubclientId:subcomponent];
          return YES;
      }
      else
      {
          if (sqlite3_open(dbpath, &amp;database) == SQLITE_OK)
          {
  
              if (sqlite3_exec(database, [@"PRAGMA CACHE_SIZE=500000;" UTF8String], NULL, NULL, NULL) == SQLITE_OK) {
                  NSLog(@"Successfully changed cache size");
              }
              else
                  NSLog(@"Error: failed to set cache size with message %s.", sqlite3_errmsg(database));
              NSLog(@"inside isnert query");
              NSString *insertSQL = [NSString stringWithFormat:
                                     @"insert into subcomponent(subcomponentid,name,createddate,subclientid,clientid,cliententityid, inspectionid, initialstatus,componentclientname,componentsubclientname, checkedstatus) values (\"%@\",\"%@\",\"%@\",\"%@\",\"%@\",\"%@\",\"%@\",\"%ld\",\"%@\",\"%@\",\"2\")",subcomponent.componentid,subcomponent.componentName,subcomponent.componentcreateddate,subcomponent.componentsubclientid,subcomponent.componentClientid, subcomponent.componentEntityid,subcomponent.componentinspectionid,(long)subcomponent.componentStatus,subcomponent.componentClientName,subcomponent.componentSubclientName];
              NSLog(@"%@",insertSQL);
              const char *insert_stmt = [insertSQL UTF8String];
              sqlite3_prepare_v2(database, insert_stmt,-1, &amp;statement, NULL);
              if (sqlite3_step(statement) == SQLITE_DONE)
              {
  
                  sqlite3_finalize(statement);
                  return YES;
              }
              else
              {
                  NSLog(@"REMASH DB ERROR %s",sqlite3_errmsg(database));
                 // sqlite3_reset(statement);
                  //sqlite3_close(database);
                  sqlite3_finalize(statement);
                  return NO;
              }
          }
          sqlite3_close(database);
      }
      return YES;
  }
  </code></pre>
  
  <p><strong>GET DATA FUNCTION</strong></p>
  
  <pre><code>- (SubComponent*) getSubComponentDataBySubclientClientInspectionid:(NSString*)componentid : (NSString *)subclientid : (NSString *)clientId : (NSString *)inspectionid
  {
      const char *dbpath = [databasePath UTF8String];
      if (sqlite3_open(dbpath, &amp;database) == SQLITE_OK)
      {
          NSString *querySQL = [NSString stringWithFormat:@"select * from subcomponent where subcomponentid = \"%@\" and subclientid = \"%@\" and clientid = \"%@\" and inspectionid = \"%@\"",componentid,subclientid, clientId,inspectionid];
          NSLog(@"gghgh query is %@",querySQL);
          const char *query_stmt = [querySQL UTF8String];
          SubComponent *resultArray = [[SubComponent alloc]init];
          if (sqlite3_prepare_v2(database,query_stmt, -1, &amp;statement, NULL) == SQLITE_OK)
          {
              if (sqlite3_step(statement) == SQLITE_ROW)
              {
                  NSString *component_id = [[NSString alloc] initWithUTF8String:
                                            (const char *) sqlite3_column_text(statement, 0)];
                  resultArray.componentid = component_id;
                  NSString *name = [[NSString alloc] initWithUTF8String:
                                    (const char *) sqlite3_column_text(statement, 1)];
                  resultArray.componentName = name;
                  NSString *createddate = [[NSString alloc] initWithUTF8String:
                                           (const char *) sqlite3_column_text(statement, 2)];
                  resultArray.componentcreateddate = createddate;
                  NSString *subclientid = [[NSString alloc] initWithUTF8String:
                                           (const char *) sqlite3_column_text(statement, 3)];
                  resultArray.componentsubclientid = subclientid;
                  NSString *clientid = [[NSString alloc] initWithUTF8String:
                                        (const char *) sqlite3_column_text(statement, 4)];
                  resultArray.componentClientid = clientid;
                  NSString *entityid = [[NSString alloc] initWithUTF8String:
                                        (const char *) sqlite3_column_text(statement, 5)];
                  resultArray.componentEntityid = entityid;
                  NSString *inspectionid = [[NSString alloc]initWithUTF8String:(const char *) sqlite3_column_text(statement, 6)];
                  resultArray.componentinspectionid = inspectionid;
                  NSString *clientName = [[NSString alloc]initWithUTF8String:(const char *) sqlite3_column_text(statement, 8)];
                  resultArray.componentClientName = clientName;
                  NSString *subclientName = [[NSString alloc]initWithUTF8String:(const char *) sqlite3_column_text(statement, 9)];
                  resultArray.componentSubclientName = subclientName;
  //                sqlite3_finalize(statement);
  //                sqlite3_close(database);
                  return resultArray;
              }
              else
              {
  
  
  //                sqlite3_finalize(statement);
  //                sqlite3_close(database);
                  return nil;
              }
          }
          else
          {
              NSLog(@"error is %s",sqlite3_errmsg(database));
          }
      }
    //  sqlite3_reset(statement);
     // sqlite3_finalize(statement);
      sqlite3_close(database);
  
      return nil;
  }
  </code></pre>
  
  <p><strong>Update Function</strong></p>
  
  <pre><code>-(void)updateCompSubComponentDataBySubclientId:(SubComponent *)subcomponent
  {
      NSLog(@"inside update data");
      const char *dbpath = [databasePath UTF8String];
      if (sqlite3_open(dbpath, &amp;database) == SQLITE_OK)
      {
  
  
              if (sqlite3_exec(database, [@"PRAGMA CACHE_SIZE=500000;" UTF8String], NULL, NULL, NULL) == SQLITE_OK) {
                  NSLog(@"Successfully changed cache size");
              }
              else
                  NSLog(@"Error: failed to set cache size with message %s.", sqlite3_errmsg(database));
  
  
  
          NSString *querySql=[NSString stringWithFormat:
                              @"UPDATE subcomponent SET name=\"%@\", createddate=\"%@\", cliententityid =\"%@\",componentclientname=\"%@\", componentsubclientname = \"%@\", checkedstatus = \"2\" where subcomponentid = \"%@\" and subclientid = \"%@\" and clientid = \"%@\" and inspectionid =\"%@\"",subcomponent.componentName,subcomponent.componentcreateddate, subcomponent.componentEntityid,subcomponent.componentClientName,subcomponent.componentSubclientName,subcomponent.componentid, subcomponent.componentsubclientid,subcomponent.componentClientid,subcomponent.componentinspectionid];
          const char *sql=[querySql UTF8String];
          if(sqlite3_prepare_v2(database,sql, -1, &amp;statement, NULL) == SQLITE_OK)
          {
              if(SQLITE_DONE != sqlite3_step(statement))
              {
                  NSLog(@"REMASH Error while updating. '%s'", sqlite3_errmsg(database));
              }
              else
              {
                  //sqlite3_reset(statement);
                  NSLog(@"Update done successfully!");
              }
          }
  
          NSLog(@"NOT OK");
          sqlite3_finalize(statement);
          sqlite3_close(database);
      }
  
  
  }
  </code></pre>
  
  <p><strong>PS</strong>:Can somebody suggest whether use of sqlite_finilaize(),sqlite_close().sqlite_reset() is correct by seeing my code.Because memory is increasing.</p>