I use a generic function to execute all sqlite queries in a class. Everything works until I use a for loop with more than one item in the list.
Here's a generic function that executes SQL queries:
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
And here is the loop that causes me an error with a closed database:
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
The error seems to come from "getStageAdditives ()" because it returns 4 elements, and "getStageByDate ()" returns only 1.
It seems to me that the database connection does not close before trying a second connection. Why is this happening? This did not happen when used with a MySQL database. What are the solutions to this problem?