tuple indexing by name does not work

21 views
Skip to first unread message

sdw

unread,
Oct 23, 2011, 3:54:58 PM10/23/11
to pyodbc
Hello,

I am on Win64 / 64-bit python, and have the following 2 problems. No
idea what causes this:

1) cursor.rowcount always returns -1

2) result tuples cannot be accessed by name, for example:
cursor.execute(stuff)
for row in cursor.fetchall():
print row['field1']
gives a TypeError: tuple indices must be integers

This happens when accessing a ms access .accdb database. I do not have
MS Office installed, but use the redistributable at
http://www.microsoft.com/download/en/details.aspx?id=13255. The whole
thing works fine on another box with a 32-bit python installation. The
rest of the resultsets come through fine, it is just these two things
that don't work.

I am at a loss here. Any ideas greatly appreciated! (might not be a
pyodbc related issue of course).

Stefan

Michael Kleehammer

unread,
Oct 25, 2011, 1:22:17 PM10/25/11
to pyo...@googlegroups.com
The rowcount value is provided by the driver via the  SQLRowCount function.  Different databases do different things.  SQL Server, for example, will return -1 until all rows have been read.

On a brighter note, the attribute access is actually simpler:

  cursor.execute(stuff) 
  for row in cursor: 
    print row.field1

Two things, the ['field1'] is a lot of trouble and noise and not very Python-like, so pyodbc looks them up dynamically.  All rows from the same select share the field names, so it saves quite a bit of memory when selecting a lot of rows.  You can access the field names even after discarding the cursor using Row.cursor_description, which returns the same object as Cursor.description.

Second, instead of using fetchall(), iterate using the cursor directly, which saves a lot of memory.  When you call fetchall(), it returns a list of the rows all at once.

Reply all
Reply to author
Forward
0 new messages