mysqldb cursor returning type along with result ?

Discussion in 'Python' started by Paul O'Sullivan, Nov 29, 2009.

  1. 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.
    Paul O'Sullivan, Nov 29, 2009
    #1
    1. Advertising

  2. Paul O'Sullivan

    Tim Chase Guest

    > ((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
    Tim Chase, Nov 29, 2009
    #2
    1. Advertising

  3. Paul O'Sullivan

    Lie Ryan Guest

    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)
    Lie Ryan, Nov 29, 2009
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Michel Combe

    MySQLdb and Cursor

    Michel Combe, Oct 9, 2003, in forum: Python
    Replies:
    3
    Views:
    385
    Andy Todd
    Oct 10, 2003
  2. Brian Kelley

    MySQLDB multiple cursor question

    Brian Kelley, Jan 7, 2004, in forum: Python
    Replies:
    7
    Views:
    1,400
    Brian Kelley
    Jan 16, 2004
  3. Tim Williams
    Replies:
    2
    Views:
    628
    Tim Williams
    May 28, 2004
  4. dmaziuk
    Replies:
    3
    Views:
    540
    Chris Gonnerman
    Jan 25, 2011
  5. Michael Tan
    Replies:
    32
    Views:
    909
    Ara.T.Howard
    Jul 21, 2005
Loading...

Share This Page