├── README.md ├── app.py ├── requirements.txt └── templates └── index.html /README.md: -------------------------------------------------------------------------------- 1 | # python-mysql-db-proj-1 -------------------------------------------------------------------------------- /app.py: -------------------------------------------------------------------------------- 1 | from flask import Flask, jsonify, render_template, request 2 | import pymysql 3 | 4 | app = Flask(__name__) 5 | 6 | def get_db_connection(): 7 | connection = pymysql.connect(host='mydb.cylck8yh5jkc.eu-central-1.rds.amazonaws.com', # Replace with your RDS endpoint 8 | user='dbuser', # Replace with your RDS username 9 | password='dbpassword', # Replace with your RDS password 10 | db='devprojdb', # Replace with your database name 11 | charset='utf8mb4', 12 | cursorclass=pymysql.cursors.DictCursor) 13 | return connection 14 | 15 | @app.route('/health') 16 | def health(): 17 | return "Up & Running" 18 | 19 | @app.route('/create_table') 20 | def create_table(): 21 | connection = get_db_connection() 22 | cursor = connection.cursor() 23 | create_table_query = """ 24 | CREATE TABLE IF NOT EXISTS example_table ( 25 | id INT AUTO_INCREMENT PRIMARY KEY, 26 | name VARCHAR(255) NOT NULL 27 | ) 28 | """ 29 | cursor.execute(create_table_query) 30 | connection.commit() 31 | connection.close() 32 | return "Table created successfully" 33 | 34 | @app.route('/insert_record', methods=['POST']) 35 | def insert_record(): 36 | name = request.json['name'] 37 | connection = get_db_connection() 38 | cursor = connection.cursor() 39 | insert_query = "INSERT INTO example_table (name) VALUES (%s)" 40 | cursor.execute(insert_query, (name,)) 41 | connection.commit() 42 | connection.close() 43 | return "Record inserted successfully" 44 | 45 | @app.route('/data') 46 | def data(): 47 | connection = get_db_connection() 48 | cursor = connection.cursor() 49 | cursor.execute('SELECT * FROM example_table') 50 | result = cursor.fetchall() 51 | connection.close() 52 | return jsonify(result) 53 | 54 | # UI route 55 | @app.route('/') 56 | def index(): 57 | return render_template('index.html') 58 | 59 | if __name__ == '__main__': 60 | app.run(debug=True, host='0.0.0.0') 61 | -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | Flask 2 | pymysql -------------------------------------------------------------------------------- /templates/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | Flask REST API UI 7 | 8 | 9 |

Flask REST API UI

10 | 11 |

Insert Record

12 |
13 | 14 | 15 | 16 |
17 | 18 |

View Data

19 | 20 | 21 | 55 | 56 | 57 | --------------------------------------------------------------------------------