-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimport.py
35 lines (30 loc) · 977 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
25
26
27
28
29
30
31
32
33
34
35
import os
import csv
from sqlalchemy import create_engine
from sqlalchemy.orm import scoped_session, sessionmaker
from sqlalchemy.sql import text
engine = create_engine(os.getenv("DATABASE_URL"))
db = scoped_session(sessionmaker(bind=engine))
# Check for environment variable
if not os.getenv("DATABASE_URL"):
raise RuntimeError("DATABASE_URL is not set")
with open("books.csv") as books:
rows = csv.DictReader(books)
for row in rows:
try:
q = "INSERT INTO books(isbn, title, author, year) VALUES(:i, :t, :a, :y)"
qi = text(q)
db.execute(
qi,
{
"i": row["isbn"],
"t": row["title"],
"a": row["author"],
"y": row["year"],
},
)
except:
db.rollback()
raise Exception("an error occurred when importing a book")
else:
db.commit()