-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimport.py
58 lines (44 loc) · 1.34 KB
/
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import os
import csv
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))
f = open("books.csv")
reader = csv.reader(f, delimiter=',')
#skip headers
next(reader)
#create tables:
#table user_account
db.execute(
"""CREATE TABLE user_accounts (
user_id SERIAL PRIMARY KEY,
username VARCHAR(50) NOT NULL,
password VARCHAR(20) NOT NULL)"""
)
db.commit()
#table book
db.execute(
"""CREATE TABLE books (
book_isbn VARCHAR(13) PRIMARY KEY,
title VARCHAR(50) NOT NULL,
author VARCHAR(50) NOT NULL,
year SMALLINT)"""
)
db.commit()
#table review
db.execute(
"""CREATE TABLE reviews (
review_id SERIAL PRIMARY KEY,
book_id VARCHAR(13) REFERENCES book (book_isbn) NOT NULL,
user_id integer REFERENCES user_account (user_id) NOT NULL,
review_text VARCHAR(300),
review_rating SMALLINT NOT NULL)"""
)
db.commit()
#fill table books from .csv file
for isbn, title, author, year in reader:
db.execute("INSERT INTO books (book_isbn, title, author, year) VALUES (:isbn, :title, :author, :year)",
{"isbn": isbn, "title": title, "author": author, "year": year})
print(f"Added book {title} - ISBN: {isbn} - Author: {author} - Year: {year}")
db.commit()