[Newbie] Strange output from list

Ben Finney bignose+hates-spam at benfinney.id.au
Mon Nov 10 20:41:10 EST 2008


Gilles Ganault <nospam at nospam.com> writes:

> Hello
> 
> I'm getting some unwanted result when SELECTing data from an SQLite
> database:
> 
> ======
> sql = 'SELECT id FROM master'
> rows=list(cursor.execute(sql))
> for id in rows:
> 	sql = 'SELECT COUNT(code) FROM companies WHERE code="%s"' % id[0]
> 	result = list(cursor.execute(sql))
> 	print "Code=%s, number=%s" % (id[0],result[0])
> ======	
> Code=0111Z, number=(47,)	
> ======
> 
> I expected to see "number=47". Why does Python return "(47,)"?

The result of an SQL SELECT is a sequence of tuples, where each item
in the tuple is a value for a column as specified in the SELECT
clause.

SQLAlchemy represents this with a sequence of ResultProxy objects.
When you convert a ResultProxy object to a string, it displays like a
tuple. See the documentation for other ways of accessing various
attributes of a ResultProxy object.

-- 
 \         “What is it that makes a complete stranger dive into an icy |
  `\   river to save a solid gold baby? Maybe we'll never know.” —Jack |
_o__)                                                           Handey |
Ben Finney



More information about the Python-list mailing list