[Tutor] Counting MySQL Fields

Don Parris webdev at matheteuo.org
Sat Jun 11 09:52:13 CEST 2005


I've dabbled in programming/scripting a little, but never really seem to get
the hang of it.  I thought I'd try out Python for a front-end to my
SQL database project.  I've worked through the tutorial, and a few others to
some extent.  At the moment, I'm really just tinkering, but have succesfully
connected Python to MySQL and performed a simple query.

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.  I searched the comp.lang.python group via Google, but
didn't find any relevant info.  The MySQLdb User Guide didn't give an
example, nor does the module source.

My attempt here was to retrieve a single row, and count the fields.  That
obviously didn't work. If there's a good example of this, I'd appreciate a
pointer.  If you can explain this operation a bit, that would be useful
also.  I'm running SUSE Linux 9.2 Pro on an AMD box.  

--------- My Test Script ------------
sql = "SELECT * FROM person order by lst_name"

# Fetch all results from the cursor into a sequence and close the connection
result = Cursor.fetchone()

# Count the fields

------ Relevant info from the resulting Traceback ---------
AttributeError: 'Tuple' object has no attribute 'num_fields'

Using variations of this gives errors to the effect that "result" or
num_fields" is "undefined".

DC Parris   GNU Evangelist
http://matheteuo.org/                  http://lulu.com/dcparris
Read the highly rated, widely recommended "Penguin in the Pew"!

More information about the Tutor mailing list