-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatabase.py
39 lines (34 loc) · 1.5 KB
/
database.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
import os
from sqlalchemy import *
from sqlalchemy.pool import NullPool
from flask import Flask, request, render_template, g, redirect, Response
tmpl_dir = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'templates')
app = Flask(__name__, template_folder=tmpl_dir)
#
# The following is a dummy URI that does not connect to a valid database. You will need to modify it to connect to your Part 2 database in order to use the data.
#
# XXX: The URI should be in the format of:
#
# postgresql://USER:[email protected]/project1
#
# For example, if you had username zy2431 and password 123123, then the following line would be:
#
# DATABASEURI = "postgresql://zy2431:[email protected]/project1"
#
# Modify these with your own credentials you received from TA!
DATABASE_USERNAME = "lw2980"
DATABASE_PASSWRD = "3316"
DATABASE_HOST = "34.148.107.47" # change to 34.28.53.86 if you used database 2 for part 2
DATABASEURI = f"postgresql://{DATABASE_USERNAME}:{DATABASE_PASSWRD}@{DATABASE_HOST}/project1"
#
# This line creates a database engine that knows how to connect to the URI above.
#
engine = create_engine(DATABASEURI)
with engine.connect() as conn:
command = """
select t.name , count(*) as num from (select a.name as name from customer as c left join book as b on c.customer_id=b.customer_id left join airlines as a on a.airline_id=b.airline_id where c.name='Anna Ball') as t group by t.name
"""
res = conn.execute(text(command))
res=res.fetchall()
print(res)
conn.commit()