-
Notifications
You must be signed in to change notification settings - Fork 0
/
std_db.py
47 lines (37 loc) · 1.14 KB
/
std_db.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
import sqlite3
from flask import Flask, render_template, request
app = Flask(__name__)
@app.route('/')
def home():
return 'hello word!'
@app.route('/std_db')
def std_db():
# step 1: connect to db
conn = sqlite3.connect('std3.sqlite')
# step2: define cursor
cur = conn.cursor()
# step3: execute SQL statment
# SQL query: select <field name(s)> from table
# select std_id, dept from std_info
cur.execute('select * from std_info')
# step 4: fetch records
rows = cur.fetchall()
# print(rows)
return render_template('std_db3.html', rows=rows)
@app.route('/std')
def std():
student_id = request.args.get('stdid')
# step 1: connect to db
conn = sqlite3.connect('std3.sqlite')
# step2: define cursor
cur = conn.cursor()
# step3: execute SQL statement
# SQL query: select <field name(s)> from table
# select std_id, dept from std_info
cur.execute('select * from std_info where std_id={}'.format(student_id))
# step 4: fetch records
rows = cur.fetchall()
# print(rows)
return render_template('std_db3.html', rows=rows)
if __name__ == '__main__':
app.run()