Database to Web

Steve Holden sholden at bellatlantic.net
Fri Jan 14 14:33:58 EST 2000


Do you mean the HTML renders in an ugly way, or that you don't like
the look of the Python code?  Your program seems an entirely acceptable
way to get the data out, but clearly it won't look too nice to the
user.

For what it's worth, the table rows you are generating aren't well-
formed, since the row contents should be contained between table data
tags.  A minimal improvement would be to replace the

  print Name

statement with

  print "<td>%s</td>" % Name

But a better solution would give each element its own cell:

  for i in range(len(Name)):
    print "<td>%s</td>" % Name[i]

to get a more readable result.  Of course, in any given context you
should know the (fixed) length of the tuples returned for each row,
and thus be able to optimize the len(Name) to a fixed value.

regards
 Steve

Simon Faulkner wrote:
> 
> Has anyone got a good sugestion for the easiest way to display the
> results of an sql query on a web page?
> 
> This works but it's not pretty!
> 
> TIA
> 
> def listdetails():
>   db =MySQLdb.connect(host="localhost",db='BTC',user="root",passwd="")
>   cursor = db.cursor()
>   cursor.execute("SELECT * FROM tblDetail WHERE lngSalesPersonID=30")
>   tuple = cursor.fetchall()
>   total = len(tuple)
>   if total < 1:
>     print "No entries"
>   else:
>     print "Content-type: text/html\n"
>     print "<H3>DETAILS</H3>"
>     print "<P>"
>     print "<table border cellspacing=0 cellpadding=5>"
>     for record in range(total):
>       Name =  tuple[record]
>       print "<tr>"
>       print Name
>       print "</tr>"
>     print "</table>"
>   db.close()
> 
> Simon



More information about the Python-list mailing list