-
Notifications
You must be signed in to change notification settings - Fork 0
/
import0.py
26 lines (19 loc) · 860 Bytes
/
import0.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
#same as lecture3. explicitly writing sql syntax.
import csv
import os
from sqlalchemy import create_engine
from sqlalchemy.orm import scoped_session, sessionmaker
app = Flask(__name__)
app.config["SQLALCHEMY_DATABASE_URI"] = "postgresql://postgres:5504655@localhost/lecture4"
app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False
db.init_app(app)
def main():
f = open("flights.csv")
reader = csv.reader(f)
for origin, destination, duration in reader: #looping every line in the CSV file.
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()