Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import MySQLdb as mdb
- from view import *
- from flask import Flask,render_template, flash,url_for,redirect,request,jsonify
- from flask.ext.bootstrap import Bootstrap
- app= Flask(__name__)
- app.config['SECRET_KEY'] = 'Good luck Trying to Guess my password'
- bootstrap = Bootstrap(app)
- def connection():
- conn = mdb.connect(host='localhost',user='root',passwd='iloveGod123',db='GALAXZY')
- c=conn.cursor()
- return c,conn
- @app.route('/', methods=['GET', 'POST'])
- def index():
- form = NameForm(prefix = "form")
- form1 = CountryForm(prefix= "form1")
- form2 = CountyForm(prefix = "form2")
- form3 = CityForm(prefix = "form3")
- if form.validate_on_submit():
- Cont_Name = form.cont_Name.data
- Cont_Pop = form.cont_pop.data
- Cont_Area = form.cont_area.data
- Planet_Name = form.planet_Name.data
- try:
- c,conn = connection()
- c.execute("INSERT INTO Continent (Cont_Name,Cont_Pop, Cont_Area,Planet_Name) VALUES (%s,%s,%s,%s)",(Cont_Name,Cont_Pop,Cont_Area,Planet_Name))
- c.execute("INSERT INTO Planets (Planet_Name,Continents) VALUES (%s,%s)",(Planet_Name,Cont_Name))
- flash("Data has been inserted")
- except mdb.IntegrityError as e:
- flash('The Continent Name "%s" already exits in Database' % form.cont_Name.data)
- finally:
- conn.commit()
- c.close()
- return redirect(url_for('index'))
- elif form1.validate_on_submit():
- Cont_Name = form1.cont_Name.data
- Country_Name = form1.country_Name.data
- Country_Pop = form1.country_pop.data
- Country_Area = form1.country_area.data
- try:
- c,conn = connection()
- c.execute("INSERT INTO Country (Country_Name,Country_Pop, Country_Area,Cont_Name) VALUES (%s,%s,%s,%s)",(Country_Name,Country_Pop,Country_Area,Cont_Name))
- flash("Data has been inserted")
- except mdb.IntegrityError as e:
- flash('The Country Name "%s" already exits in Database' % form1.cont_Name.data)
- finally:
- conn.commit()
- c.close()
- return redirect(url_for('index'))
- elif form2.validate_on_submit():
- County_Name = form2.count_Name.data
- County_Pop = form2.count_pop.data
- County_Area = form2.count_area.data
- try:
- c,conn = connection()
- c.execute("INSERT INTO County (Cont_Name,Cont_Pop, Cont_Area) VALUES (%s,%s,%s)",(County_Name,County_Pop,County_Area))
- flash("Data has been inserted")
- except mdb.IntegrityError as e:
- flash('The County Name "%s" already exits in Database' % form2.count_Name.data)
- finally:
- conn.commit()
- c.close()
- return redirect(url_for('index'))
- elif form3.validate_on_submit():
- Town_Name = form3.town_Name.data
- City_State = form3.city_state.data
- City_Name = form3.city_Name.data
- City_Pop = form3.city_pop.data
- City_Area = form3.city_area.data
- try:
- c,conn = connection()
- c.execute("INSERT INTO City (City_Name,City_Pop, City_Area,City_State,Town_Name) VALUES (%s,%s,%s,%s,%s)",(City_Name,City_Pop,City_Area,City_State,Town_Name))
- flash("Data has been inserted")
- except mdb.IntegrityError as e:
- # flash('The City Name %(1) and State Name %(2) are already exits in Database ' %{"1":form3.city_Name.data, "2":form3.city_state.data})
- flash('The City Name "%s" already exits in Database' % form3.city_Name.data)
- finally:
- conn.commit()
- c.close()
- return redirect(url_for('index'))
- return render_template('main.html',form=form,form1=form1,form2=form2,form3=form3)
- @app.route("/delete", methods=['POST','GET'])
- def deletes():
- formA= DeleteContinent(prefix = "formA")
- form1A = DeleteCountry(prefix= "form1A")
- form2A = DeleteCounty(prefix = "form2A")
- form3A = DeleteCity(prefix = "form3A")
- if formA.validate_on_submit():#request.method=='POST':
- cont_key = formA.del_cont.data
- try:
- c,conn = connection()
- c.execute('delete FROM Continent where Cont_Name = %s',(cont_key,))
- flash("All rows deleted")
- except:
- flash('The row with value "%s" is not found in the table' %formA.del_cont.data)
- finally:
- conn.commit()
- c.close()
- return redirect(url_for('deletes'))
- elif form1A.validate_on_submit():#request.method=='POST':
- count_key = form1A.del_count.data
- try:
- c,conn = connection()
- c.execute('delete FROM Country where Country_Name = %s',(count_key,))
- except:
- flash('The row with value "%s" is not found in the table' %form1A.count_key.data)
- finally:
- conn.commit()
- c.close()
- return redirect(url_for('deletes'))
- elif form2A.validate_on_submit():#request.method=='POST':
- conty_key = form2A.del_conty.data
- try:
- c,conn = connection()
- c.execute('delete FROM County where Cont_Name = %s',(conty_key,))
- flash("All rows deleted")
- except:
- flash('The row with value "%s" is not found in the table' %form2A.conty_key.data)
- finally:
- conn.commit()
- c.close()
- return redirect(url_for('deletes'))
- elif form3A.validate_on_submit():#request.method=='POST':
- city_key = form3A.del_city.data
- try:
- c,conn = connection()
- c.execute('delete FROM City where City_Name = %s',(city_key,))
- flash("All rows deleted")
- except:
- flash('The row with value "%s" is not found in the table' %formA.city_key.data)
- finally:
- conn.commit()
- c.close()
- return redirect(url_for('deletes'))
- return render_template("delete.html",formA=formA,form1A=form1A,form2A=form2A,form3A=form3A)
- @app.route("/update")
- def updates():
- return render_template("update.html")
- @app.route("/viewSingle", methods=['POST','GET'])
- def SingleView():
- if request.method=="POST":
- if request.form['submit']=="View CONTINENT":
- c,conn = connection()
- c.execute('''select * from Country''')
- data = c.fetchall ()
- c.close ()
- conn.close ()
- return render_template("view.html",data=data)
- @app.route("/viewAll")
- def viewsAll():
- c,conn = connection()
- c.execute('''select * from Country''')
- #data=c.fetchall()
- #data1=json.dumps(data)
- return str([columns[0] for columns in c.fetchall()])
- #return str(a)
- c.close()
- conn.close()
- #return a,b,c,d
- #render_template("view.html")
- if __name__ == "__main__":
- app.run(debug = True,host='0.0.0.0')
Advertisement
Add Comment
Please, Sign In to add comment