forked from marcorichetta/cs50w-project1
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimport.py
More file actions
executable file
·27 lines (18 loc) · 793 Bytes
/
import.py
File metadata and controls
executable file
·27 lines (18 loc) · 793 Bytes
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
import os, csv
from sqlalchemy import create_engine
from sqlalchemy.orm import scoped_session, sessionmaker
# database engine object from SQLAlchemy that manages connections to the database
engine = create_engine(os.getenv("DATABASE_URL"))
# create a 'scoped session' that ensures different users' interactions with the
# database are kept separate
db = scoped_session(sessionmaker(bind=engine))
file = open("books.csv")
reader = csv.reader(file)
for isbn, title, author, year in reader:
db.execute("INSERT INTO books (isbn, title, author, year) VALUES (:isbn, :title, :author, :year)",
{"isbn": isbn,
"title": title,
"author": author,
"year": year})
print(f"Added book {title} to database.")
db.commit()