from flask import Flask , render_template, request
import sqlite3
app=Flask(‘main’)
conn=sqlite3.connect(“database.db”)
cur=conn.cursor()
cur.execute(‘Select count(*) from sqlite_master where type =“table” and name= “student”’)
if cur.fetchone()[0]==1:
print("table exist")
else:
conn.execute("create table student(Name TEXT ,Address TEXT , City TEXT , Pin TEXT)")
print("table created")
conn.close()
@app.route(’/’)
def first():
return render_template("index.html")
@app.route(’/addstudent’)
def second():
return render_template("/add_student.html")
@app.route(’/savestudent’ ,methods=[‘POST’,‘GET’])
def save_student():
msg=""
if request.method =='POST':
try:
name=request.form.get('Studname')
addr=request.form.get('Studaddress')
city=request.form.get('Studcity')
pin=request.form.get('Studpin')
with sqlite3.connect('database.db') as con:
cur=conn.cursor()
cur.execute("INSERT INTO student(name,city,addr,pin)Values(?,?,?,?)",(name,addr,city,pin))
conn.commit()
msg="data inserted"
except:
conn.rollback()
msg="data not inserted"
return render_template('success.html',msg=msg)
conn.close()
if name ==‘main’:
app.run(debug=True)