forked from render-examples/flask-hello-world
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
93 lines (77 loc) · 2.73 KB
/
app.py
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
## Mohammed Alsailani
## 11/3/2024
## CSPB 3308 - Lab 10
###############################################################################
## This is for testing deployment of website using Flask
##
###############################################################################
import psycopg2
from flask import Flask
app = Flask(__name__)
@app.route('/')
def hello_world():
return 'Hello World from -- Mohammed Alsailani -- in 3308'
@app.route('/db_test')
def testing():
conn = psycopg2.connect("postgresql://mohammed_db_user:UmSc7JQQWVM3IqL8sbwxtGBI8I4cINRV@dpg-csj5d6btq21c73d9b840-a/mohammed_db")
conn.close()
return "Database Connection Successful"
@app.route('/db_create')
def creating():
conn = psycopg2.connect("postgresql://mohammed_db_user:UmSc7JQQWVM3IqL8sbwxtGBI8I4cINRV@dpg-csj5d6btq21c73d9b840-a/mohammed_db")
cur = conn.cursor()
cur.execute('''
CREATE TABLE IF NOT EXISTS Basketball (
First varchar(255),
Last varchar(255),
City varchar(255),
Name varchar(255),
Number int
);
''')
conn.commit()
conn.close()
return "Basketball Table Successfully Created"
@app.route('/db_insert')
def inserting():
conn = psycopg2.connect("postgresql://mohammed_db_user:UmSc7JQQWVM3IqL8sbwxtGBI8I4cINRV@dpg-csj5d6btq21c73d9b840-a/mohammed_db")
cur = conn.cursor()
cur.execute('''
INSERT INTO Basketball (First, Last, City, Name, Number)
VALUES
('Jayson', 'Tatum', 'Boston', 'Celtics', 0),
('Stephen', 'Curry', 'San Francisco', 'Warriors', 30),
('Nikola', 'Jokic', 'Denver', 'Nuggets', 15),
('Kawhi', 'Leonard', 'Los Angeles', 'Clippers', 2);
''')
conn.commit()
conn.close()
return "Basketball Table Successfully Populated"
@app.route('/db_select')
def selecting():
conn = psycopg2.connect("postgresql://mohammed_db_user:UmSc7JQQWVM3IqL8sbwxtGBI8I4cINRV@dpg-csj5d6btq21c73d9b840-a/mohammed_db")
cur = conn.cursor()
cur.execute('''
SELECT * FROM Basketball;
''')
records = cur.fetchall()
conn.close()
response_string = ""
response_string += "<table>"
for player in records:
response_string += "<tr>"
for info in player:
response_string += "<td>{}</td>".format(info)
response_string += "</tr>"
response_string += "</table>"
return response_string
@app.route('/db_drop')
def dropping():
conn = psycopg2.connect("postgresql://mohammed_db_user:UmSc7JQQWVM3IqL8sbwxtGBI8I4cINRV@dpg-csj5d6btq21c73d9b840-a/mohammed_db")
cur = conn.cursor()
cur.execute('''
DROP TABLE Basketball;
''')
conn.commit()
conn.close()
return "Basketball Table Successfully Dropped"