python databaseoperate_sqlite3.ProgrammingError: Cannot operate on a closed database. (python / sqli...

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?

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值