Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from flask import Flask, render_template
- import pymysql
- app = Flask(__name__)
- @app.route('/')
- def index():
- host = "127.0.0.1"
- port = 3306
- user = "root"
- password = ""
- dbname = "schedule"
- try:
- con = pymysql.connect(host=host, port=port, user=user, password=password, db=dbname, cursorclass=pymysql.cursors.DictCursor)
- cursor = con.cursor()
- # Example SQL query
- sql = "SELECT * FROM your_table_name"
- cursor.execute(sql)
- result = cursor.fetchall()
- # Render HTML with the query result
- return render_template('index.html', data=result)
- except pymysql.Error as e:
- error_msg = f"Could not connect to the database server: {e}"
- return render_template('error.html', error_msg=error_msg)
- finally:
- cursor.close()
- con.close()
- if __name__ == '__main__':
- app.run(debug=True)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement