Lino Website

Query.fetchall()

Monday, 29. August 2005 13:48.

New method Query.fetchall() which does a fetchall() on the query's DataIterator. This is used in ledger's DemoPopulator to avoid the database table is locked problem.

The following code raises an OperationalError because a table cannot be updated while we are looping on it:

  q=sess.query(Partner)
  for p in q:
      p.lock()
      p.currency=BEF
      p.unlock()

The following code works because the fetchall() does the loop before, collecting the rows into a list. When we iterate on this list, the table has already been released.

  q=sess.query(Partner)
  for p in q.fetchall():
      p.lock()
      p.currency=BEF
      p.unlock()

Of course this approach works only with reasonable number of rows to be updated. It is a non-trivial problem.

Copyright 2001-2007 Luc Saffre.
http://lino.saffre-rumma.ee
Generated 2007-06-07 16:22:31