-
Notifications
You must be signed in to change notification settings - Fork 2
/
app.py
104 lines (82 loc) · 3.16 KB
/
app.py
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
from turtle import st
from flask import Flask, render_template, request, redirect, url_for, session
from markupsafe import escape
import ibm_db
conn = ibm_db.connect("DATABASE=<databasename>;HOSTNAME=<your-hostname>;PORT=<portnumber>;SECURITY=SSL;SSLServerCertificate=DigiCertGlobalRootCA.crt;UID=<username>;PWD=<password>",'','')
app = Flask(__name__)
@app.route('/')
def home():
return render_template('home.html')
@app.route('/addstudent')
def new_student():
return render_template('add_student.html')
@app.route('/addrec',methods = ['POST', 'GET'])
def addrec():
if request.method == 'POST':
name = request.form['name']
address = request.form['address']
city = request.form['city']
pin = request.form['pin']
sql = "SELECT * FROM students WHERE name =?"
stmt = ibm_db.prepare(conn, sql)
ibm_db.bind_param(stmt,1,name)
ibm_db.execute(stmt)
account = ibm_db.fetch_assoc(stmt)
if account:
return render_template('list.html', msg="You are already a member, please login using your details")
else:
insert_sql = "INSERT INTO students VALUES (?,?,?,?)"
prep_stmt = ibm_db.prepare(conn, insert_sql)
ibm_db.bind_param(prep_stmt, 1, name)
ibm_db.bind_param(prep_stmt, 2, address)
ibm_db.bind_param(prep_stmt, 3, city)
ibm_db.bind_param(prep_stmt, 4, pin)
ibm_db.execute(prep_stmt)
return render_template('home.html', msg="Student Data saved successfuly..")
@app.route('/list')
def list():
students = []
sql = "SELECT * FROM Students"
stmt = ibm_db.exec_immediate(conn, sql)
dictionary = ibm_db.fetch_both(stmt)
while dictionary != False:
# print ("The Name is : ", dictionary)
students.append(dictionary)
dictionary = ibm_db.fetch_both(stmt)
if students:
return render_template("list.html", students = students)
@app.route('/delete/<name>')
def delete(name):
sql = f"SELECT * FROM Students WHERE name='{escape(name)}'"
print(sql)
stmt = ibm_db.exec_immediate(conn, sql)
student = ibm_db.fetch_row(stmt)
print ("The Name is : ", student)
if student:
sql = f"DELETE FROM Students WHERE name='{escape(name)}'"
print(sql)
stmt = ibm_db.exec_immediate(conn, sql)
students = []
sql = "SELECT * FROM Students"
stmt = ibm_db.exec_immediate(conn, sql)
dictionary = ibm_db.fetch_both(stmt)
while dictionary != False:
students.append(dictionary)
dictionary = ibm_db.fetch_both(stmt)
if students:
return render_template("list.html", students = students, msg="Delete successfully")
# # while student != False:
# # print ("The Name is : ", student)
# print(student)
return "success..."
# @app.route('/posts/edit/<int:id>', methods=['GET', 'POST'])
# def edit(id):
# post = BlogPost.query.get_or_404(id)
# if request.method == 'POST':
# post.title = request.form['title']
# post.author = request.form['author']
# post.content = request.form['content']
# db.session.commit()
# return redirect('/posts')
# else:
# return render_template('edit.html', post=post)