-
Notifications
You must be signed in to change notification settings - Fork 56
Expand file tree
/
Copy pathapp.py
More file actions
138 lines (117 loc) · 4.99 KB
/
app.py
File metadata and controls
138 lines (117 loc) · 4.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# Author: Clinton Daniel, University of South Florida
# Date: 4/4/2023
# Description: This is a Flask App that uses SQLite3 to
# execute (C)reate, (R)ead, (U)pdate, (D)elete operations
from flask import Flask
from flask import render_template
from flask import request
import sqlite3
app = Flask(__name__)
# Home Page route
@app.route("/")
def home():
return render_template("home.html")
# Route to form used to add a new student to the database
@app.route("/enternew")
def enternew():
return render_template("student.html")
# Route to add a new record (INSERT) student data to the database
@app.route("/addrec", methods = ['POST', 'GET'])
def addrec():
# Data will be available from POST submitted by the form
if request.method == 'POST':
try:
nm = request.form['nm']
addr = request.form['add']
city = request.form['city']
zip = request.form['zip']
# Connect to SQLite3 database and execute the INSERT
with sqlite3.connect('database.db') as con:
cur = con.cursor()
cur.execute("INSERT INTO students (name, addr, city, zip) VALUES (?,?,?,?)",(nm, addr, city, zip))
con.commit()
msg = "Record successfully added to database"
except:
con.rollback()
msg = "Error in the INSERT"
finally:
con.close()
# Send the transaction message to result.html
return render_template('result.html',msg=msg)
# Route to SELECT all data from the database and display in a table
@app.route('/list')
def list():
# Connect to the SQLite3 datatabase and
# SELECT rowid and all Rows from the students table.
con = sqlite3.connect("database.db")
con.row_factory = sqlite3.Row
cur = con.cursor()
cur.execute("SELECT rowid, * FROM students")
rows = cur.fetchall()
con.close()
# Send the results of the SELECT to the list.html page
return render_template("list.html",rows=rows)
# Route that will SELECT a specific row in the database then load an Edit form
@app.route("/edit", methods=['POST','GET'])
def edit():
if request.method == 'POST':
try:
# Use the hidden input value of id from the form to get the rowid
id = request.form['id']
# Connect to the database and SELECT a specific rowid
con = sqlite3.connect("database.db")
con.row_factory = sqlite3.Row
cur = con.cursor()
cur.execute("SELECT rowid, * FROM students WHERE rowid = " + id)
rows = cur.fetchall()
except:
id=None
finally:
con.close()
# Send the specific record of data to edit.html
return render_template("edit.html",rows=rows)
# Route used to execute the UPDATE statement on a specific record in the database
@app.route("/editrec", methods=['POST','GET'])
def editrec():
# Data will be available from POST submitted by the form
if request.method == 'POST':
try:
# Use the hidden input value of id from the form to get the rowid
rowid = request.form['rowid']
nm = request.form['nm']
addr = request.form['add']
city = request.form['city']
zip = request.form['zip']
# UPDATE a specific record in the database based on the rowid
with sqlite3.connect('database.db') as con:
cur = con.cursor()
cur.execute("UPDATE students SET name='"+nm+"', addr='"+addr+"', city='"+city+"', zip='"+zip+"' WHERE rowid="+rowid)
con.commit()
msg = "Record successfully edited in the database"
except:
con.rollback()
msg = "Error in the Edit: UPDATE students SET name="+nm+", addr="+addr+", city="+city+", zip="+zip+" WHERE rowid="+rowid
finally:
con.close()
# Send the transaction message to result.html
return render_template('result.html',msg=msg)
# Route used to DELETE a specific record in the database
@app.route("/delete", methods=['POST','GET'])
def delete():
if request.method == 'POST':
try:
# Use the hidden input value of id from the form to get the rowid
rowid = request.form['id']
# Connect to the database and DELETE a specific record based on rowid
with sqlite3.connect('database.db') as con:
cur = con.cursor()
cur.execute("DELETE FROM students WHERE rowid="+rowid)
con.commit()
msg = "Record successfully deleted from the database"
except:
con.rollback()
msg = "Error in the DELETE"
finally:
con.close()
# Send the transaction message to result.html
return render_template('result.html',msg=msg)