Advertisement
Guest User

Initial Flask Attempt code

a guest
Mar 1st, 2024
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.91 KB | None | 0 0
  1. from flask import Flask, render_template
  2. import pymysql
  3.  
  4. app = Flask(__name__)
  5.  
  6. @app.route('/')
  7. def index():
  8. host = "127.0.0.1"
  9. port = 3306
  10. user = "root"
  11. password = ""
  12. dbname = "schedule"
  13.  
  14. try:
  15. con = pymysql.connect(host=host, port=port, user=user, password=password, db=dbname, cursorclass=pymysql.cursors.DictCursor)
  16. cursor = con.cursor()
  17.  
  18. # Example SQL query
  19. sql = "SELECT * FROM your_table_name"
  20. cursor.execute(sql)
  21. result = cursor.fetchall()
  22.  
  23. # Render HTML with the query result
  24. return render_template('index.html', data=result)
  25.  
  26. except pymysql.Error as e:
  27. error_msg = f"Could not connect to the database server: {e}"
  28. return render_template('error.html', error_msg=error_msg)
  29.  
  30. finally:
  31. cursor.close()
  32. con.close()
  33.  
  34. if __name__ == '__main__':
  35. app.run(debug=True)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement