Python and SQLite Problem

software development

#1

Hello,

I am new to both SQLite and Python. I am trying to write a script that calls data from a database and prints it to a website. The problem is, when I try to do this, as soon as I try to do something with cursor.execute(“SQL cmd”), the webpage stops printing at that spot and does not continue. When I run it in from the shell, however, the program prints as it should.

My script is:
import cgi
import sqlite3 as sql

def defineContentType():
print “Content-type:text/html\n"
defineContentType()
print “””

test page This is before
"""

conn = sql.connect(“data_temp”)
cur = conn.cursor()
cur.execute(’’‘SELECT type FROM sqlite_master WHERE tbl_name=“data”’’’)
if len(cur.fetchall())>0:
cur.execute(’’‘DROP TABLE data’’’)
conn.commit()
cur.execute(’’‘CREATE TABLE data (
id INTEGER PRIMARY KEY AUTOINCREMENT,
strfield varchar(8),
numfld1 FLOAT NOT NULL,
numfld2 FLOAT NOT NULL)’’’)
conn.commit()
cur.execute(’’‘INSERT INTO data (strfield, numfld1, numfld2) VALUES ("%s",%f,%f)’’’ % (“testing123”,40.8052, -73.9654))
conn.commit()
cur.execute(’’‘SELECT * FROM data’’’)
print cur.fetchall()
conn.close()

print “”"This is after

"""

When I call it from a browser, this is what I get:

test page This is before

But when I run the file from the shell, I get:
Content-type:text/html

test page This is before
[(1, u'testing123', 40.805199999999999, -73.965400000000002)] This is after

Can anyone please explain why the program might stop printing when retrieved through a browser and run fine when run in the shell?