· 9 years ago · Dec 04, 2016, 06:40 AM
1#!/usr/bin/python
2
3#import required modules/libraries
4import cgi
5import cgitb; cgitb.enable()
6import sqlite3
7
8
9#link our html form submission elements to our script
10form = cgi.FieldStorage()
11fname = form.getvalue('fname')
12lname = form.getvalue('lname')
13course = form.getvalue('course')
14wtype = form.getvalue('work')
15grades = form.getvalue('grades')
16
17
18#create a function that creates a database (if it doesnt already exist) and adds values from our form
19def Main():
20
21 try:
22 con=sqlite3.connect('test.db')
23
24 cur = con.cursor()
25 cur.execute('CREATE TABLE IF NOT EXISTS School(FirstName TEXT, LastName TEXT, Course TEXT, WorkType TEXT, Grades TEXT)')
26 cur.execute("INSERT INTO School VALUES (?, ?, ?, ?, ?)", (fname, lname, course, wtype, grades))
27
28 con.commit()
29
30 cur.execute('SELECT * FROM School')
31 data = cur.fetchall()
32
33 for row in data:
34 print(row)
35
36 except sqlite3.Error:
37 if con:
38 print("Error! Rolling back")
39 con.rollback()
40 finally:
41 if con:
42 con.close()
43
44if __name__ == '__main__':
45 Main()
46
47
48#create a function that displays the data in the database as html (as of right now its just placeholder to see if everything works)
49
50def GenHtml():
51 print "Content-type: text/html"
52 print """
53 <html><body><h1>Hello</h1></body></html>
54 """
55
56GenHtml()