Spewing SQL query resultset to HTML table...

Martin Franklin martin.franklin at westgeo.com
Wed Oct 10 02:44:57 EDT 2001


Steve,


Orr, Steve wrote:

> Newbie's first post... New Project... should I use Python or PHP?
> 
> I need to take ANY valid SQL query and display the resultset in HTML.
> KEY QUESTION: How do I capture the column aliases and display them as
> headers in an HTML table? I have over 100 SQL queries and I just want to
> use one simple routine to display results. I don't want reams of hand code
> just for output. I've already done this with PHP but I can't find a way to
> do it in Python. I'm assuming it's because of my Python newbie status and
> not because PHP is more capable.
> BY WAY OF EXAMPLE... here's how this was accomplished in PHP:
> --------------------------------
> <?php // proofofconcept.php


--- snip snip snip nasty PHP code<wink> ---


> 
> 

Not very OO but this will do it:-


## DCOracle is third party python / OCI extension (its free though!)

import DCOracle

## get connected

user = 'YourOracleUser'
password='YourPassword'
db='YourConnection(sid)'

connection=DCOracle.Connect('%s/%s@%s' %(user, password, db))
cursor=connection.cursor()
        
## get data

cursor.execute("SELECT * from ALL_USERS")
columns=cursor.description



## do column headings
print "<TABLE><TR>"
for col in columns:
    print "<TD>%s</TD>" %col[0]
print "</TR>"

## do the data

data=cursor.fetchall() ## may want to change this if large table

for row in data:
    print "<TR>"
    for col in row:
        print "<TD>%s</TD>" %col
    print "</TR>"
print "</TABLE>"





More information about the Python-list mailing list