#! /usr/local/bin/python
import os, sys, string
import Ns
# We define doc as a list of lines to avoid successive contatenations
# onto the end of a string (which would be O(N^2)):
doc = [
'<html>',
'<head><title>test-db.py</title></head>',
]
conn = Ns.GetConn()
dbhandle = Ns.DbHandle(Ns.DbPoolDefault(conn.Server()))
doc.append('<body>')
set = dbhandle.Select('select sysdate from dual')
doc.append('<pre>Contents of set before first row is read: %s</pre>'
% (Ns.QuoteHtml(repr(set.dict())),))
doc.append('<table border="1" cellpadding="1">')
doc.append('<tr>')
doc.append('<th rowspan="2">#</th>')
for i in range(len(set)):
doc.append('<th>%d</th>' % (i,))
doc.append('</tr>')
doc.append('<tr>')
for k in set.keys():
doc.append('<th>%s</th>' % (Ns.QuoteHtml(k),))
doc.append('</tr>')
i = 0
while dbhandle.GetRow(set) == Ns.OK:
doc.append('<tr>')
doc.append('<th>%d</th>' % (i,))
for v in set.values():
doc.append('<td>"%s"</td>' % (Ns.QuoteHtml(v),))
doc.append('</tr>')
i = i + 1
if i > 1000:
break
doc.append('</table>')
doc.append('</body>')
doc.append('</html>')
conn.ReturnHtml(200, string.join(doc, '\n'))
# Python's cleanup of global symbols is unavoidably unreliable, so
# delete the database handle explicitly:
del dbhandle
|