Munchies Experiments

Munchie needs to find some more chemicals for her experiments, can you help her?
Coded by Madeline from Ellwood School!
We had #!/usr/bin/python import sys import sqlite3 import os try: page = os.environ["DOCUMENT_URI"] address = os.environ["REMOTE_ADDR"] except: page = sys.argv[1] address = "unknown" conn = sqlite3.connect('hitcounter.db') c = conn.cursor() # c.execute("CREATE TABLE IF NOT EXISTS pages ( id INTEGER PRIMARY KEY, page TEXT UNIQUE)") # c.execute("CREATE TABLE IF NOT EXISTS counter ( page_id INTEGER, count INTEGER, FOREIGN KEY (page_id) REFERENCES pages(id) ON DELETE CASCADE )") # c.execute("CREATE TABLE IF NOT EXISTS accesslog ( page_id INTEGER, ip TEXT, ts DATETIME DEFAULT CURRENT_TIMESTAMP,FOREIGN KEY (page_id) REFERENCES pages(id) ON DELETE CASCADE )") def get_page_id(page): global conn c = conn.cursor() c.execute("SELECT id from pages WHERE page=:page", {"page": page} ); row = c.fetchone() if row != None: return row[0] c.execute("INSERT INTO pages ( page ) VALUES ( :page )", {"page": page} ); return get_page_id(page); def update_access_log(page_id, address): global conn c = conn.cursor() c.execute("INSERT INTO accesslog ( page_id, ip ) VALUES( :page, :ip )", {"page": page_id, "ip": address } ); def update_counter(page_id): c.execute("SELECT count FROM counter WHERE page_id=:page", { "page": page_id } ) row = c.fetchone() cnt = 0 if row != None: cnt = row[0]+1 conn.execute("UPDATE counter SET count=count+1 WHERE page_id=:page", { "page": page_id }) else: cnt = 1 conn.execute("INSERT INTO counter (page_id, count) VALUES (:page,1)", {"page": page_id } ) return cnt page_id = get_page_id(page) update_access_log(page_id, address) cnt = update_counter(page_id) conn.commit() print "Content-type: text/plain\n\n" print(cnt) visitors!