Python/MySQL Where do "SELECT ..." results go?

Andy Dustman adustman at comstar.net
Tue Mar 7 15:42:03 EST 2000


On Tue, 7 Mar 2000, Miles Thompson wrote:

> Dear All,
> 
> Please be patient with me, if I've done something incredibly dumb. I've
> workekd with FoxPro, Access, SQl Server for years, but the Python/MySQL
> combination is new to me. My query results disappear and all I get is a
> count of the records retrieved.
> 
> Here's what I've done. (Explore is the database, and table Test has 5
> records.)
> 
> >>> import Mysqldb
> >>>conn = Mysqldb.mysqldb('explore')
> >>>conn
> <Mysqldb.Connection instance at 810ea08>
> #
> # here's my first attempt at a select
> #
> >>>conn.execute("Select * from test;")
> 5
> #
> # wasn't what I wanted, so then I did this
> #
> >>>curs = conn.cursor()
> >>>curs.execute("Select * from test;")
> 5
> #
> # Nope. Try assigning to a variable?
> #
> >>>data = curs.execute("Select * from test;")
> >>>data
> 5
> #
> # and at this point it was 11:30, and over the past two days I'd
> # hammered myself through the Python tutorial, various Python docs,
> # set up MySQL and played with it interactively, reinstalled a new
> version
> # of Python (1.5.2) and the Python module for MySQL.
> # I used python-MySQLmodule-1.4.0-7.i386.rpm
> # So  I went to bed.

5 is the number of rows the query returned. To actually get the data, you
need to do:

nrows = curs.execute("Select * from test;")
data = curs.fetchall()

Check out the database topic guide over at www.python.org for the DB API
specification.
 

-- 
andy dustman       |     programmer/analyst     |      comstar.net, inc.
telephone: 770.485.6025 / 706.549.7689 | icq: 32922760 | pgp: 0xc72f3f1d
"Therefore, sweet knights, if you may doubt your strength or courage, 
come no further, for death awaits you all, with nasty, big, pointy teeth!"




More information about the Python-list mailing list