conn = sqlite3.connect('tealearn_grades.db', check_same_thread=False)
import sqlite3
import os
dir_path = os.path.dirname(os.path.realpath(__file__))
def connect_db():
#create and connect to the database
conn = sqlite3.connect(dir_path+'/tealearn_grades.db', check_same_thread=False)
return conn
from flask import Blueprint, render_template, request
from database import connect_db
conn = connect_db()
student_bp=Blueprint("student_blueprint", __name__, static_folder="static", template_folder="templates")
#load form
@student_bp.route('/student')
def student():
return render_template('student.html')
#display record
@student_bp.route('/seestudents')
def seestudents():
cursor = conn.cursor()
cursor.execute('SELECT * FROM student')
data = cursor.fetchall()
return render_template('seestudents.html', students=data)
#process the student add
@student_bp.route('/addstudent', methods=['GET', 'POST'])
def addstudent():
if request.method == 'POST':
surname = request.form['surname']
othernames = request.form['othernames']
address = request.form['address']
conn.execute('INSERT INTO student (surname_name,other_name,address_address) VALUES (?,?,?)',(surname, othernames, address))
conn.commit()
output="Student record saved"
return render_template('student.html', msg=output)
from flask import Blueprint, render_template, request
from database import connect_db
conn = connect_db()
tables_bp=Blueprint("table_create_blueprint", __name__, template_folder="templates")
#setup the tables in the database
@tables_bp.route('/dbsetup')
def dbsetup():
sql_create_student_table='CREATE TABLE IF NOT EXISTS student (student_id INTEGER PRIMARY KEY, surname_name TEXT, other_name TEXT, address_address TEXT)'
sql_create_instructor_table='CREATE TABLE IF NOT EXISTS instructor (instructor_id INTEGER PRIMARY KEY, surname_name TEXT, other_name TEXT, address_address TEXT, qualification TEXT)'
sql_create_subject_table='CREATE TABLE IF NOT EXISTS subject (subject_id INTEGER PRIMARY KEY, instructor_id INTEGER, subject_title TEXT, more_detail TEXT, max_score INTEGER)'
sql_create_score_table='CREATE TABLE IF NOT EXISTS student_score (score_id INTEGER PRIMARY KEY, score_value INTEGER, student_id INTEGER, subject_id INTEGER)'
try:
#use the connection to execute the queries to create the tables
conn.execute(sql_create_student_table)
conn.commit()
conn.execute(sql_create_instructor_table)
conn.commit()
conn.execute(sql_create_subject_table)
conn.commit()
conn.execute(sql_create_score_table)
conn.commit()
output="db setup successful"
except sqlite3.Error as er:
output=er.sqlite_errorname
return render_template('dbsetup.html', msg=output)
from flask import Flask, render_template, request
from database import connect_db
conn = connect_db()
from student_blueprint import student_bp
from table_create_blueprint import tables_bp
app = Flask(__name__)
#register or access the blueprints
app.register_blueprint(student_bp)
app.register_blueprint(tables_bp)
#load the home or index page
@app.route('/home')
@app.route('/')
def index():
return render_template('index.html')
if __name__ == '__main__':
app.run(debug=True)
No Comment yet!