List limitations?

Andrew M. Kuchling akuchlin at mems-exchange.org
Thu Mar 23 18:59:20 CET 2000


"Daley, MarkX" <markx.daley at intel.com> writes:
> 			result = crsr.fetchall()

What does fetchall() return when the result set is a single row?  Try
printing repr(result) at each step.  I'll wager that it returns a
single tuple, not a list of length 1 containing a tuple.  This means
that the 'for item in range(len(result))' winds up looping over the
individual strings in the tuple.  result[item] is therefore a string,
and 'data1, data2, data3, ... = result[item]' then unpacks the string
into single characters.  This happens because strings are also
sequences:

>>> a,b,c = 'abc'
>>> a
'a'
>>> b
'b'
>>> a,b,c = 'abcd'
Traceback (innermost last):
  File "<stdin>", line 1, in ?
ValueError: unpack sequence of wrong size

If that's the case, you'd have to do something like this, to find when
.fetchall() hasn't returned a list, and turn it into a single-element
list yourself:

import types
 ...
	result = crsr.fetchall()
	if type( result ) != types.ListType:
	    # Didn't return a list, so it must be a single tuple
	    result = [result]
        
        for item in range(len(result)):

The author of the ODBC code shouldn't have written such an
inconsistent interface for fetchall(), though maybe there's some
esoteric reason for it.  

-- 
A.M. Kuchling			http://starship.python.net/crew/amk/
Nobody can be exactly like me. Sometimes even I have trouble doing it.
    -- Tallulah Bankhead




More information about the Python-list mailing list