Aggregate funuctions broken in MySQLdb?

L

Lorenzo

I'm trying to use some of the agg functions in MySQLdb (avg, min, max),
but they're just not working as I would expect. They all return the
value 1 when executed as part of Python scripts, but work as expected
when used in mysql 4.1. Does anyone have any experience using Python
with MySQLdb? Quite frankly, the more I use it, the more MySQLdb seems
to be not quite ready for prime time.
 
W

Wade Leftwich

Works fine for me, and I certainly hope MySQLdb is ready for prime
time, because I use the heck out of it. Maybe you're getting fooled by
the fact that cursor.execute() returns the count of result rows. To
actually see the result rows, you have to say cursor.fetchone() or
fetchall() --

In [34]: cur.execute("select article_id from articles limit 10")
Out[34]: 10L

In [35]: cur.fetchall()
Out[35]: ((3L,), (4L,), (5L,), (6L,), (7L,), (8L,), (9L,), (10L,),
(11L,), (12L,))

In [36]: cur.execute("select count(article_id) from articles where
article_id < 13")
Out[36]: 1L

In [37]: cur.fetchall()
Out[37]: ((10L,),)

In [38]: cur.execute("select sum(article_id) from articles where
article_id < 13")
Out[38]: 1L

In [39]: cur.fetchone()
Out[39]: (75.0,)

In [40]: cur.execute("select avg(article_id) from articles where
article_id < 13")
Out[40]: 1L

In [41]: cur.fetchone()
Out[41]: (7.5,)
 
L

Lorenzo Thurman

Thanks, that was my problem. Can you point me to some documentation on
MySQLdb? I've been googling to get answers and that obviously has not
been working.
 
D

Dennis Lee Bieber

I've been looking into this too lately, and finding thorough docs for it
is hard. Have you seen these yet:
http://sourceforge.net/docman/?group_id=22307

For the most part, it follows the DB-API 2 specifications. The
subject of this thread (aggregates) would have occurred with ANY db-api
compliant adapter, even plain ODBC -- since it was a misunderstanding
that xxxx.execute() returns the status code (typically # of records
affected by the query), and xxxx.fetchnnn() is needed to obtain the data
values. This misunderstanding is not specific to use of aggregates as
any "select..." statement functions this way.

Most divergences from the db-api specifications should be
determinable by looking at the sources of the python portion of the
adapter; or by looking at the features of the underlying RDBM.
--
Wulfraed Dennis Lee Bieber KD6MOG
(e-mail address removed) (e-mail address removed)
HTTP://wlfraed.home.netcom.com/
(Bestiaria Support Staff: (e-mail address removed))
HTTP://www.bestiaria.com/
 
L

Lorenzo

Dennis Lee Bieber said:
For the most part, it follows the DB-API 2 specifications. The
subject of this thread (aggregates) would have occurred with ANY db-api
compliant adapter, even plain ODBC -- since it was a misunderstanding
that xxxx.execute() returns the status code (typically # of records
affected by the query), and xxxx.fetchnnn() is needed to obtain the data
values. This misunderstanding is not specific to use of aggregates as
any "select..." statement functions this way.

Most divergences from the db-api specifications should be
determinable by looking at the sources of the python portion of the
adapter; or by looking at the features of the underlying RDBM.

Thanks, you are correct. I have done similar database things using PHP
and Perl to connect to databases, and I felt like DUH, when I got my
first reply, but there are times when one cannot see the forest for the
trees, so to speak. Better docs can help.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,297
Messages
2,571,525
Members
48,249
Latest member
reactnativeexpert

Latest Threads

Top