how can I put mysql input without ([,])

Gerhard Häring gerhard.haering at gmx.de
Wed Jul 3 15:42:42 EDT 2002


Ria wrote in comp.lang.python:
> Hi,
> 
> I have a question about list or string in Python/Jython.
> If I get a select command from mysql en put it in a string (print) than I
> can't use it.
> 
> example...
> I wil know how many rows ther are in my database table:
> 
>>>>cursur.execute("select count(*) from table")
>>>>data = cursur.fetchall()
>>>>if data:
>>>>    for a in data:
> ...            print a
> ...
> ([21345,])
> 
> how can I put only the 21345 without the ([,])??
 
cursor.fetchall() returns a list of tuples. Each tuple contains the
columns from your query. Obviously, your query only returns one row,
with a one-element tuple.

So better use something like this:

    cursor.execute("select count(*) from table")
    result = cursor.fetchone()
    count = result[0]
    print "We have %i rows in the table." % count

I use fetchone() because I know that there's exactly one row. No need
to use fetchmany() or fetchall() in this case.

Gerhard
-- 
mail:   gerhard <at> bigfoot <dot> de       registered Linux user #64239
web:    http://www.cs.fhm.edu/~ifw00065/    OpenPGP public key id AD24C930
public key fingerprint: 3FCC 8700 3012 0A9E B0C9  3667 814B 9CAA AD24 C930
reduce(lambda x,y:x+y,map(lambda x:chr(ord(x)^42),tuple('zS^BED\nX_FOY\x0b')))



More information about the Python-list mailing list