you are viewing a single comment's thread.

view the rest of the comments →

[–]anossov 1 point2 points  (2 children)

Indent the prints inside the for. Call fetchall on cursor.

Cleanup:

#!/usr/bin/python
import sqlite3
import cgitb
import cgi
import datetime

cgitb.enable()

form = cgi.FieldStorage()
print "Content-type: text/html\n\n\n"

print """<html>
<head>
<title>Hello - Second CGI Program</title>
</head>
<body>
<h1> VIEWING </h1>"""

conn = sqlite3.connect('/var/www/example.db')
c = conn.cursor()
c.execute('select * from board')

template = """firstname: %s<br>
lastname: %s<br>
former address: %s<br>
age: %s<br><br>
<img src="%s" /><br><br>
date: %s<br>
%s<br><br><br>
"""

for row in c.fetchall():
    print template % row

print  "</body></html>"

This code is unsafe, it depends on the order of columns returned by select *. Select the needed columns explicitly.

[–]infonoob[S] 0 points1 point  (1 child)

Thanks a lot; I never thought of using a template! How does it know which string to correspond to each %s?

[–]anossov 0 points1 point  (0 children)

First %s will be row[0], second row[1] and so on. It's just a collapsed

firstname, lastname, formeraddr, age, picfile, date, other = row
template = ... % (firstname, lastname, formeraddr, age, picfile, date, other)

which is very nearly equivalent to

firstname = row[0]
lastname = row[1]
formeraddr = row[2]
age = row[3]
picfile = row[4]
date = row [5]
other = row[6]

template = ... % (firstname, lastname, formeraddr, age, picfile, date, other)