WilliamRen / django-pyodbc

Automatically exported from code.google.com/p/django-pyodbc
BSD 3-Clause "New" or "Revised" License
0 stars 0 forks source link

'CursorWrapper' object is not iterable -- when iterating result of raw() query #70

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
What steps will reproduce the problem?

list(SomeModel.objects.raw("SELECT * FROM SomeModelTable"))

Crashes -- "'CursorWrapper' object is not iterable" with a backtrace ending 
with:

File "C:\Python26\lib\site-packages\django\db\models\query.py" in __iter__
  1248.         for row in self.query:
File "C:\Python26\lib\site-packages\django\db\models\sql\query.py" in 
__iter__
  58.         return iter(self.cursor)

django-pyodbc revision 175
django 1.2 alpha 1 SVN-12224
windows 2003, sql server 2000

I was able to "fix" the problem by providing the iterator in 
sql_server.pyodbc.base.CursorWrapper:

   def __iter__(self):
      class MyIterator(object):
        def __init__(self, wrapper):
          self.rows = wrapper.fetchall()
          self.ix = 0

        def __iter__(self):
          return self

        def next(self):
          if self.ix == len(self.rows):
            raise StopIteration
          else:
            self.ix += 1
            return self.rows[self.ix - 1]

      return MyIterator(self)

I'm sure this is a terrible solution, but it works, and maybe will help 
coming up with a proper solution.

Original issue reported on code.google.com by ama...@gmail.com on 22 Jan 2010 at 3:38

GoogleCodeExporter commented 8 years ago
fixed in r176.

Original comment by vcc.ch...@gmail.com on 24 Jan 2010 at 7:47