-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimport.py
29 lines (22 loc) · 810 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
import csv
import os
from sqlalchemy import create_engine
from sqlalchemy.orm import scoped_session, sessionmaker
engine = create_engine(os.getenv("DATABASE_URL"))
db = scoped_session(sessionmaker(bind=engine))
def main():
f = open("books.csv")
reader = csv.reader(f)
for isbn,title,author,year in reader:
try:
ISBN = str(isbn)
TITLE = str(title)
AUTHOR = str(author)
YEAR = int(year)
db.execute("INSERT INTO book(isbn,title,author,year) VALUES(:isbn, :title, :author, :year)",{"isbn":ISBN, "title":TITLE, "author":AUTHOR, "year":YEAR})
print(f"Added BOOK OF {ISBN} AND {TITLE} AUTHOR {AUTHOR} WRONG {YEAR}.")
except ValueError:
print("wrong")
db.commit()
if __name__ == "__main__":
main()