title: sqlite3.ProgrammingError: Cannot operate on a closed database. [Python] [sqlite]
body: <p>I am using a common function to execute all sqlite queries in a class. It all works, until I use a for loop with more than one item in the list.</p>
  
  <p>Here's the common function that executes sqlite queries:</p>
  
  <pre><code>def executeQuery(self, query, params = ()):
          results = {}
          try:
              cur = self.conn.cursor()
              cur.execute(query, params)
              self.conn.commit()
              rows = cur.fetchall()
  
              results['status'] = 'success'
              result = []
              if rows:
                  column = map(lambda x: x[0], cur.description)
                  for row in rows:
                      result.append( dict(zip(column, row)) )
  
              results['results'] = result
  
          except self.conn.Error, e:
              if self.conn:
                  self.conn.rollback()
  
              print "Error: %s" % e.args[0]
              results['status'] = 'failure'
              results['results'] = e.args[0]
  
          finally:
              if self.conn:
                  self.conn.close()
  
          return results
  </code></pre>
  
  <p>And here's the loop that gets me the database closed error:</p>
  
  <pre><code>stages = self.getStageByDate(2000)
          for stage in stages['results']:
              print stage['name']
              additives = self.getStageAdditives(stage['name'])
              print additives
              for additive in additives['results']:
                  print additive
  </code></pre>
  
  <p>Error seems to originate from the <code>getStageAdditives()</code> as it return 4 items, while <code>getStageByDate()</code> return only 1.</p>
  
  <p>It seems to me like the connection to the database is not closed before the second connection is attempted. Why does this happen? It did not happen when used with MySQL database. What are the solutions to this issue?</p>