import Ns, sys
psql_fmt = """\
<p>%s:
<blockquote><b>
%s
</b></blockquote>
</p>
"""
failed_string = """\
<blockquote><p>
<font color=red><i>(failed)</i></font>
</p></blockquote>
"""
def psql(msg,sql):
print psql_fmt % (msg,sql,)
print 'Content-type: text/html\n'
print '''\
<body bgcolor=white>
<h2>Simple SQL commands</h2>
<i>Warning:</i>
You need to have an SQL database installed with AOLserver in order to
run this script.
<p>
This script runs a bunch of SQL commands through the AOLserver
database interface system.
<p>
<hr>
'''
conn = Ns.GetConn()
try:
db = Ns.DbHandle(Ns.DbPoolDefault(conn.Server()))
except:
print 'Can\'t get handle -- do you have a database installed??'
sql = 'CREATE TABLE pywx_test (i INTEGER, j INTEGER)'
psql('Creating table pywx_test', sql)
try:
db.DML(sql)
except:
print failed_string
for i in range(0, 3):
sql = 'INSERT INTO pywx_test VALUES (%d, %d)' % (i, i)
psql('Inserting (%d, %d) into pywx_test' % (i,i), sql)
try:
db.DML(sql)
except:
print failed_string
sql = 'SELECT i,j FROM pywx_test'
psql('Selecting from pywx_test', sql)
try:
results = db.Select(sql)
while db.GetRow(results) == Ns.OK:
print 'Got row: %s, %s<br>' % (results['i'], results['j'])
except:
print failed_string
sql = 'DROP TABLE pywx_test'
psql('Dropping table pywx_test', sql)
try:
db.DML(sql)
except:
print failed_string
del db
|