Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import psycopg2
- import psycopg2.extras
- from flask import Flask, render_template, request
- app = Flask(__name__)
- def getConn():
- #function to retrieve the password, construct
- #the connection string then make the connection and return it.
- pwFile = open("pw.txt", "r")
- pw = pwFile.read();
- pwFile.close()
- connStr = "host='localhost' dbname= 'DBcw' user='postgres' \
- password = "+ pw
- # FOR LAB MACHINES
- # connStr = "host='cmpstudb-01.cmp.uea.ac.uk' \
- # dbname= 'DBcw' user='yfs15buu' password = " + pw
- conn=psycopg2.connect(connStr)
- return conn
- @app.route('/')
- def home():
- return render_template('home.html')
- @app.route('/displaycat', methods =['GET'])
- def displayCat():
- conn= None
- try:
- conn=getConn()
- cur = conn.cursor()
- cur.execute('SET search_path to public')
- cur.execute('SELECT * FROM category')
- colNames = [desc[0] for desc in cur.description]
- cate = cur.fetchall()
- return render_template('showCategory.html', cate = cate, \
- colNames = colNames)
- except Exception as e:
- return render_template('home.html', msg = 'An error occured', error = e)
- finally:
- if conn:
- conn.close()
- if __name__ == "__main__":
- app.run(debug = True)
Add Comment
Please, Sign In to add comment