forked from PDXPythonPirates/cs50w-lecture-sql
-
Notifications
You must be signed in to change notification settings - Fork 0
/
import.py
24 lines (19 loc) · 793 Bytes
/
import.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
import csv
import os
from sqlalchemy import create_engine
from sqlalchemy.orm import scoped_session, sessionmaker
conn_args = {}
if 'pg8000' in os.getenv('DATABASE_URL'):
conn_args = {'ssl': True}
engine = create_engine(os.getenv("DATABASE_URL"), connect_args=conn_args)
db = scoped_session(sessionmaker(bind=engine))
def main():
f = open("flights.csv")
reader = csv.reader(f)
for origin, destination, duration in reader:
db.execute("INSERT INTO flights (origin, destination, duration) VALUES (:origin, :destination, :duration)",
{"origin": origin, "destination": destination, "duration": duration})
print(f"Added flight from {origin} to {destination} lasting {duration} minutes.")
db.commit()
if __name__ == "__main__":
main()