Python数据集数据库错误

时间:2014-10-03 17:32:15

标签: python database sqlite sqlalchemy dataset

我写了一个小脚本,它在社交网络中迭代一个feed,并在另一个社交网络中发布尚未发布的帖子。我通过两个步骤完成这项工作:首先,我阅读了第一个社交网络的供稿,并将每个帖子都放在我的数据库中,包括发布的'布尔值。其次,我遍历数据库中所有已发布布尔值设置为false的条目,并在第二个社交网络上发布消息。 我使用dataset库连接到我的本地SQLite数据库。现在问题是:在第一步中一切正常。在第二步中,我将提到的布尔值设置为false迭代数据库条目,将条目发布到第二个社交网络,然后我想更新数据库中的布尔值。问题在于:更新只能运行一次,然后我收到以下错误:

Traceback (most recent call last):
  File "xy.py", line 56, in <module>
    table.update(data, ['sc_id'])
  File "/Users/.../lib/python2.7/site-packages/dataset/persistence/table.py", line 137, in update
    rp = self.database.executable.execute(stmt)
  File "/Users/.../lib/python2.7/site-packages/sqlalchemy/engine/base.py", line 1752, in execute
    return connection.execute(statement, *multiparams, **params)
  File "/Users/.../lib/python2.7/site-packages/sqlalchemy/engine/base.py", line 729, in execute
    return meth(self, multiparams, params)
  File "/Users/.../lib/python2.7/site-packages/sqlalchemy/sql/elements.py", line 321, in _execute_on_connection
    return connection._execute_clauseelement(self, multiparams, params)
  File "/Users/.../lib/python2.7/site-packages/sqlalchemy/engine/base.py", line 826, in _execute_clauseelement
    compiled_sql, distilled_params
  File "/Users/.../lib/python2.7/site-packages/sqlalchemy/engine/base.py", line 997, in _execute_context
    self._commit_impl(autocommit=True)
  File "/Users/.../lib/python2.7/site-packages/sqlalchemy/engine/base.py", line 530, in _commit_impl
    self.connection._reset_agent is self.__transaction:
  File "/Users/.../lib/python2.7/site-packages/sqlalchemy/engine/base.py", line 236, in connection
    return self._revalidate_connection()
  File "/Users/.../lib/python2.7/site-packages/sqlalchemy/engine/base.py", line 247, in _revalidate_connection
    raise exc.ResourceClosedError("This Connection is closed")
sqlalchemy.exc.ResourceClosedError: This Connection is closed

这是我更新数据库字段的方式(根据数据集文档):

unpublished_messages = table.find(published = False)
for posting in unpublished_messages:
    # here i post to the second social network...
    data = dict(sc_id = posting['sc_id'], published = True)
    table.update(data, ['sc_id'])

它在第一步中完美地工作,并且在迭代数据库条目时它在第二步中工作一次,但在第二次循环迭代时它在table.update ... line崩溃。

我绝对不明白发生了什么。

1 个答案:

答案 0 :(得分:0)

不知何故,人们无法遍历ResultIter('unpublished_messages')并在同一步骤中修改表。我通过遍历ResultIter并将元素放入普通的python列表来解决这个问题。然后我迭代了列表并更新了表格。