approach for assigning ODBC results to fieldnames as variables?

fluxent at yahoo.com fluxent at yahoo.com
Tue Feb 27 10:41:19 EST 2001


When you execute a SQL (select) query via ODBC and do a fetchall(), 
you get back a result set as a tuple (array). You can also ask for 
the cursor's description, which returns a separate tuple, the first 
column of which contains the fieldnames (or other labels calculated 
by SQL).

Getting back an unlabelled tuple means that when you want to write 
out a table of results (with some individual handling of various 
fields, not just dumbly iterating over the columns), you end up with 
an ugly line like:
  Response.Write('<tr><td>%s<td>%s<td>%s<td><a 
href="matterEdit.asp?clientID=%s&matterID=%s">%s</a><td>%s<td>%
s</tr>' % (row[0],row[1],row[2],row[1],row[3],row[3],row[4],row[5]))

(this is a Python-via-IIS model, but would be similar under Apache, 
etc.).

Is there a standard approach to assigning field names to columns, so 
that this output line could be changed to something like
  Response.Write('<tr><td>%s<td>%s<td>%s<td><a 
href="matterEdit.asp?clientID=%s&matterID=%s">%s</a><td>%s<td>%
s</tr>' % (maxDate, clientID, clientName, clientID, jobID, jobID, 
jobName, totalHours))

or even at least something like
  Response.Write('<tr><td>%s<td>%s<td>%s<td><a href="matterEdit.asp?
clientID=%s&matterID=%s">%s</a><td>%s<td>%s</tr>' % (maxDate[i], 
clientID[i], clientName[i], clientID[i], jobID[i], jobID[i], jobName
[i], totalHours[i]))

thx for your help...






More information about the Python-list mailing list