2014-04-13 17:21:07
1
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.
Here's the common function that executes sqlite 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's the loop that gets me the database closed error:
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
Error seems to originate from the "getStageAdditives()" as it return 4 items while "getStageByDate()" return only 1.
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?