[Tutor] Counting MySQL Fields

Alan G alan.gauld at freenet.co.uk
Sat Jun 11 10:07:58 CEST 2005

> I'd like to use the result.numfields() function to count the fields
on a
> given table, but apparently don't quite grasp how to implement it.
Below is
> my lame attempt.

OK, I've never use the numfields function so have no idea how it works
nor why I'd want to use it but...

> sql = "SELECT * FROM person order by lst_name"

Oh OK, I see why - you are using * instead of named fields...

> Cursor.execute(sql)
> # Fetch all results from the cursor into a sequence and close the
> result = Cursor.fetchone()
> Con.close()
> # Count the fields
> result.num_fields()

First of all this seems odd since, although you call the function, you
aren't storing the result anywhere, even if it worked!

> ------ Relevant info from the resulting Traceback ---------
> result.num_fields()
> AttributeError: 'Tuple' object has no attribute 'num_fields'

If results is a tuple then presumably the number of fields is just
the len() of the tuple?


Alan G.

More information about the Tutor mailing list