forked from render-examples/flask-hello-world
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
77 lines (71 loc) · 2.44 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
from flask import Flask
import psycopg2
app = Flask(__name__)
@app.route('/')
def hello_world():
return 'Hello World from Kathryn Woessner in 3308'
@app.route('/db_test')
def testing():
conn = psycopg2.connect("postgresql://katie_3308_lab10_user:RV4E0w9kat01p8aZxkf80Yx5hbHGLzkX@dpg-csj511q3esus7380aaf0-a/katie_3308_lab10")
conn.close()
return "Database Connection Successful"
@app.route('/db_create')
def creating():
conn = psycopg2.connect("postgresql://katie_3308_lab10_user:RV4E0w9kat01p8aZxkf80Yx5hbHGLzkX@dpg-csj511q3esus7380aaf0-a/katie_3308_lab10")
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://katie_3308_lab10_user:RV4E0w9kat01p8aZxkf80Yx5hbHGLzkX@dpg-csj511q3esus7380aaf0-a/katie_3308_lab10")
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://katie_3308_lab10_user:RV4E0w9kat01p8aZxkf80Yx5hbHGLzkX@dpg-csj511q3esus7380aaf0-a/katie_3308_lab10")
cur = conn.cursor()
cur.execute('''
SELECT * FROM Basketball;
''')
records = cur.fetchall()
conn.commit()
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 drop():
conn = psycopg2.connect("postgresql://katie_3308_lab10_user:RV4E0w9kat01p8aZxkf80Yx5hbHGLzkX@dpg-csj511q3esus7380aaf0-a/katie_3308_lab10")
cur = conn.cursor()
cur.execute('''
DROP TABLE Basketball;
''')
conn.commit()
conn.close()
return "Basketball Table Successfully Dropped"