SQLITE3.PROGRAMMINGERROR: Unable to run on the closed database. [Python] [SQLite]

I am using a common function to execute all the sqlite queries in the class. Everything works fine until I use a for loop with multiple items in the list.

This is a commonly used function for executing 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]

fi nally:
if self.conn:
self.conn.close()

return results

This is the loop that gets me the database close 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

The error seems to come from getStageAdditives(), because it returns 4 items, while getStageByDate() only returns 1.

In my opinion, the connection to the database was not closed before attempting the second connection. Why is this happening? It didn’t happen when used with MySQL database. What is the solution to this problem?

You wrote “In my opinion, before the second connection attempt, the database connection Not closed”, but in fact, the database does not have a “second connection”. You are using a single connection, I guess this was created in the initializer (__init__) for an unshown example class that contains the method execute_query. < p>

You (guess again) created conn in the __init__ method, but closed it immediately after executing any query. Therefore, when you execute another query, it will not be available.

p>

Conversely, you should not use .close() at the end of the query but .commit(). Don’t do this at the end, but at the end of the attempt. In this way, the transaction will be committed (if successful) or returned Roll (in the except block, if it fails).

Then, add a separate .close() method to the larger class, then call .close() on the connection, and let the calling program This method is called when all queries are completed. The close call will appear appropriately in the finally block within the larger program.

I am using a general function to execute the class All sqlite queries. Everything works fine until I use a for loop with multiple items in the list.

This is a common function for executing sqlite queries:

< /p>

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

This is the loop that makes me get the database close 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

The error seems to come from getStageAdditives() because it returns 4 items, and getStageByDate () only returns 1.

In my opinion, the connection with the database is not closed before trying to connect for the second time. Connect. Why is this? It didn’t happen when used with MySQL database. What is the solution to this problem?

You wrote “In my opinion, the database connection was not closed before trying to connect for the second time”, but in fact, the database did not have ” Second connection”. You are using a single connection, I guess this was created in the initializer (__init__) for an unshown example class that contains the method execute_query.

You (again Guess) Create conn in the __init__ method, but close it immediately after executing any query. Therefore, when you execute another query, it will not be available.

Instead, you should not be querying Use .close() instead of .commit() at the end. Don’t do this at the end, but at the end of the attempt. In this way, the transaction will be committed (if successful) or rolled back (in the except block, if it fails). < /p>

Then, add a separate .close() method to the larger class, then call .close() on the connection, and let the caller call this method when all queries are completed. The close call will Appropriately appear in a finally block within a larger program.

Leave a Comment

Your email address will not be published.