title: Print python database query to HTML document
body: <p>I wrote some python to query an Oracle database and I would like it to print the results in a formatted HTML table when I look at it with my browser.  I am unsure how to do this.</p>
  
  <p>The python I wrote is as below: </p>
  
  <pre><code>#!/usr/bin/python2.6
  import imp,datetime
  import cx_Oracle
  
  def index():
     conn_str = u'$USERNAME/$PASSWORD@$HOSTNAME:$PORT/$SERVICENAME'
     conn = cx_Oracle.connect(conn_str)
     c = conn.cursor()
     query = c.execute(u'SELECT $FIELD1, $FIELD2, $FIELD3 FROM $TABLE')
     cur = c.fetchall()
     for row in cur:
        print(str(row))
  
  
     conn.close()
  </code></pre>
  
  <p>A co-worker of mine has a similar script he wrote that is immensely more complicated in terms of credentialing and cursor creation, and he uses the "write" in Python to output.  With his I can at least get output to a webpage, and I can't understand why mine won't even show anything, let alone my query results.  The problem is that his output comes out unformatted and even if I used his code I don't know how to give it table structure.</p>
  
  <p>For contrast, his:</p>
  
  <pre><code>#!/usr/bin/python2.6
  import os
  os.environ["ORACLE_BASE"]="/oracle"
  os.environ["ORACLE_HOME"]="/oracle/product/11.2.0/client_1"
  os.environ["LD_LIBRARY_PATH"]="/oracle/product/11.2.0/client_1/lib:/oracle/product/11.2.0/client_1/dbjava/lib"
  os.environ["TNS_ADMIN"]="/oracle/product/11.2.0/client_1/network/admin"
  import imp,datetime
  import cx_Oracle
  
  DBCONNECTED=""
  CONNECT={}
  
  def connect(tnsname):
     global DBCONNECTED
     DB={}
     DB['$DATABASE']=['$USER','$PASSWORD']
  
     #print str(DB[tnsname][0]+"/"+DB[tnsname][1]+"@"+tnsname)
     conn=cx_Oracle.connect(DB[tnsname][0]+"/"+DB[tnsname][1]+"@"+tnsname)
     DBCONNECTED+=tnsname+":"
     return conn
  
  def getcredentials(env,user):
     env=env.lower()
     CRED={};CRED['$DATABASENAME']={};
     CRED['$DATABASENAME']['$USERNAME']='$PASSWORD'
     if env in CRED and user in CRED[env]:
        return CRED[env][user]
     else:
        return 'ERR'
  
  def returnconnection(dbtns):
     global CONNECT
     if DBCONNECTED.find(dbtns+":")==-1: #connection hasn't been initialized, do that
        CONNECT[dbtns]=connect(dbtns.lower())
     cur= CONNECT[dbtns].cursor()
     return cur
  
  def runq(dbtns,query,bindvar=''):
     query=query.replace("\n"," ")
     cur=returnconnection(dbtns)
     if bindvar=='':
        cur.execute(query)
     else:
        cur.execute(query,bindvar)
  
     rs=cur.fetchall() #this should be fine for up to several thousand rows
     return rs
  
  def index (req,rssid=""):
     global R; R=req; R.content_type="text/html"
     R.write("""
        &lt;!DOCTYPE HTML"&gt;
        &lt;html&gt;&lt;head&gt;&lt;title&gt;TABLES&lt;/title&gt;&lt;META HTTP-EQUIV='Pragma' CONTENT='no-cache'&gt;
        &lt;/head&gt;
        &lt;table&gt;
     """)
     dat=runq('$DATABASE','SELECT $FIELD1, $FIELD2, $FIELD3 FROM $TABLE')
  
     for row in dat:
        R.write(str(row))
  
  
     #write footer
     R.write("""
     &lt;/table&gt;
     &lt;/body&gt;&lt;/html&gt;
     """)
  </code></pre>
  
  <p>I like the simplicity of what I wrote, but my colleague is obviously doing something right to spit the output to a page.  When I try and re-create his usage of "Global R" to invoke "R.write" I get a unicode error regarding the content_type module, which seems odd.</p>
  
  <p>Regardless, I feel this should be insanely simple.  I'm more used to PHP, and this is my first attempt at using Python to create this sort of webpage.</p>
  
  <p>Ideas?</p>