forked from muralialakuntla3/flask-postgresql
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
48 lines (40 loc) · 1.23 KB
/
Copy pathapp.py
File metadata and controls
48 lines (40 loc) · 1.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
from flask import Flask, request, render_template_string
import psycopg2
app = Flask(__name__)
# PostgreSQL connection config
conn = psycopg2.connect(
host="azure2501db.postgres.database.azure.com",
database="demo_app",
user="adminuser",
password="Crmedify@12345"
)
cursor = conn.cursor()
# HTML Form Template
form_template = """
<!doctype html>
<title>PostgreSQL Form</title>
<h2>Enter User Details</h2>
<form method=post>
Name: <input type=text name=name><br><br>
Email: <input type=email name=email><br><br>
Number: <input type=text name=number><br><br>
<input type=submit value=Submit>
</form>
{% if message %}
<p><strong>{{ message }}</strong></p>
{% endif %}
"""
@app.route("/", methods=["GET", "POST"])
def index():
message = ""
if request.method == "POST":
name = request.form["name"]
email = request.form["email"]
number = request.form["number"]
sql = "INSERT INTO users (name, email, number) VALUES (%s, %s, %s)"
cursor.execute(sql, (name, email, number))
conn.commit()
message = "Data inserted successfully!"
return render_template_string(form_template, message=message)
if __name__ == "__main__":
app.run(host="0.0.0.0", port=5000, debug=True)