Login | Register For Free | Help
Search for: (Advanced)

Mailing List Archive: Python: Python

mysqldb cursor returning type along with result ?

 

 

Python python RSS feed   Index | Next | Previous | View Threaded


thesul1970 at googlemail

Nov 29, 2009, 5:14 AM

Post #1 of 3 (203 views)
Permalink
mysqldb cursor returning type along with result ?

Just taken to Python (2.5)and started to look at some DB cursor stuff
using MySQL. Anyway, after creating a query that in MySQL that has a
result set of decimals I find that the cursor in python after a
fetchall() returns a tuple that contains the following ::

((Decimal("101.10"),), (Decimal("99.32"),), (Decimal("97.95"),),
(Decimal("98.45"),), (Decimal("97.39"),), (Decimal("97.91"),), (Decimal
("98.08"),), (Decimal("97.73"),))

as such :
sum(result)
fails with "TypeError: unsupported operand type(s) for +: 'int' and
'tuple'"

How do I either get the resultset back as 'float' or convert the
returned tuple to 'floats'.?

Thanks, Newb.
--
http://mail.python.org/mailman/listinfo/python-list


python.list at tim

Nov 29, 2009, 5:54 AM

Post #2 of 3 (201 views)
Permalink
Re: mysqldb cursor returning type along with result ? [In reply to]

> ((Decimal("101.10"),), (Decimal("99.32"),), (Decimal("97.95"),),
> (Decimal("98.45"),), (Decimal("97.39"),), (Decimal("97.91"),), (Decimal
> ("98.08"),), (Decimal("97.73"),))
>
> as such :
> sum(result)
> fails with "TypeError: unsupported operand type(s) for +: 'int' and
> 'tuple'"
>
> How do I either get the resultset back as 'float' or convert the
> returned tuple to 'floats'.?

Well, what you have is a tuple-of-tuples-of-decimals, and Sum can
handle Decimal types just fine. You simply have to extract the
first (only) item in each row:

sum(row[0] for row in result)

or

sum(value for (value,) in result)

whichever makes more sense to you.

-tkc



--
http://mail.python.org/mailman/listinfo/python-list


lie.1296 at gmail

Nov 29, 2009, 7:57 AM

Post #3 of 3 (196 views)
Permalink
Re: mysqldb cursor returning type along with result ? [In reply to]

On 11/30/2009 12:14 AM, Paul O'Sullivan wrote:
> Just taken to Python (2.5)and started to look at some DB cursor stuff
> using MySQL. Anyway, after creating a query that in MySQL that has a
> result set of decimals I find that the cursor in python after a
> fetchall() returns a tuple that contains the following ::
>
> ((Decimal("101.10"),), (Decimal("99.32"),), (Decimal("97.95"),),
> (Decimal("98.45"),), (Decimal("97.39"),), (Decimal("97.91"),), (Decimal
> ("98.08"),), (Decimal("97.73"),))
>
> as such :
> sum(result)
> fails with "TypeError: unsupported operand type(s) for +: 'int' and
> 'tuple'"
>
> How do I either get the resultset back as 'float' or convert the
> returned tuple to 'floats'.?

I believe it returned decimal.Decimal() objects. You can do arithmetic
with decimal.Decimals, so:

sum(x[0] for x in result)
--
http://mail.python.org/mailman/listinfo/python-list

Python python RSS feed   Index | Next | Previous | View Threaded
 
 


Interested in having your list archived? Contact Gossamer Threads
 
  Web Applications & Managed Hosting Powered by Gossamer Threads Inc.